How to convert Youtube API V3 duration in Java

2019-03-18 02:18发布

The Youtube V3 API uses ISO8601 time format to describe the duration of videos. Something likes "PT1M13S". And now I want to convert the string to the number of seconds (for example 73 in this case).

Is there any Java library can help me easily do the task under Java 6? Or I have to do the regex task by myself?

Edit

Finally I accept the answer from @Joachim Sauer

The sample code with Joda is as below.

PeriodFormatter formatter = ISOPeriodFormat.standard();
Period p = formatter.parsePeriod("PT1H1M13S");
Seconds s = p.toStandardSeconds();

System.out.println(s.getSeconds());

13条回答
贼婆χ
2楼-- · 2019-03-18 02:49

Solution in Java 8:

Duration.parse(duration).getSeconds()
查看更多
登录 后发表回答