File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Validating date using regex and simple date format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validating date using regex and simple date format" Watch "Validating date using regex and simple date format" New topic
Author

Validating date using regex and simple date format

Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
Hi,

How can I check if date for Feb month is valid?
I tried the below program but for input 02/31/2100, I get the valid result.

I have to verify using regex and Simple date format only, if my date is valid or not.

Please help.

Thanks
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Why dont you just try to parse the date? If it throws an exception on parsing then you know its invalid... or even better, make the user enter the date with a date time picker in case its coming from an UI.


Time is what we want the most, but what we use the worst. -- William Penn
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

I dont think SimpleDateFormat will here. Because SimpleDateFormat will parse this string successfully (after removing spaces), and gives a date of March 2nd or 3rd.

My suggestion is to split the string and validate using a simple algorithm. Having a complex regex will make code unreadable.


- Chinna
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Chinna Eranna wrote:I dont think SimpleDateFormat will here. Because SimpleDateFormat will parse this string successfully (after removing spaces), and gives a date of March 2nd or 3rd.


SimpleDateFormat extends java.text.DateFormat which has a method setLenient( boolean) which by default is 'true'. Set to 'false' and then it will throw the exception.

I'm a great fan of regex but this is not the right place to use it. Especially since the OP's regex allows '-' as a field separator but the SimpleDateFormat pattern does not! I would not even use a regex to convet the '-' separators to '/'; I would just use String.replace() .


My suggestion is to split the string and validate using a simple algorithm. Having a complex regex will make code unreadable.


If one has to check the syntax before parsing then I would use a regex and I would comment the regex so that it is not unreadable.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

James, Thanks. I was not aware of setLenient()
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Not everyone is aware of the fact that you can parse dates with DateFormat (and numbers with NumberFormat) without getting exceptions, using the "other" parse method:
This is similar to how parse(String) is implemented:
pos.index (pos.getIndex() from other packages) returning 0, the result Date being null or pos.getErrorIndex() returning a value other than -1 all indicate error.

I still don't like one thing about this solution: if the String has a valid date followed by some other garbage (e.g. "02/31/2100abc") the date will be parsed successfully, and success is shown. I usually make it a bit more strict:
If the ParsePosition's index is not equal to the String length there is some garbage after the valid date; only if these two are equal is the entire String used for the valid Date.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating date using regex and simple date format
 
Similar Threads
Validating The Date String
Needed help in composing an regular expression
Code review required for converting dateformat
Parsing date using SimpleDateFormat for hh mm ss
IP address regular expression