user200797
user200797

Reputation: 21

date parsing in java throws exception

Why is the following code throws ParseException?

DateFormat df = new SimpleDateFormat("MMM d, yyyy");
String date = "Jan 1, 2011"; 
df.parse(date);

Result:

java.text.ParseException: Unparseable date: "Jan 1, 2011"

Upvotes: 2

Views: 1490

Answers (1)

Juvanis
Juvanis

Reputation: 25950

It's related with the Locale parameter as @Gijs Overvliet mentioned. For instance, I'm using Turkish locale and my String should be set accordingly.

public static void main(String[] args) throws ParseException
{
   DateFormat df = new SimpleDateFormat("MMM d, yyyy", Locale.getDefault());
   String date = "Oca 1, 2011"; 
   df.parse(date);
}

Upvotes: 2

Related Questions