tags:

views:

2465

answers:

9

Does anyone know a good date parser for different languages/locales. The built-in parser of Java (SimpleDateFormat) is very strict. It should complete missing parts with the current date.

For example

  • if I do not enter the year (only day and month) then the current year should be used.
  • if the year is 08 then it should not parse 0008 because the current year pattern has 4 digits.

Edit: I want to parse the input from a user. For example if the locale date format of the user is "dd.mm.yyyy" and the user type only "12.11." then the parser should accept this as a valid date with the value "12.11.2008". The target is a good usability.

+14  A: 

Check Joda Time, and its Freaky Formatters. http://joda-time.sourceforge.net/userguide.html#Input_and_Output

Petteri Hietavirta
I love Joda Time. It's so good, it'll be the basis of a rewrite of Date Time handling in Java 7
Vinko Vrsalovic
So, how would the code to answer this question would look like using Joda Time?
Eduardo
+7  A: 

(Edited for clarity.)

Personally, I think strict is good. So many different situations call for different rules around relaxed parsing, it's impossible to really put that into a common library comprehensively.

However, I would thoroughly recommend Joda Time instead of the built-in date/time classes in general. Their formatters and parsers are thread-safe and immutable, which helps too. Joda Time has some support for relaxed parsing, as shown in the other answer, but you should expect to have to provide some of the rules yourself.

Jon Skeet
+1  A: 

Use DateFormat ... Current standard until the welcome respite of Joda.

Ichorus
+1  A: 

Calendar is usually the way to go, but understand that most Java Date management will be handled on your part if you want it done properly. Timezone is a good thing to look into if you have to manage international/cross-zone info. Joda Time is also a neat thing and is the inspiration behind the new suggested Date/Time concepts to be added to Java in JSR 310.

Hope this is helpful.

codeLes
FYI, JSR 310 is *not* JodaTime although it is inspired by it and the spec lead is the author of JT.
Alex Miller
thanks for the comment... my mistake, will correct my answer. Thanks.
codeLes
A: 

I would have to say +1 for JodaTime. In addition to parsing, Joda makes just about every date-related operation better.

Joey Gibson
+4  A: 

From 43642, although not necessarily a duplicate:

See Apache Commons' DateUtils. There's a parseDate method that takes your String and multiple patterns to try and spits out a Date instance.

Joe Liversedge
+1 for apache Commons! Those libraries are so often overlooked or underappreciated and many programmers really don't know what they include.
codeLes
Apache Commons in general are great. But Joda Time is marvelous! :)
Vinko Vrsalovic
+2  A: 

I would say JChronic if you're looking for something that will parse dates from natural "fuzzy" human input.

But I see that they've got a note on the site saying that the project is going to be deleted in 30 days due to inactivity :(

I've used both JChronic and Chronic (the original Ruby version) with great success.

madlep
I have emailed the author to ask if he wants me to host it on my website
kolrie
+1  A: 

The POJava project on SourceForge has a DateTime object that parses dates from multiple languages (when month is specified as a name) and is configurable between MM-DD-YYYY and DD-MM-YYYY. It parses dates heuristically, picking out the most likely year, month, date, hour, minute, second, and time zone rather than supporting predefined formats. The jar file is about 60K in size.

There is ambiguity in interpretation of a date like "10-08" in that it could be intended as shorthand for either "2008-10-08" or "Oct 2008". You could append the year yourself if you are accepting the sort of shorthand you give in your example.

Proj: POJava Docs: HOWTO use DateTime

+1  A: 

I tried to implement an extensible PHP's strtotime in Java in this answer

dfa