Link to home
Start Free TrialLog in
Avatar of John4343
John4343

asked on

Convert string to date independant of locale in Java

This code works with locale US but gives java.text.ParseException: Unparseable date in French and other locales.

String sDate= "Nov 12, 2007";
DateFormat df = new SimpleDateFormat("MMM d, yyyy");
Date dDate = df.parse(sDate);
System.out.println (dDate);

How can the string be converted to a date regardless of locale?

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Create your DateFormat using the appropriate Locale
Avatar of John4343
John4343

ASKER

If you mean this I get same error:

Locale locale = Locale.getDefault();
String sDate= "Nov 12, 2007";
DateFormat df = new SimpleDateFormat("MMM d, yyyy", locale);
Date dDate = df.parse(sDate);

Note that sDate will always be in this format.

ASKER CERTIFIED SOLUTION
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
John4343, i thought you wanted to parse it in French and other languages?:

>>but gives java.text.ParseException: Unparseable date in French and other locales.