Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating date using regex and simple date format

 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, Thanks. I was not aware of setLenient()
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic