0

I am trying to parse some time strings like this:

String time_string = "10:00 AM";
SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
Date date = null;
try {
    date = format.parse(time_string);
    // Do something with 'date'
} catch (ParseException e) {
    Log.w("Time", e.toString());
}

But the parser is failing with the exception:

java.text.ParseException: Unparseable date: "10:00 AM"

What I am doing wrong?

Guilherme
  • 7,839
  • 9
  • 56
  • 99

1 Answers1

2

The AM/PM marker may not match that of your default Locale Try

SimpleDateFormat format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • That it. Thanks (have to wait 10 min to mark as correct). Just for curiosity, what other markers exist besides AM/PM? I've never seen any translation of them. – Guilherme Mar 24 '14 at 14:04
  • The full list of `DateFormat` pattern component can be found in the [javadoc](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). For fun looking up current `Locale` date format component values take a look at [DateFormatSymbols](http://docs.oracle.com/javase/7/docs/api/java/text/DateFormatSymbols.html) – Reimeus Mar 24 '14 at 14:29