-2

I want to validate that calendar object should be as 2014-05-05T12:12:30.How to validate a this using regular expression

Alan Moore
  • 71,299
  • 12
  • 93
  • 154
Debesh Kuanr
  • 35
  • 1
  • 1
  • take a look at this thread, it is JS but very simil arhttp://stackoverflow.com/a/12756279/2362664 – Khinsu Jul 24 '14 at 15:35
  • Depending on what you really want to achieve you can use the built-in functionality of the Joda datetime library. Check out the function [ISODateTimeFormat.dateTimeParser()](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTimeParser()). – MicSim Jul 24 '14 at 15:45

2 Answers2

3

The regex in Adam Yost's answer is close, but missing a closing bracket before the T... don't have enough rep to comment so here is the corrected version:

(19|20)[0-9][0-9]-(0[0-9]|1[0-2])-(0[1-9]|([12][0-9]|3[01]))T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]
Gus
  • 3,474
  • 1
  • 32
  • 38
sam
  • 71
  • 6
0

This regex will only match dates in that format with a few restrictions:

(19|20)[0-9][0-9]-(0[0-9]|1[0-2])-(0[1-9]|([12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]

This matches years 1900-2099, 12 months, up to 31 days, 24 hr clock, up to 59 minutes, up to 59 seconds

It should be noted that if you wish to validate whether or not something is a truly valid date (ie not Feb 30) you will need either a far more complex regex, or some simple code to wrap around it.

Adam Yost
  • 3,534
  • 21
  • 36