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?
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" like9999-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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With