tags:

views:

55

answers:

3

Hi, Can any one tell me solution for the following: I have a Util.Date object in java.I want to validate the date entered. I am parsing the date object using the required format.

For ex, Format is "MM/dd/yyyy" and Date entered is "23/12/2010" For this date, I am not getting any Parse Exception but the date is adjusted to "11/12/2011" which should not happen in my case.Instead I want to throw error message.

Please help me in this asap.

Thanks in advance

+2  A: 

In DateFormat class you have to reset "lenient" flag, i.e.

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
. . .
sdf.parse(mydate, pos);
Pier Luigi
Thanks a lot. It worked as I want.
asd
A: 

I think SimpleDateFormat class can help you in parsing date to correct format. Let me know if it helps.

abhishekshahi
+3  A: 

You must set your "lenient" mode of the SimpleDateFormat to false:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);

By default it is tolerant to some errors, and tries to interpret them somehow.

Eyal Schneider