Duration Class in Java 8

A Duration is time elapsed between two Instant’s. Duration concept is linked to Instant. The Duration is the amount of time between two Instant’s,  between LocalTime or between LocalDateTime.

Duration is suitable when working with machine time and Instants. Durations can contain negative values if the end point occurs before the starting point.

It has static method between which takes two instant of a timeline and give duration elapsed between these 2 instant on timelines. We have bunch of method in this class like toNanos(), toMillis(), toSeconds(), toMinutes(), toHours(), toDays(), minusNanons() – using minusNamnos() method, I can subtract  nano seconds to given duration, plusNanos() – using plusNanos() method, I can add nano seconds to given duration using this method and mutipliedBy(), dividedBy(), negated(), isZero(), isNegative() etc.

 

Reference:

Java 8 Duration Doc

Leave a Reply

Your email address will not be published. Required fields are marked *