By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,482 Members | 3,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,482 IT Pros & Developers. It's quick & easy.

Date Validation Question

Stang02GT
Expert 100+
P: 1,208
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to call it. It was suggested to be to have it run through an if statement and if the user enters an invalid date it will kick them to an error page.

Here is the date validation code.

Expand|Select|Wrap|Line Numbers
  1. // date validation using SimpleDateFormat
  2. // it will take a string and make sure it's in the proper 
  3. // format as defined by you, and it will also make sure that
  4. // it's a legal date
  5.  
  6. public boolean isValidDate(String date)
  7. {
  8.    // set date format, this can be changed to whatever format
  9.    // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
  10.    // you can read more about it here:
  11.    // http://java.sun.com/j2se/1.4.2/docs/api/index.html
  12.  
  13.    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
  14.  
  15.    // declare and initialize testDate variable, this is what will hold
  16.    // our converted string
  17.  
  18.    Date testDate = null;
  19.  
  20.    // we will now try to parse the string into date form
  21.    try
  22.    {
  23.      testDate = sdf.parse(date);
  24.    }
  25.  
  26.    // if the format of the string provided doesn't match the format we 
  27.    // declared in SimpleDateFormat() we will get an exception
  28.  
  29.    catch (ParseException e)
  30.    {
  31.      errorMessage = "the date you provided is in an invalid date" +
  32.                              " format.";
  33.      return false;
  34.    }
  35.  
  36.    // dateformat.parse will accept any date as long as it's in the format
  37.    // you defined, it simply rolls dates over, for example, december 32 
  38.    // becomes jan 1 and december 0 becomes november 30
  39.    // This statement will make sure that once the string 
  40.    // has been checked for proper formatting that the date is still the 
  41.    // date that was entered, if it's not, we assume that the date is invalid
  42.  
  43.    if (!sdf.format(testDate).equals(date)) 
  44.    {
  45.      errorMessage = "The date that you provided is invalid.";
  46.      return false;
  47.    }
  48.  
  49.    // if we make it to here without getting an error it is assumed that
  50.    // the date was a valid one and that it's in the proper format
  51.  
  52.    return true;
  53.  
  54. } // end isValidDate
  55.  

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.
Jan 17 '08 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
If you want the method to be available without creating an instance of its enclosing class then make it static as well and Just call it like you've done above.
Jan 17 '08 #2

Stang02GT
Expert 100+
P: 1,208
Ok, well now i guess my next question would be there is also a toDate field, is there a way that i can include bother fromDate and toDate in one statement could i start it like this?

Expand|Select|Wrap|Line Numbers
  1. if (!Validator.isValid(fromDate) || !Validator.isValid(toDate))
How can i change the rest of my if statement to include toDate?

I apologize for my lack of knowledge in Java :( I'm just starting to learn it.
Jan 17 '08 #3

BigDaddyLH
Expert 100+
P: 1,216
Ok, well now i guess my next question would be there is also a toDate field, is there a way that i can include bother fromDate and toDate in one statement could i start it like this?
Expand|Select|Wrap|Line Numbers
  1. if (!Validator.isValid(fromDate) || !Validator.isValid(toDate))
Could you? Have you tried it? I suggest you write small unit tests for your code so that you can see if it works without having to go through the web layer for each test.

Unit Testing
Jan 17 '08 #4

Stang02GT
Expert 100+
P: 1,208
For this date validation issue. I am still trying to develop this IF statement. I got side tracked with another project and now have time to go back and focus on this.

So if i need to validate this date, i want to check both fields (fromDate, toDate) against the function. If there is nothing wrong with the date then I just do nothing?
Else send them to the error page telling them they entered an invalid date.

I apologize for my ignorance with this but i am trying to learn Java, could you possibly help me to develop this statement and better understand it?

I have the general idea of what i want to do, but i am not sure how to code it.
Jan 24 '08 #5

Stang02GT
Expert 100+
P: 1,208
Actually I figured it out.

I was thinking too hard about something that was so simple.

Expand|Select|Wrap|Line Numbers
  1. if (!Validator.isValid(fromDate) || !Validator.isValid(toDate)) {
  2.     LOG.fine("From Date and To Date are not the correct format:" + fromDate + ", " + toDate);
  3.     return mapping.findForward(NOT_FOUND_FORWARD);
  4. }
This if statement is checking to see if the dates are "not valid". It translates to - "If fromDate is not valid or toDate is not valid, so i just needed to add my code for the error message and I was done.
Jan 24 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.