Question

I'm having the following data:

"startAt": "PT0S",
"endAt": "PT21M12.667S"

startAt defines the start of a video and endAt the end of the video. How can I calculate the time between these points? I guess its ISO 8601 and I am using Java, but the library I tried (Joda) doens't work with the endAt paramter.

Was it helpful?

Solution

These are ISO-8601 period values (not points in time) - and Joda Time handles them fine:

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
    public static void main(String[] args) {
        String startText = "PT0S";
        String endText = "PT21M12.667S";

        PeriodFormatter format = ISOPeriodFormat.standard();
        Period start = format.parsePeriod(startText);
        Period end = format.parsePeriod(endText);

        Duration duration = end.minus(start).toStandardDuration();
        System.out.println(duration); // PT1272.667S
        System.out.println(duration.getMillis()); // 1272667
    }
}

OTHER TIPS

Using Java-8 standard library

java.time.Duration is modelled on ISO-8601 standards and was introduced as part of JSR-310 implementation.

Demo:

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        String startAt = "PT0S";
        String endAt = "PT21M12.667S";
        Duration start = Duration.parse(startAt);
        Duration end = Duration.parse(endAt);
        long millis = end.minus(start).toMillis();
        System.out.println(millis);
    }
}

Output:

1272667

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top