Skip to content

Instantly share code, notes, and snippets.

@ppazos
Last active March 19, 2018 05:37
Show Gist options
  • Save ppazos/6f35ed70d8a4a70f9c47999604649e1d to your computer and use it in GitHub Desktop.
Save ppazos/6f35ed70d8a4a70f9c47999604649e1d to your computer and use it in GitHub Desktop.
Couple of methods on how to parse an ISO 8601 duration expression in Java/Groovy.
import javax.xml.datatype.*;
Duration dur = DatatypeFactory.newInstance().newDuration("PT5H12M36S");
println dur.getHours()
println dur.getMinutes()
println dur.getSeconds()
println dur
println dur.getHours()*60*60 + dur.getMinutes()*60 + dur.getSeconds()
// ONLY WORKS ON JAVA 8!
java.time.Duration d = java.time.Duration.parse("PT5H12M36S");
println("Duration in seconds: " + d.get(java.time.temporal.ChronoUnit.SECONDS));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment