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