In the java.time framework of Java 8 and later, the Duration
class says:
This class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours. In addition, the
DAYS
unit can be used and is treated as exactly equal to 24 hours, thus ignoring daylight savings effects.
Yet when I call the get
method and pass ChronoUnit.DAYS
, an exception is thrown.
LocalTime start = LocalTime.of ( 0 , 0 , 0 ); // First moment of the day.
LocalTime stop = LocalTime.of ( 2 , 0 , 0 ); // 2 AM.
Duration duration = Duration.between ( start , stop );
long days = duration.get ( ChronoUnit.DAYS );
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Days
Am I misunderstanding something, or misusing the classes?
ChronoUnit
constants that work inDuration.get(...)
areSECONDS
andNANOS
but I never found out why. – FingerbreadthDuration
which only holds seconds and nanoseconds. Theget(TemporalField)
-method is designed to yield a partial amount according to the inner state. In contrast,toDays()
tries to determine the whole amount converted to days on base 1d=24h. – Silveryget(TemporalField)
I wanted to say:get(TemporalUnit)
. – Silvery