Vinit89
Vinit89

Reputation: 583

Facing issue in DateFormat parsing

With the below code snippet I am getting exception in Java SE 1.7 environment.Can anybody help me figure out problem with this code.

DateFormat df = new SimpleDateFormat("hh:mm a");
Date date = df.parse("10:00 PM");

Exception Details :

Exception in thread "main" java.text.ParseException: Unparseable date: "10:00 PM"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.premute.ModuleTest.main(ModuleTest.java:74)

Upvotes: 1

Views: 93

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280181

You seem to have a default Locale which cannot parse the English PM. Use the overloaded SimpleDateFormat constructor which accepts a Locale and provide it with an appropriate instance.

DateFormat df = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);

Upvotes: 7

Related Questions