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

1

6

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?

Jalap answered 13/8, 2017 at 14:4 Comment(1)
I have find the error if i used J7 samsung don't work the first code and if i want to work must to set the this line timeComeFromServer = timeComeFromServer.replaceAll("PM", "p.m.").replaceAll("AM", "a.m."); but other devices work without any replayeJalap
P
1

SimpleDateFormat uses the system's default locale (which you can check using the java.util.Locale class, calling Locale.getDefault()). This locale is device/environment specific, so you have no control over it and can have different results in each device.

And some locales might have a different format for AM/PM field. Example:

Date d = new Date();
System.out.println(new SimpleDateFormat("a", new Locale("es", "US")).format(d));
System.out.println(new SimpleDateFormat("a", Locale.ENGLISH).format(d));

The output is:

p.m.
PM

To not depend on that, you can use Locale.ENGLISH in your formatters, so you won't depend on the system/device's default configuration:

String timeComeFromServer = "3:30 PM";
// use English Locale
SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm");
System.out.println(date24Format.format(date12Format.parse(timeComeFromServer)));

The output is:

15:30

The second formatter doesn't need a specific locale as it's not dealing with locale specific information.


Java new Date/Time API

The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.

One detail is that SimpleDateFormat always works with Date objects, which has the full timestamp (the number of milliseconds since 1970-01-01T00:00Z), and both classes implicity use the system default timezone behind the scenes, which can mislead you and generate unexpected and hard to debug results. But in this specific case, you need only the time fields (hour and minutes) and there's no need to work with timestamp values. The new API has specific classes for each case, much better and less error prone.

In Android you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need ThreeTenABP (more on how to use it here).

You can use a org.threeten.bp.format.DateTimeFormatter and parse the input to a org.threeten.bp.LocalTime:

String timeComeFromServer = "3:30 PM";

DateTimeFormatter parser = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");

LocalTime time = LocalTime.parse(timeComeFromServer, parser);
System.out.println(time.format(formatter));

The output is:

15:30

For this specific case, you could also use time.toString() to get the same result. You can refer to javadoc for more info about the backport API.

Pudendas answered 13/8, 2017 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.