1

i've been searching StackOverflow's posts about this specific date type, also i've checked on google's search engine to verify the name of this date type. So far what i've learned it's somekind of Google's date type format and i do not know the name of it (eg. GMT ..).

The date, actually "timer" i extract from JSON object and display it in the textView, and it is String type :

duration:"PT3M33S" 

Browsing the net i've seen you have a date format and simpledate format but when i try to "translate" the format the PT doesnt stand for anything as the AM PM stand and it is translated to symbol "a" and like hours(HH), minutes (MM) ...

If someone can explain me what type of dateformat is this and how can i force it to display HH:mm:ss aswell in string so i can display to terxtView?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Luka Čagalj
  • 31
  • 1
  • 6
  • 2
    That's a standard [ISO-8601 format for representing duration](https://en.wikipedia.org/wiki/ISO_8601#Durations). – zwer Dec 08 '17 at 12:31
  • I believe the best thing you can do is to get [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), the backport of JSR-310, the modern Java date and time API also known as `java.time`, and use its `Duration` class. `Duration.parse()` will parse your string. Unfortunately `Duration` objects don’t lend themselves very well to formatting, but there are a number of questions already on how to do, please search. – Ole V.V. Dec 08 '17 at 13:04
  • `PT3M33S` means a duration with no date part and a time part (T) of 3 minutes 33 seconds. You may think of P as standing for period. – Ole V.V. Dec 08 '17 at 13:06

1 Answers1

2

You can use java.time.Duration which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenience methods were introduced.

If you have gone through the above links, you might have already noticed that PT3M33S specifies a duration of 3 minutes 33 seconds that you can parse to a Duration object and out of this object, you can create a string formatted as per your requirement by getting days, hours, minutes, seconds from it.

Demo:

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        String strIso8601Duration = "PT3M33S";

        Duration duration = Duration.parse(strIso8601Duration);
        // Default format
        System.out.println(duration);

        // Custom format
        // ####################################Java-8####################################
        String formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHours() % 24,
                duration.toMinutes() % 60, duration.toSeconds() % 60);
        System.out.println(formattedElapsedTime);
        // ##############################################################################

        // ####################################Java-9####################################
        formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHoursPart(), duration.toMinutesPart(),
                duration.toSecondsPart());
        System.out.println(formattedElapsedTime);
        // ##############################################################################
    }
}

Output:

PT3M33S
00:03:33
00:03:33

Learn about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110