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);
Do it as follows:
With Java-8:
Output:
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:
Output:
Note that
Duration#toHoursPart
andDuration#toMinutesPart
were introduced with Java-9.