How to convert Youtube API V3 duration in Java

前端 未结 15 2008
梦如初夏
梦如初夏 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:10

    Solution in Java 8:

    Duration.parse(duration).getSeconds()
    
    0 讨论(0)
  • 2021-02-07 04:12
    public String pretty(String duration) {
      String time = duration.replace("PT", "");
      String hour = null;
      String minute = null;
      String second = null;
    
      if (time.indexOf("H") > 0) {
        String[] split = time.split("H");
        if (split.length > 0) {
          hour = split[0];
        }
        if (split.length > 1) {
          time = split[1];
        }
      }
    
      if (time.indexOf("M") > 0) {
        String[] split = time.split("M");
        if (split.length > 0) {
          minute = split[0];
        }
        if (split.length > 1) {
          time = split[1];
        }
      }
    
      if (time.indexOf("S") > 0) {
        String[] split = time.split("S");
        if (split.length > 0) {
          second = split[0];
        }
      }
    
      if (TextUtils.isEmpty(hour)) {
        if (TextUtils.isEmpty(minute)) { return "0:" + pad(second, 2, '0'); }
        else { return minute + ":" + pad(second, 2, '0'); }
      }
      else {
        if (TextUtils.isEmpty(minute)) { return hour + ":00:" + pad(second, 2, '0'); }
        else {return hour + ":" + pad(minute, 2, '0') + ":" + pad(second, 2, '0');}
      }
    }
    
    private String pad(String word, int length, char ch) {
      if (TextUtils.isEmpty(word)) { word = ""; }
      return length > word.length() ? pad(ch + word, length, ch) : word;
    }
    
    0 讨论(0)
  • 2021-02-07 04:13

    I've implemented this method and it has worked so far.

    private String timeHumanReadable (String youtubeTimeFormat) {
    // Gets a PThhHmmMssS time and returns a hh:mm:ss time
    
        String
                temp = "",
                hour = "",
                minute = "",
                second = "",
                returnString;
    
        // Starts in position 2 to ignore P and T characters
        for (int i = 2; i < youtubeTimeFormat.length(); ++ i)
        {
            // Put current char in c
            char c = youtubeTimeFormat.charAt(i);
    
            // Put number in temp
            if (c >= '0' && c <= '9')
                temp = temp + c;
            else
            {
                // Test char after number
                switch (c)
                {
                    case 'H' : // Deal with hours
                        // Puts a zero in the left if only one digit is found
                        if (temp.length() == 1) temp = "0" + temp;
    
                        // This is hours
                        hour = temp;
    
                        break;
    
                    case 'M' : // Deal with minutes
                        // Puts a zero in the left if only one digit is found
                        if (temp.length() == 1) temp = "0" + temp;
    
                        // This is minutes
                        minute = temp;
    
                        break;
    
                    case  'S': // Deal with seconds
                        // Puts a zero in the left if only one digit is found
                        if (temp.length() == 1) temp = "0" + temp;
    
                        // This is seconds
                        second = temp;
    
                        break;
    
                } // switch (c)
    
                // Restarts temp for the eventual next number
                temp = "";
    
            } // else
    
        } // for
    
        if (hour == "" && minute == "") // Only seconds
            returnString = second;
        else {
            if (hour == "") // Minutes and seconds
                returnString = minute + ":" + second;
            else // Hours, minutes and seconds
                returnString = hour + ":" + minute + ":" + second;
        }
    
        // Returns a string in hh:mm:ss format
        return returnString; 
    
    }
    
    0 讨论(0)
提交回复
热议问题