96
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. |
Duration abs() | It returns a copy of this duration with a positive length. |
static Durationbetween(Temporal startInclusive, Temporal endExclusive) | It obtains a Duration representing the duration between two temporal objects. |
int compareTo(Duration otherDuration) | It compares the given duration to the specified Duration. |
intgetNano() | It gets the number of nanoseconds within the second in this duration. |
long getSeconds() | It gets the number of seconds in this duration. |
static Durationof(long amount, TemporalUnit unit) | It obtains a Duration representing an amount in the specified unit. |
static DurationofDays(long days) | It obtains a Duration representing a number of standard 24 hour days. |
static DurationofHours(long hours) | It obtains a Duration representing a number of standard hours. |
static DurationofMillis(long millis) | It obtains a Duration representing a number of milliseconds. |
static DurationofMinutes(long minutes) | It obtains a Duration representing a number of standard minutes. |
static DurationofNanos(long nanos) | It obtains a Duration representing a number of nanoseconds. |
static DurationofSeconds(long seconds) | It obtains a Duration representing a number of seconds. |
Java Duration Example: get()
DurationExample1.java
Output:
43199
Java Duration Example: isNegative()
DurationExample2.java
Output:
true false
Java Duration Example: between()
DurationExample3.java
Output:
43199
Java Duration Example: minus()
DurationExample4.java
Output:
43199 0
Java Duration Example: plus()
DurationExample5.java
Output:
43199 86399
Next TopicJava Instant