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());
And yet another long way to do the same.
Here is my solution
It will return a string in H:MM:SS format if you want to convert in seconds you can use
Note: it can throw exception so please handle it based on size of result.split(":");
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):
You can use the standart
SimpleDateFormat
to parse theString
to aDate
and process it from there:I did by myself
Let's try
The question Converting ISO 8601-compliant String to java.util.Date contains another solution: