Automatic Date/Time parser without specifying format

saravanan07 picture saravanan07 · Feb 21, 2013 · Viewed 13k times · Source

I am searching for a java library that can parse a string into a POJO without specifying the format. I have researched POjava. Is there anyother library which does similar thing?

DateTime dateTime = DateTimeParser.parse("21/02/13");

//If unclear use the cultural information passed
DateTime dateTime = DateTimeParser.parse("01/02/13", new Locale("en-us"));

//Should also work with time zones
DateTime dateTime = DateTimeParser.parse("2011/12/13T14:15:16+01:00");

I found the following links with the same problem Intelligent date / time parser for Java, but not very useful answers. Neither Joda or JChronic does what I wanted. Correct me if I am wrong.

Update:

The reason I say Joda does not solve my purpose is, Joda expects the string to be parsed in ISO8601 format or any format you specify like "yyyyMMdd". I will not be able to hardcode this format as I need to handle several formats.

I have a hack around solution for eliminating the ambiguity with respect to American or European date formats, i.e. mm/dd/yy or dd/mm/yy. Assuming I have access to timezone of the date, can I determine if it is American or European format? Can someone tell me way to do this? Googled but found nothing.

Answer

Has QUIT--Anony-Mousse picture Has QUIT--Anony-Mousse · Feb 21, 2013

The problem is that there are some formats that cannot be guessed right.

A simple example is 01/02/2013. Is this february 1st or january 2nd? Or even worse: 01/02/09?

Both formats exist. (Thank you, UK and US!)

So any format guesser will have to rely on luck for these formats, or fail deliberately for these.

The python module dateutil.parser can serve as an example of a best effort parser. Sorry that I don't know a java equivalent. But you might want to look at Joda Time

http://labix.org/python-dateutil#head-b95ce2094d189a89f80f5ae52a05b4ab7b41af47

it actually has parameters dayfirst and yearfirst.

Then there is a perl module:

https://metacpan.org/pod/Time::ParseDate

You might be able to use the precedence list from that module. It's not very fast to blindly try a number of patterns (an optimized lexer will be way faster), but it may be good enough for you, unless you are guessing the format of millions of records.