Hello everybody,
We are moving all our validations from client side (java script) to server side (servlets/jsp). I need to know how to do the following
- How to validate that a String contains a valid date while we have a date format "YYYY/MM/DD"
- If we have a valid date in String, how to convert it into java.util.Date from String
- If we have a java.util.Date, how to convert it into different date formats like DD MONTH YYYY
Thanks,
fa.
-
Date Validation In Servlets/JSPs (3 messages)
- Posted by: Farhan Alam
- Posted on: May 30 2001 10:01 EDT
Threaded Messages (3)
- Date Validation In Servlets/JSPs by Dan Josephs on May 30 2001 10:15 EDT
- Date Validation In Servlets/JSPs by Jason McKerr on May 30 2001 12:41 EDT
- Date Validation In Servlets/JSPs by Keith McRae on May 31 2001 05:26 EDT
-
Date Validation In Servlets/JSPs[ Go to top ]
- Posted by: Dan Josephs
- Posted on: May 30 2001 10:15 EDT
- in response to Farhan Alam
There are a bunch of helper classes that you should take a look at in the java.text package:
DateFormat
SimpleDateFormat
These classes should be able to help you convert dates and strings pretty easily.
Dan -
Date Validation In Servlets/JSPs[ Go to top ]
- Posted by: Jason McKerr
- Posted on: May 30 2001 12:41 EDT
- in response to Farhan Alam
Here are a pair of overloaded methods for validating a Date string, the return true(valid) false(invalid). The first one allows the caller to set the date pattern (i.e. 'mm/dd/yy'), the second one I use as sort of the "default" format. An exception is thrown if the date passed in can't be parsed properly.
They are a little heavy, but I'm kinda of paranoid about dates, so they are very exacting. You could probably loosen them up quite a bit. They'd be faster.
Two side notes.
1) Make sure that you use setLenient=false.
This is very important. If you don't then the date 05/99/99
will be valid (I'm trying to remember here but I think Java just adds 99 days to the first day of may or something like that).
2) Then length test is important as you'll see. Since without it the date '05/05/1999a' would return as valid, since the date parse only gets the part that is matched with the parse pattern.
Here ya go.
public static boolean checkDate(String inDate, String dateFormat) {
int dateFormatLength = dateFormat.length();
try {
if (inDate.length() != dateFormatLength)
throw new Exception();
else {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
format.setLenient(false);
Date theDate = new Date();
theDate = format.parse(inDate);
return true;
}
}
catch(Exception e) {
return false;
}
}
public static boolean checkDate(String inDate) {
String dateFormat = "MM/dd/yyyy";
int dateFormatLength = dateFormat.length();
try {
if (inDate.length() != dateFormatLength)
throw new Exception();
else {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
format.setLenient(false);
Date theDate = new Date();
theDate = format.parse(inDate);
return true;
}
}
catch(Exception e) {
return false;
}
}
-
Date Validation In Servlets/JSPs[ Go to top ]
- Posted by: Keith McRae
- Posted on: May 31 2001 05:26 EDT
- in response to Jason McKerr
Might be worth having a look at http://developer.java.sun.com/developer/qow/archive/120/index.html