Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for SimpleDateFormat string [closed]

I have dates mainly in the forms of yyyy-MM-dd and yyyy-MM-dd hh:mm:ss. I want to pattern match yyyy-MM-dd hh:mm:ss.

Is it trivial to write an regex for this purpose? I am new to regex so would appreciate any resource tat could get me up to speed?

like image 955
yoda Avatar asked Dec 20 '22 20:12

yoda


1 Answers

If you are trying to find these date notations in a String, then regexes is indeed a good choice. You could use this regex:

\b\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2}\b

See it in action here: http://rubular.com/r/qZOTsUikbo. Note: this matches "dates" like
9999-99-99 99:99:99 as well. If that is a problem for you, you could verify them after you found the potential Strings.

If you want to parse them, then use a SimpleDateFormat.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = sdf.parse("2013-12-24 18:31:20");
like image 119
Martijn Courteaux Avatar answered Jan 05 '23 20:01

Martijn Courteaux