Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SimpleDateFormat to decide if a date string is a valid date?

807580May 24 2010 — edited May 25 2010
Hi, I'm using SimpleDateFormat to verify the format of user entered date string.

Also I need to verify if the date entered is valid, meaning that if I use the pattern yyyy-MM-dd the string 2010-02-30 will be identified as invalid date.
It happens that if I call setLenient(false) in a SimpleDateFormat instance before calling it's method parse("2010-02-30"), it will throw ParseException, identifying an invalid date.

But in no way the documentation of the SimpleDateFormat or DateFormat make that clear. The documentation states that the class can be used to format a date object and parse a string representing a date. I am not sure if date validation is part of the class contract or if it is an implementation detail, subject to change in subsequent releases.

Is it ok to use SimpleDateFormat to validate if a date string represents a valid date? Or am I better using Calendar to verify if a date is valid?

I'm using JDK 6

Edited by: DMD_ANFINI on May 24, 2010 6:53 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 22 2010
Added on May 24 2010
11 comments
1,402 views