Convert seconds into T1H15M5S (ISO_8601)

Tags: , , ,



I would like to convert a number of seconds into ISO_8601/Duration in Java.

http://en.wikipedia.org/wiki/ISO_8601#Durations

Are there any existing methods to do it that are already built in?

Answer

Since ISO 8601 allows for the individual fields in a duration string to overflow, you could just prepend “PT” to the number of seconds and append “S”:

int secs = 4711;
String iso8601format = "PT" + secs + "S";

This will output “PT4711S”, which is equivalent to “PT1H18M31S”.



Source: stackoverflow