fyrestorm, 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.
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






MultiQuote


|