Last active
March 19, 2018 05:37
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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