문제

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.

도움이 되었습니까?

해결책

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
    }
}

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top