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
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.
To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly): SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy"); Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.
Instantiate the SimpleDateFormat class by passing the desired (new) format as string to its constructor. Invoke the format() method by passing the above obtained Date object as parameter.
The parse() Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.
We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes.
Check Joda Time, and its Freaky Formatters.
Java 8 includes JSR-310 so that could be a solution as well.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With