Link to home
Start Free TrialLog in
Avatar of panshul007
panshul007

asked on

Regex Date and Time Validation

Hello,
Please help me with Regex for the following date formats in java:

1. MM/dd/yyyy

2. yyyy/MM/dd

it should validate the date considering all the things: like months of 30 and 31 days and 28 days and 29 days, month fld should not be > 12 and so on.
Time Formats:

1.  hh:mm  12 hour format.

Thanks
Avatar of ksivananth
ksivananth
Flag of United States of America image

it would be difficult using regex, better you capture the date string using regex and validate using SimpleDateFormat, check the example, http://www.dreamincode.net/forums/showtopic14886.htm
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of panshul007
panshul007

ASKER

Please give me Regex for time format atleast...
MM\dd\yyyy - "\\d{2}\\\\\\d{2}\\\\\\d{4}"
For time format
^(([0]?[0-5][0-9]|[0-9]):([0-5][0-9]))$

If you explore the link that i had given earlier, many examples are given, which may interest you

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for time this regex worked: (1[012]|0[1-9]|[1-9]):[0-5][0-9]

for date I used the date formater util and put it in a try catch.... if it was unsuccessful in converting i.e not a valid date format... so generated error in catch....

actually i am validating my web service.

the expert comment by object was the most helpful...
it gave me the actual idea for validating the date...

actually i am validating my web service...

and gurwinder thanks for the date formatter idea..

Although i had to modify everything to get the actual date validation that included the 30 and 31 days check and leap year check...

and time validation i had to make myself.