Validating a date in java

Rated 4.92/5 based on 582 customer reviews

“31/9/2010”, “31/09/2010” – September has 30 days only 10.

“31/6/2010”, “31/06/2010” -June has 30 days only 9.

“31/4/20100”, “31/04/2010” – April has 30 days only 8.

“333/2/2008”, “29/02/200a” – day is invalid, year is invalid 7.

This is a Maven-based project, so it should be easy to import and run as it is.

and if the date was before the year 1582 it leaves the date as it was entered: mm/dd/yyyy i know that i need to use a switch statement. im not sure if i have the variables set right or if i even need the variables like that.

Now that we successfully matched our general date format, we need to constrain that further – to make sure the dates are actually correct: In order to match leap years correctly we must first identify when we have encountered a leap year, and then make sure that we accept February 29th as a valid date for those years.

Another example is to validate if user correctly input letters to guest a secret word. “29/2/2007”, “29/02/2007” – 2007 is not leap year, only has 28 days 4. “1/13/2010”, “01/01/1820” – month is out of range [1-12], year is out of range [1900-2999] 3. [1-9] # 01-09 or 1-9 | # .[12][0-9] # 10-19 or 20-29 | # .3[01] # 30, 31 ) #end of group #1 / # follow by a "/" ( # start of group #2 0? [1-9] # 01-09 or 1-9 | # .1[012] # 10,11,12 ) # end of group #2 / # follow by a "/" ( # start of group #3 (19|20)\d\d # 19[0-9][0-9] or 20[0-9][0-9] ) # end of group #3 The above regular expression is used to validate the date format in “dd/mm/yyyy”, you can easy customize to suit your need.

Leave a Reply