[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regex Date and Time Validation

Posted on 2009-12-28
9
Medium Priority
?
826 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:panshul007
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 26136263
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
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 300 total points
ID: 26136273
You can find a lot of examples here
http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5

for 2), this might be the regular expression you are looking for
http://www.regular-expressions.info/dates.html

But i would suggest using Simple Date format to do this for you

String strDate =""
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

Date date = formatter .parse( strDate);



If there is any validation issue, it will throw exception by itself
0
 

Author Comment

by:panshul007
ID: 26136292
Please give me Regex for time format atleast...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:ksivananth
ID: 26136312
MM\dd\yyyy - "\\d{2}\\\\\\d{2}\\\\\\d{4}"
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26136332
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
0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 26136347
0
 

Author Comment

by:panshul007
ID: 26160695
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.

0
 

Author Closing Comment

by:panshul007
ID: 31670687
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question