1
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?

2
  • please show the code where you are using the DateFormat Commented Jun 9, 2015 at 1:27
  • Okay Mike, that might be a problem, do you have any idea how can I input the AM/PM string with SimpleDateFormat? Commented Jun 9, 2015 at 9:57

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I know that, but my idea is to be able to display both formats depending on the Android system language.
1

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.

Comments

0

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()));
}

1 Comment

dataHora.setHora(timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute()); txtDataHora.setText(dataHora.toString()); picker.cancel(); I try to parse this String, in one format works in another it wont.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.