private static Duration parseDuration ( String duration ) throws ParseException { Matcher matcher = pattern . matcher ( duration ) ; long nanoseconds = 0L ; if ( matcher . find ( ) && matcher . groupCount ( ) == 4 ) { int days = Integer . parseInt ( matcher . group ( 1 ) ) ; nanoseconds += TimeUnit . NANOSECONDS . convert ( days , TimeUnit . DAYS ) ; int hours = Integer . parseInt ( matcher . group ( 2 ) ) ; nanoseconds += TimeUnit . NANOSECONDS . convert ( hours , TimeUnit . HOURS ) ; int minutes = Integer . parseInt ( matcher . group ( 3 ) ) ; nanoseconds += TimeUnit . NANOSECONDS . convert ( minutes , TimeUnit . MINUTES ) ; int seconds = Integer . parseInt ( matcher . group ( 4 ) ) ; nanoseconds += TimeUnit . NANOSECONDS . convert ( seconds , TimeUnit . SECONDS ) ; long nanos = Long . parseLong ( matcher . group ( 5 ) ) ; nanoseconds += nanos ; } else { throw new ParseException ( "Cannot parse duration " + duration , 0 ) ; } return Duration . ofNanos ( nanoseconds ) ; }
Parses a duration string of the form 98d 01h 23m 45s into milliseconds .