0

I have date formats like this which I want to parse --

02 September 2011 15:57

Friday, September 02, 2011 9:51 AM

Mittwoch, 10. August 2011 16:31

woensdag 10 augustus 2011 14:09

Wed, 27 Jul 2011 20:23:18 -0000 (UTC)

Can we have any parser which can do this also considering the language difference?

Regards Neha

Neeha
  • 27
  • 1
  • 6
  • Where do those values originate? From an enduser-provided input? Why can't you just ask the enduser for a fixed input format and validate it based on the enduser's own locale? – BalusC Oct 12 '11 at 15:18
  • these are part of some file which is there for parsing and we need to get the date out – Neeha Oct 12 '11 at 15:21
  • What kind of file is it? Has it a fixed (officially specified) format? If so, then just read the specs for guidelines. If not, you'd need to work on that first. Otherwise your best bet is to collect all possible formats and locales yourself and try parsing in a loop until no `ParseException` is been thrown. – BalusC Oct 12 '11 at 15:23
  • Similar question: http://stackoverflow.com/questions/3389348/parse-any-date-in-java – pkalinow Mar 15 '16 at 15:01

1 Answers1

4

Defining a universal date parser is not possible due to ambiguities in date formats. For example:

12/11/2011

... implies November 12th 2011 using the UK date convention but December 11th 2011 using the US convention.

You could take an approach where you define a List of SimpleDateFormat classes, each specifying a date pattern and attempt to parse the dates in that order. However, I wouldn't personally recommend it as it will result in unintuitive behaviour for any end users (assuming there are any) and / or difficult-to-track-down data related bugs. Far better to either define an explicit date format or rely on the system locale settings (depending on your problem domain).

Adamski
  • 54,009
  • 15
  • 113
  • 152