Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy/java regex check if "yyyy.MM.dd" format

I have a string representing a date, for example "2010.12.25". How can I control if it is of "yyyy.MM.dd" format? There is no need to check the validness of the date.

like image 768
Bob Avatar asked Nov 22 '13 10:11

Bob


2 Answers

You have the Regex, in Groovy, you can just do:

boolean match = "2010.12.12" ==~ /\d{4}\.\d{2}\.\d{2}/
like image 117
tim_yates Avatar answered Nov 12 '22 00:11

tim_yates


use SimpleDateFormat to parse() the string, handling the exception to decide if it is a valid date string. don't use regex to check a date. e.g.:

2010.30.40
2010.13.34
like image 20
Kent Avatar answered Nov 12 '22 00:11

Kent