Automatic Date/Time parser without specifying format
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.
I found the answer to my problem. I used this particular library POjava. This page explains how you can format the date+time string without specifying any format. However, for the library to work properly, you got to specify the date ordering like Day followed by Month or Month followed by Day.