Diogo Rosa
Diogo Rosa

Reputation: 178

SimpleDateFormat parse exception for Time

DateFormat FORMAT_TIME_AM = new SimpleDateFormat("hh:mm");//works

If I use this code, everything works fine, but when I insert an "a" to display the AM/PM in the end, a parsing exception is raised.

DateFormat FORMAT_TIME_AM = new SimpleDateFormat("hh:mm a");//does not works

Here is the error:

com.finalagenda W/System.err java.text.ParseException: Unparseable date: "2:11" (at offset 4) com.finalagenda W/System.err﹕ at java.text.DateFormat.parse(DateFormat.java:579)

Any thoughts?

Upvotes: 1

Views: 245

Answers (3)

Diogo Rosa
Diogo Rosa

Reputation: 178

Okay guys I just fixed, just added "AM" to the end of my String time and it automatically changes to "PM" if necessary. Thanks for everything, hope this helps anyone else.

Upvotes: 1

Zaw Than oo
Zaw Than oo

Reputation: 9935

In SimpleDateFormat, HHand hh are different. HH from 0 to 23 for 24 hours format and hh from 1 to 12 (AM/PM) 12 hours format.

"HH:mm" -> 13:30 is "hh:mm a" -> 01:30 PM

Upvotes: 1

Nei Borges Filho
Nei Borges Filho

Reputation: 29

The code below works fine here. Could you please provide part of your code?

public static void main(String args[])
{
    DateFormat FORMAT_TIME_AM = new SimpleDateFormat("hh:mm a");
    System.out.println(FORMAT_TIME_AM.format(new Date()));
}

Upvotes: 0

Related Questions