Convert strings to 12 hours (PM / AM) AM PM Time to 24 hours

I have a problem converting the time coming from the server and I want to convert it to 24 hours. I am 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(); } 

An error occurred:

The method threw the exception "java.text.ParseException".)

Detailed error message:

Unbeatable Date: 3:30 PM (Offset 5)

But if I replaced PM with pm , it will work without any problems:

  timeComeFromServer = timeComeFromServer.replaceAll("PM", "pm").replaceAll("AM", "am"); 

Can someone tell me this is the right way?

+5
source share
1 answer

SimpleDateFormat uses the default default locale (which you can check with the java.util.Locale class by calling Locale.getDefault() ). This language is device / environment specific, so you have no control over it and may have different results on each device.

And some locales may have a different format for the 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)); 

Output:

pm
PM

In order not to depend on this, you can use Locale.ENGLISH in your formats, so you will not depend on the default configuration for the system / device:

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

Output:

15:30

The second formatter does not need a specific locale, since it does not deal with specific information related to the language.


New Java Date / Time API

Old classes ( Date , Calendar and SimpleDateFormat ) have a lot of problems and design issues , and they are being replaced with new APIs.

One detail is that SimpleDateFormat always works with Date objects that have a full timestamp (the number of milliseconds since 1970-01-01T00:00Z ), and the implicitness of both classes uses the default system time zone behind the scenes , which can introduce you to fallacy and create unexpected and difficult to debug results. But in this particular case, you only need time fields (hour and minutes), and there is no need to work with timestamp values. The new API has specific classes for each case, much better and less error prone.

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

You can use org.threeten.bp.format.DateTimeFormatter and org.threeten.bp.format.DateTimeFormatter input on 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)); 

Output:

15:30

In this particular case, you can also use time.toString() to get the same result. You can refer to javadoc for more information on the backport API.

+1
source

Source: https://habr.com/ru/post/1270829/


All Articles