Java Duration class

Java Duration class is used to measures time in seconds and nanoseconds. It inherits the Object class and implements the Comparable interface.


Java Duration class declaration

Let's see the declaration of java.time.Duration class.

Methods of Java Duration

Method Description
Temporal addTo(Temporal temporal) It is used to add this duration to the specified temporal object.
static Duration between(Temporal startInclusive, Temporal endExclusive) It is used to obtain a Duration representing the duration between two temporal objects.
long get(TemporalUnit unit) It is used to get the value of the requested unit.
boolean isNegative() It is used to check if this duration is negative, excluding zero.
boolean isZero() It is used to check if this duration is zero length.
Duration minus(Duration duration) It is used to return a copy of this duration with the specified duration subtracted.
Duration plus(Duration duration) It is used to return a copy of this duration with the specified duration added.

Java Duration Example: get()

Test it Now

Output:

43199

Java Duration Example: isNegative()

Test it Now

Output:

true
false

Java Duration Example: between()

Test it Now

Output:

43199

Java Duration Example: minus()

Test it Now

Output:

43199
0

Java Duration Example: plus()

Test it Now

Output:

43199
86399
Next TopicJava Instant




Contact US

Email:[email protected]

Java Duration
10/30