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

hao picture hao · Feb 14, 2015 · Viewed 13.7k times · Source

I checked the SimpleDateFormat 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 SimpleDateFormat without having to truncate the string?

Answer

Bohemian picture Bohemian · Feb 14, 2015

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 :)