user1898230
user1898230

Reputation: 75

Java Regex for date time

Sep 25, 2013 4:51:00 PM

Looking to create a regex for the date time format as above I have the following regex for the time

([1-9]|1[0-2]|):([0-5][0-9]:[0-5][0-9]) (AM|am|PM|pm) 

but can't seem to figure out the date, any help would be appreciated!!

Upvotes: 0

Views: 2698

Answers (3)

([0-9]{4}[-][0-9]{2}[-][0-9]{2}[-][0-9]{2}[-][0-9]{2}) 1980-02-02-05-00

Upvotes: 0

Silviu Burcea
Silviu Burcea

Reputation: 5348

What are you trying to achieve? I can only guess that you want to get some components from a date, so SimpleDateFormat is probably what you actually want. It is recommended to use it instead of Regular Expressions because it can also deal with locales, something that you can't do with RegExp.

Edit:

You should have an OR regexp for months like (Jan|Feb| ... |Dec), the rest is simple, match the space \s*, match the day [0-9]{1,2}, match a ,, match again some space \s*, match the year [0-9]{4}, yet another space \s* and the rest of your RegExp.

Upvotes: 1

Reimeus
Reimeus

Reputation: 159754

Why not just use

String text = "Sep 25, 2013 4:51:00 PM";
Date date = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a").parse(text);

Upvotes: 7

Related Questions