Convert String in 12 (PM /AM)Hour AM PM Time to 24 Hour time android

Viewed 2139

I have a problem in convert time coming from server and I want to convert it to 24 hour. I'm using the following code:

String timeComeFromServer = "3:30 PM";

SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm a");

SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm");
try {
    ((TextView)findViewById(R.id.ahmad)).setText(date24Format.format(date12Format.parse(timeComeFromServer)));
} catch (ParseException e) {
    e.printStackTrace();
}

There is the error:

Method threw 'java.text.ParseException' exception.)

Detailed error message is:

Unparseable date: "3:30 PM" (at offset 5)

But if I replace PM to p.m. it works without any problem like this:

 timeComeFromServer = timeComeFromServer.replaceAll("PM", "p.m.").replaceAll("AM", "a.m.");

Can any one tell me which is the correct way?

1 Answers
Related