Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 498
hao Avatar asked Feb 14 '15 09:02

hao


People also ask

How do you parse a string to a date?

Date.parse() The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

How do you use ST nd rd th?

When writing ordinal numbers such as 1st, 2nd, 3rd, etc. you should use the last two letters on the word as it would be if you wrote out the whole word. Below are the ordinal numbers both written out and with digits for 1-20. As you can see, 1st, 2nd, and 3rd use -st, -nd, and -rd, but 4th-20th use -th.

What is ordinal date format?

This format of date is a combination of year plus a relative day number within the year, which is more correctly called an ordinal date. A typical example is 2013-348 in the format YYYYDDD. This is equivalent to a calendar date of December 14 th 2013.


1 Answers

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

like image 131
Bohemian Avatar answered Sep 16 '22 15:09

Bohemian