1

I am having similar problems to Unable to parse DateTime-string with AM/PM marker

I even tried the solution provided in that link but it did not help.

SimpleDateFormat timingFormat = new SimpleDateFormat("h:mm a", Locale.US);
Date l = timingFormat.parse(time);

but I keep receiving java.text.ParseException: Unparseable date: "12:34". When i enter 12:34 AM

Community
  • 1
  • 1
Jazzy
  • 305
  • 5
  • 11
  • Have you tried parsing after [`timingFormat.setLenient(true);`](http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient%28boolean%29)? –  Dec 19 '11 at 00:28
  • 1
    I ran into the same issue over here. We tried a number of variations and just couldn't get it to parse the AM or PM text, so leave it off and just use the 24 hour clock. The "a" or "aa" should only be used to format a DateTime, not for parsing a string in. – Joshua Pinter Jun 03 '13 at 22:45

2 Answers2

2

Wait a second. The error says 12:34 is unparseable, not 12:34 AM. In that case, your input method is reading the first word up to whitespace and ignoring the "AM" part of your entry. Correct your input method so that it reads the entire input stream/string, and then it should parse correctly.

1
public static void main(String[] args) throws ParseException {
    SimpleDateFormat timingFormat = new SimpleDateFormat("h:mm a", Locale.US);
    Date l = timingFormat.parse("12:34 AM");
    System.out.println(l.toString());
}

The above code runs fine. This means the input you've passed to the parse method is not what you expected.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96