Javatpoint Logo
Javatpoint Logo

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 Duration between(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.
int getNano() It gets the number of nanoseconds within the second in this duration.
long getSeconds() It gets the number of seconds in this duration.
static Duration of(long amount, TemporalUnit unit) It obtains a Duration representing an amount in the specified unit.
static Duration ofDays(long days) It obtains a Duration representing a number of standard 24 hour days.
static Duration ofHours(long hours) It obtains a Duration representing a number of standard hours.
static Duration ofMillis(long millis) It obtains a Duration representing a number of milliseconds.
static Duration ofMinutes(long minutes) It obtains a Duration representing a number of standard minutes.
static Duration ofNanos(long nanos) It obtains a Duration representing a number of nanoseconds.
static Duration ofSeconds(long seconds) It obtains a Duration representing a number of seconds.

Java Duration Example: get()

DurationExample1.java

Test it Now

Output:

43199

Java Duration Example: isNegative()

DurationExample2.java

Test it Now

Output:

true
false

Java Duration Example: between()

DurationExample3.java

Test it Now

Output:

43199

Java Duration Example: minus()

DurationExample4.java

Test it Now

Output:

43199
0

Java Duration Example: plus()

DurationExample5.java

Test it Now

Output:

43199
86399

Next TopicJava Instant





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA