hao hao - 1 year ago 136
Java Question

Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string

I checked the

javadoc, but I am not able to find a way to parse the ordinal indicator in a date format like this:

Feb 13th 2015 9:00AM

I tried
"MMM dd yyyy hh:mma"
, but the days have to be in number for it to be correct?

Is it possible to parse the "13th" date using a
without having to truncate the string?

Answer Source

Java's SimpleDateFormat doesn't support an ordinal suffix, but the ordinal suffix is just eye candy - it is redundant and can easily be removed to allow a straightforward parse:

Date date = new SimpleDateFormat("MMM dd yyyy hh:mma")
    .parse(str.replaceAll("(?<=\\d)(st|nd|rd|th)", ""));

The replace regex is so simple because those sequences won't appear anywhere else in a valid date.

To handle any language that appends any length of ordinal indicator characters from any language as a suffix:

Date date = new SimpleDateFormat("MMM dd yyyy hh:mma")
    .parse(str.replaceAll("(?<=\\d)(?=\\D* \\d+ )\\p{L}+", ""));

Some languages, eg Mandarin, prepend their ordinal indicator, but that could be handled too using an alternation - left as an exercise for the reader :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download