[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1155
  • Last Modified:

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?

0
John4343
Asked:
John4343
  • 3
1 Solution
 
CEHJCommented:
Create your DateFormat using the appropriate Locale
0
 
John4343Author Commented:
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.

0
 
Bart CremersJava ArchitectCommented:
If the date will always be that format (US format), pass the US locale to SimpleDateFormat:

DateFormat df = new SimpleDateFormat("MMM d, yyyy", Locale.US);
0
 
CEHJCommented:
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.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now