The java.time.Duration class is used to measures time in seconds and nanoseconds.
Java Duration class methods
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. |
Example
package com.w3spoint;
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class TestExample {
public static void main(String args[]){
Duration duration = Duration.between(LocalTime.NOON,LocalTime.MAX);
//get() method
System.out.println(duration.get(ChronoUnit.SECONDS));
//isNegative() method
System.out.println(duration.isNegative());
}
} |
package com.w3spoint; import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit; public class TestExample {
public static void main(String args[]){
Duration duration = Duration.between(LocalTime.NOON,LocalTime.MAX);
//get() method
System.out.println(duration.get(ChronoUnit.SECONDS));
//isNegative() method
System.out.println(duration.isNegative());
}
}
Output