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?
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?
Create your DateFormat using the appropriate Locale
http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String,%20java.util.Locale)
Pass the locale in the 2nd parm
Pass the locale in the 2nd parm
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
>>but gives java.text.ParseException: Unparseable date in French and other locales.