Here is the date validation code.
Expand|Select|Wrap|Line Numbers
- // 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
I'm not to sure about the syntax of java or how to do this, lol thats why i'm asking you guys, but
if (!Validator.isValidDate(fromDate)) {
LOG.fine("From Date contains an invalid date:" + fromDate);
return mapping.findForward(NOT_FOUND_FORWARD);
}
What i THINK this will do is run the code from the isValidDate method to check the date in the fromDate field, and if it is not right it will kick them to the NOT_FOUND_FORWARD page and tell them that From Date contains an invalid date.
That is pretty much what I want to is check the fromDate and toDate fields and make sure they are valid and if not kick them to an error page.
Hopefully i didn't confuse anyone lol.