1

is there a smart date parser in Java that would parse a variety of date formats without knowning ahead what the format is? for example I can have the following formats:

21-3-1998,
March 2004, 2001, 
3/4/97
12-Dec-1998

is there a simple parse call(third party lib is ok) I can use to handle them call?

user767034
  • 15
  • 1
  • 5
  • In my experience, whenever we've tried to enumerate all reasonable date formats, we soon discovered that there was yet another format someone was using that we hadn't considered. – Michael McGowan May 24 '11 at 21:20
  • 3
    When you see date 3/4/2011, do you expect your smart parser to parse it as March 4, 2011 or April 3, 2011? – Olaf May 24 '11 at 21:27
  • This is the reason not to use this stupid `xx/xx/yyyy` format. The ISO 8601 formats were invented for a reason. (Does not help for someone who has to parse this, though.) – Paŭlo Ebermann May 24 '11 at 21:46
  • So what really is `March 2004, 2001,`? – nawfal Jan 30 '14 at 11:51
  • possible duplicate of [Parse any date in Java](http://stackoverflow.com/questions/3389348/parse-any-date-in-java) – nawfal Jan 30 '14 at 11:53

3 Answers3

1

I don't know if there is a good out-of-the-box choice, given almost unlimited number of permutations; but if I had to do it, I'd start with de-facto Java date package, Joda, and just do linear lookup with sequence of allowed data formats, and accept first one that parses succsefully.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
0

See:

Is there a good date parser for Java?

for some suggestions. (Apache Commons and Joda Time)

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
-2

Have you tried the standard Java solution of SimpleDateFormat.parse(String, ParsePosition)?

As per the documentation:

Parses text from a string to produce a Date. The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed date is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

Pål Brattberg
  • 4,568
  • 29
  • 40