• (2 Pages)
  • +
  • 1
  • 2

Date Validation using SimpleDateFormat() Rate Topic: ***** 1 Votes

#16 TomR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-July 11

Posted 06 July 2011 - 12:16 PM

I am not a programmer so will likely be in contravention of some rule. But I have a problem. I keep getting the error message: "Out of Valid Date". Often the message is accompanied with a 'java script..." note at the bottom of my screen. So I am wondering if the error message has something to do with the Java software that is automatically updated on my computer. No other blog seems to answer the question. I am hoping that someone in this blog has the answer.


View Postfyrestorm, on 24 February 2006 - 11:16 AM, said:

I was told that the JDK class SimpleDateFormat() might be useful for me to use in validating dates for a program that I'm working on.

Unfortunately for me, the documentation was crummy and it took a while to figure out what I needed to do. Fortunately for all that are reading this, after many hours of pain on my end, I can now write a tutorial for all the world to see.

This is very elementary in that it will only check to see if the date supplied is in the proper fomat that meets your specifications and it will check to see if the date is a proper date, meaning that someone can't try to enter a date such as 02/30/06.

In the future, I may add more robustness to this tutorial, but as it stands, this is all that I needed for my current assignment and I know that there are a number of other students stuggling with this so I thought it would be beneficial to post this tutorial as it is.

// date validation using SimpleDateFormat
// it will take a string and make sure it's in the proper 
// format as defined by you, and it will also make sure that
// it's a legal date

public boolean isValidDate(String date)
{
    // set date format, this can be changed to whatever format
    // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
    // you can read more about it here:
    // http://java.sun.com/j2se/1.4.2/docs/api/index.html
    
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    
    // declare and initialize testDate variable, this is what will hold
    // our converted string
    
    Date testDate = null;

    // we will now try to parse the string into date form
    try
    {
      testDate = sdf.parse(date);
    }

    // if the format of the string provided doesn't match the format we 
    // declared in SimpleDateFormat() we will get an exception

    catch (ParseException e)
    {
      errorMessage = "the date you provided is in an invalid date" +
                              " format.";
      return false;
    }

    // dateformat.parse will accept any date as long as it's in the format
    // you defined, it simply rolls dates over, for example, december 32 
    // becomes jan 1 and december 0 becomes november 30
    // This statement will make sure that once the string 
    // has been checked for proper formatting that the date is still the 
    // date that was entered, if it's not, we assume that the date is invalid

    if (!sdf.format(testDate).equals(date)) 
    {
      errorMessage = "The date that you provided is invalid.";
      return false;
    }
    
    // if we make it to here without getting an error it is assumed that
    // the date was a valid one and that it's in the proper format

    return true;

} // end isValidDate


Was This Post Helpful? 0
  • +
  • -

#17 mcc99  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-September 12

Posted 06 September 2012 - 02:03 PM

This was great. I did discover one weird thing about using SDF though on a string that is of the form xx/xx/xxxx. If you hand it a "date" string of xx/xx/xxxx/xx, etc. even if lenient is set to false, you will NOT get a format error! So what I did was simply take the string in question and run it through StringTokenizer and them count tokens. If there are not three tokens, I throw an Exception. Since talk is cheap, here's my code:

		try{
			  SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/YYYY");
			  sdf.setLenient(false);
			  Date dt = sdf.parse(datestringwearetesting);
			  StringTokenizer st = new StringTokenizer(datestringwearetesting,"/");
			  // Why?  Bizarrely, if you hand sdf.parse() a date like 21/12/34/45, it will
			  // not throw an exception.  A BIG bug?  You betchya!
			  if(st.countTokens()!=3){
				  throw new Exception();
				  }
		  } catch (Exception e)
		  {
                    // Do whatever
		  }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2