Java 9 and later: Duration::to…Part
methods
In Java 9 the Duration
class gained new to…Part
methods for returning the various parts of days, hours, minutes, seconds, milliseconds/nanoseconds. See this pre-release OpenJDK source code.
Given a duration of 49H30M20.123S…
toNanosPart()
= 123000000toMillisPart()
= 123toSecondsPart()
= 20toMinutesPart()
= 30toHoursPart()
= 1toDaysPart()
= 2
Remember that “days” here means chunks of 24-hours, ignoring dates on a calendar. If you care about dates, use Period
class instead.
I do not know if any additional formatter features are added. But at least you will be able to more conveniently generate your own strings from numbers obtained via these new getter methods.
Java 8
Oddly enough, no convenient getter methods for these values were included in the first edition release of java.time in Java 8. One of very few oversights in the otherwise excellent design of the java.time framework.
See the related Question: Why can’t I get a duration in minutes or hours in java.time?.