java.time.Duration Class in Java

Duration is the value-based Class present in the Java time library. It’s used to get time-based amount of time. This class is immutable and thread-safe. This article describes all the methods present in this class and some basic examples using class methods.
This class implements Serializable, Comparable<Duration>, TemporalAmount interfaces.
Field present in Class:
| Field | Description |
|---|---|
| static Duration | It is a constant for zero duration. |
Methods present in Class:
| Method | Description |
|---|---|
| abs() | This method returns positive copy of length |
| addTo(Temporal temporal) | This method adds this duration to the specified temporal object. |
| Duration between(Temporal startInclusive, Temporal endExclusive) | This method finds the duration between two Temporal objects. |
| compareTo(Duration otherDuration) | This method compares this duration to the specified Duration. |
| dividedBy(long divisor) | This method returns a copy of this duration divided by the specified value. |
| equals(Object otherDuration) | This method checks if this duration is equal to the specified Duration. |
| from(TemporalAmount amount) | This method obtains an instance of Duration from a temporal amount. |
| get(TemporalUnit unit) | This method gets the value of the requested unit. |
| getNano() | This method gets the number of nanoseconds within the second in this duration. |
| getSeconds() | This method gets the number of seconds in this duration. |
| getUnits() | This method gets the set of units supported by this duration. |
| hashCode() | A hash code for this duration. |
| isNegative() | This method checks if this duration is negative, excluding zero. |
| isZero() | This method checks if this duration is zero length. |
| minus(Duration duration) | This method returns a copy of this duration with the specified duration subtracted. |
| minus(long amountToSubtract, TemporalUnit unit) | This method returns a copy of this duration with the specified duration subtracted. |
| minusDays(long daysToSubtract) | This method returns a copy of this duration with the specified duration in standard 24-hour days subtracted. |
| minusHours(long hoursToSubtract) | This method returns a copy of this duration with the specified duration in hours subtracted. |
| minusMillis(long millisToSubtract) | This method returns a copy of this duration with the specified duration in milliseconds subtracted. |
| minusMinutes(long minutesToSubtract) | This method returns a copy of this duration with the specified duration in minutes subtracted. |
| minusNanos(long nanosToSubtract) | This method returns a copy of this duration with the specified duration in nanoseconds subtracted. |
| minusSeconds(long secondsToSubtract) | This method returns a copy of this duration with the specified duration in seconds subtracted. |
| multipliedBy(long multiplicand) | This method returns a copy of this duration multiplied by the scalar. |
| negated() | This method returns a copy of this duration with the length negated. |
| of(long amount, TemporalUnit unit) | This method obtains a Duration representing an amount in the specified unit. |
| ofDays(long days) | This method obtains a Duration representing a number of standard 24 hour days. |
| ofHours(long hours) | This method obtains a Duration representing a number of standard hours. |
| ofMillis(long millis) | This method obtains a Duration representing a number of milliseconds. |
| ofMinutes(long minutes) | This method obtains a Duration representing a number of standard minutes. |
| ofNanos(long nanos) | This method obtains a Duration representing a number of nanoseconds. |
| ofSeconds(long seconds) | This method obtains a Duration representing a number of seconds. |
| ofSeconds(long seconds, long nanoAdjustment) | This method obtains a Duration representing a number of seconds and an adjustment in nanoseconds. |
| parse(CharSequence text) | This method obtains a Duration from a text string such as PnDTnHnMn.nS. |
| plus(Duration duration) | This method returns a copy of this duration with the specified duration added. |
| plus(long amountToAdd, TemporalUnit unit) | This method returns a copy of this duration with the specified duration added. |
| plusDays(long daysToAdd) | This method returns a copy of this duration with the specified duration in standard 24 hour days added. |
| plusHours(long hoursToAdd) | This method returns a copy of this duration with the specified duration in hours added. |
| plusMillis(long millisToAdd) | This method returns a copy of this duration with the specified duration in milliseconds added. |
| plusMinutes(long minutesToAdd) | This method returns a copy of this duration with the specified duration in minutes added. |
| plusNanos(long nanosToAdd) | This method returns a copy of this duration with the specified duration in nanoseconds added. |
| plusSeconds(long secondsToAdd) | This method returns a copy of this duration with the specified duration in seconds added. |
| subtractFrom(Temporal temporal) | This method subtracts this duration from the specified temporal object. |
| toDays() | This method gets the number of days in this duration. |
| toHours() | This method gets the number of hours in this duration. |
| toMillis() | This method converts this duration to the total length in milliseconds. |
| toMinutes() | This method gets the number of minutes in this duration. |
| toNanos() | This method converts this duration to the total length in nanoseconds expressed as a long. |
| toString() | A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S. |
| withNanos(int nanoOfSecond) | This method returns a copy of this duration with the specified nano-of-second. |
| withSeconds(long seconds) | This method returns a copy of this duration with the specified amount of seconds. |
Example 1: This example illustrates simple use of Duration class.
Java
// Importing required classesimport java.time.Duration;import java.time.LocalTime;import java.time.temporal.ChronoUnit;public class GFG { public static void main(String[] args) { // Initializing Duration variable Duration duration = Duration.between(LocalTime.NOON,LocalTime.MIDNIGHT); // Printing difference between time in seconds System.out.println(duration.get(ChronoUnit.SECONDS)); // Finding absolute difference Duration absDuration = duration.abs(); // Printing absolute time difference in seconds System.out.println(absDuration.get(ChronoUnit.SECONDS)); }} |
Output
-43200 43200
Example 2:
Java
// Importing required classesimport java.time.Duration;import java.time.temporal.ChronoUnit;public class GFG { public static void main(String[] args) { // Getting duration in an hour Duration duration = Duration.from(ChronoUnit.HOURS.getDuration()); // Printing duration in minutes System.out.println(duration.toMinutes()); }} |
Output:
60



