Conversion from time Duration to time String

1k views Asked by At

I am getting time through JSON in this format "DocTime": "PT18H30M".

How to convert from this format to normal String time e.g. "6:30 pm" in android?

For time being I found this Solution:

    String  json = "PT18H30M";
    System.out.println("Server Time: " +json);
    int h = json.indexOf("H");
    int m = json.indexOf("M");
    int hrs = Integer.valueOf(json.substring(2 , h));
   // System.out.println("hrs: " + hrs);
    int min = Integer.valueOf(json.substring((h+1) , m));
   // System.out.println("min: " + min);
    String shrs = (hrs>12)? String.valueOf((hrs - 12)) : String.valueOf(hrs);
    String mode = (hrs>12)? "pm" : "am";
    
    String fTime = shrs+":"+min+" "+mode;
           
    System.out.println("Normal Time: " +fTime);
2

There are 2 answers

2
Arvind Kumar Avinash On BEST ANSWER

Do it as follows:

With Java-8:

import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        Duration duration = Duration.parse("PT18H30M");
        LocalTime time = LocalTime.of((int) duration.toHours(), (int) (duration.toMinutes() % 60));
        System.out.println(time.format(DateTimeFormatter.ofPattern("h:m a")));
    }
}

Output:

6:30 pm

If your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

With Java-9:

import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        Duration duration = Duration.parse("PT18H30M");
        LocalTime time = LocalTime.of(duration.toHoursPart(), duration.toMinutesPart());
        System.out.println(time.format(DateTimeFormatter.ofPattern("h:m a")));
    }
}

Output:

6:30 pm

Note that Duration#toHoursPart and Duration#toMinutesPart were introduced with Java-9.

2
Anonymous On

java.time either through desugaring or through ThreeTenABP

It seems that you have received a string where someone has misunderstood the ISO 8601 standard and given you a string representing a duration where they intended a time of day.

I recommened you use java.time, the modern Java date and time API, for your time work. The problem with the incorrect string can be solved by regarding it as a duration since 00:00.

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
    
    String docTimeString = "PT18H30M";
    
    Duration docTimeDur = Duration.parse(docTimeString);
    LocalTime docTimeOfDay = LocalTime.MIDNIGHT.plus(docTimeDur);
    String humanReadableDocTime = docTimeOfDay.format(formatter);
    
    System.out.println(humanReadableDocTime);

Output:

6:30 PM

I am exploiting the fact that Duration.parse() expects and parses ISO 8601 format.

"Call requires API level 26 (current min is 16): java.time.Duration#parse" showing this

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links