How to convert Youtube API V3 duration in Java

前端 未结 15 2004
梦如初夏
梦如初夏 2021-02-07 03:39

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

15条回答
  •  感情败类
    2021-02-07 04:04

    You can use the standart SimpleDateFormat to parse the String to a Date and process it from there:

    DateFormat df = new SimpleDateFormat("'PT'mm'M'ss'S'");
    String youtubeDuration = "PT1M13S";
    Date d = df.parse(youtubeDuration);
    Calendar c = new GregorianCalendar();
    c.setTime(d);
    c.setTimeZone(TimeZone.getDefault());
    System.out.println(c.get(Calendar.MINUTE));
    System.out.println(c.get(Calendar.SECOND));
    

提交回复
热议问题