How to convert Youtube API V3 duration in Java

前端 未结 15 2034
梦如初夏
梦如初夏 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 03:47

    In case you can be pretty sure about the validity of the input and can't use regex, I use this code (returns in miliseconds):

    Integer parseYTDuration(char[] dStr) {
        Integer d = 0;
    
        for (int i = 0; i < dStr.length; i++) {
            if (Character.isDigit(dStr[i])) {
                String digitStr = "";
                digitStr += dStr[i];
                i++;
                while (Character.isDigit(dStr[i])) {
                    digitStr += dStr[i];
                    i++;
                }
    
                Integer digit = Integer.valueOf(digitStr);
    
                if (dStr[i] == 'H')
                    d += digit * 3600;
                else if (dStr[i] == 'M')
                    d += digit * 60;
                else
                    d += digit;
            }
        }
    
        return d * 1000;
    }
    
    0 讨论(0)
  • 2021-02-07 03:47

    Time is already formatted, so it seems just replacing will be enough.

     private String stringForTime(String ytFormattedTime) {
                     return ytFormattedTime
                            .replace("PT","")
                            .replace("H",":")
                            .replace("M",":")
                            .replace("S","");
     }
    
    0 讨论(0)
  • 2021-02-07 03:48

    The question Converting ISO 8601-compliant String to java.util.Date contains another solution:

    The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

    0 讨论(0)
  • 2021-02-07 03:49

    And yet another long way to do the same.

    // PT1H9M24S -->  1:09:24
    // PT2H1S"   -->  2:00:01
    // PT23M2S   -->  23:02
    // PT31S     -->  0:31
    
    public String convertDuration(String duration) {
        duration = duration.substring(2);  // del. PT-symbols
        String H, M, S;
        // Get Hours:
        int indOfH = duration.indexOf("H");  // position of H-symbol
        if (indOfH > -1) {  // there is H-symbol
            H = duration.substring(0,indOfH);      // take number for hours
            duration = duration.substring(indOfH); // del. hours
            duration = duration.replace("H","");   // del. H-symbol
        } else {
            H = "";
        }
        // Get Minutes:
        int indOfM = duration.indexOf("M");  // position of M-symbol
        if (indOfM > -1) {  // there is M-symbol
            M = duration.substring(0,indOfM);      // take number for minutes
            duration = duration.substring(indOfM); // del. minutes
            duration = duration.replace("M","");   // del. M-symbol
            // If there was H-symbol and less than 10 minutes
            // then add left "0" to the minutes
            if (H.length() > 0 && M.length() == 1) {
                M = "0" + M;
            }
        } else {
            // If there was H-symbol then set "00" for the minutes
            // otherwise set "0"
            if (H.length() > 0) {
                M = "00";
            } else {
                M = "0";
            }
        }
        // Get Seconds:
        int indOfS = duration.indexOf("S");  // position of S-symbol
        if (indOfS > -1) {  // there is S-symbol
            S = duration.substring(0,indOfS);      // take number for seconds
            duration = duration.substring(indOfS); // del. seconds
            duration = duration.replace("S","");   // del. S-symbol
            if (S.length() == 1) {
                S = "0" + S;
            }
        } else {
            S = "00";
        }
        if (H.length() > 0) {
            return H + ":" +  M + ":" + S;
        } else {
            return M + ":" + S;
        }
    }
    
    0 讨论(0)
  • I may be late for the party, it's actually very simple. Although there may be better ways of doing this. duration is in milliseconds.

    public long getDuration() {
        String time = "PT15H12M46S".substring(2);
        long duration = 0L;
        Object[][] indexs = new Object[][]{{"H", 3600}, {"M", 60}, {"S", 1}};
        for(int i = 0; i < indexs.length; i++) {
            int index = time.indexOf((String) indexs[i][0]);
            if(index != -1) {
                String value = time.substring(0, index);
                duration += Integer.parseInt(value) * (int) indexs[i][1] * 1000;
                time = time.substring(value.length() + 1);
            }
        }
        return duration;
    }
    
    0 讨论(0)
  • 2021-02-07 03:52

    Here is my solution

    public class MyDateFromat{
        public static void main(String args[]){
            String ytdate = "PT1H1M15S";
            String result = ytdate.replace("PT","").replace("H",":").replace("M",":").replace("S","");
            String arr[]=result.split(":");
            String timeString = String.format("%d:%02d:%02d", Integer.parseInt(arr[0]), Integer.parseInt(arr[1]),Integer.parseInt(arr[2]));
            System.out.print(timeString);       
        }
    }
    

    It will return a string in H:MM:SS format if you want to convert in seconds you can use

    int timeInSedonds = int timeInSecond = Integer.parseInt(arr[0])*3600 + Integer.parseInt(arr[1])*60 +Integer.parseInt(arr[2])
    

    Note: it can throw exception so please handle it based on size of result.split(":");

    0 讨论(0)
提交回复
热议问题