o7planning

Java Duration Tutorial with Examples

View more Tutorials:

Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- Duration

The Duration class represents an amount of time with nanosecond precision. For example: "2 days 3 hours", "2 hours 3 minutes 15 seconds", "2 seconds 123 nanoseconds",..

public final class Duration
        implements TemporalAmount, Comparable<Duration>, Serializable
  • ChronoPeriod
  • Period
  • TemporalAmount
The Duration and Period classes both represent an amount of time, but they have differences. Duration represents an exact amount of time, while Period represents an estimated amount of time.
  • Duration can be converted to NANOS, MILLIS, SECONDS, MINITES, HOURS or DAYS. These units are precise, with 1 day being considered 24 hours.
  • Period supports DAYS, MONTHS, YEARS units, they are independent of each other. The reason is that these units are estimation, they are not fixed length.
Example:
  Example
Duration 1 days 23 hours 15 seconds
Period 3 years 5 months 1 days
Day, hour, minute, second, millisecond, nanosecond values in Duration can be converted to each other. For example, "13 hours and 90 minutes" equal to "14 hours and 30 minutes".
Even though Duration is a class based on the number of nanoseconds. But to compare 2 Duration objects, you should use equals(Object) method.

2- Factory methods

Static factory methods:

public static Duration ofDays(long days)  
public static Duration ofHours(long hours)  
public static Duration ofMinutes(long minutes)
public static Duration ofSeconds(long seconds)
public static Duration ofSeconds(long seconds, long nanoAdjustment)
public static Duration ofMillis(long millis)
public static Duration ofNanos(long nanos)
public static Duration of(long amount, TemporalUnit unit)
public static Duration from(TemporalAmount amount)
public static Duration parse(CharSequence text)  
public static Duration between(Temporal startInclusive, Temporal endExclusive)
Example: Create a Duration from seconds and nanoseconds:
Duration_ofSeconds_ex2.java

// ofSeconds(long seconds, long nanoAdjustment)
// 3 minutes 20 seconds 12345 nanoseconds.
Duration duration = Duration.ofSeconds(3*60 + 20, 12345); // 3M 20S 12345 Nanos.

System.out.println(duration); // PT3M20.000012345S
System.out.println("duration.toHoursPart(): " + duration.toHoursPart()); // 0
System.out.println("duration.toMinutesPart(): " + duration.toMinutesPart()); // 3
System.out.println("duration.toSecondsPart(): " + duration.toSecondsPart()); // 20
System.out.println("duration.toNanosPart(): " + duration.toNanosPart()); // 12345
Example: Create a Duration that is the time interval between 2 Temporal objects. Note: These two Temporal objects must support ChronoUnit.SECONDS, otherwise an exception will be thrown. For the highest accuracy, ChronoUnit.NANOS or ChronoField.NANO_OF_SECOND must be supported.
Duration_between_ex1.java

LocalDateTime localDateTime1 = LocalDateTime.of(2020, 5, 15, 13, 30, 45);
LocalDateTime localDateTime2 = LocalDate.of(2020, 5, 17).atStartOfDay();

Duration duration = Duration.between(localDateTime1, localDateTime2);

System.out.println(duration); // PT34H29M15S  (10 hours 29 minutes 15 seconds).
System.out.println("duration.toHoursPart(): " + duration.toHoursPart()); // 10
System.out.println("duration.toMinutesPart(): " + duration.toMinutesPart()); // 29
System.out.println("duration.toSecondsPart(): " + duration.toSecondsPart()); // 15

3- Other methods

Other methods:

public boolean isZero()  
public boolean isNegative()  

// -----------------------------------------------------------------
// getX()
// -----------------------------------------------------------------

// Inherited from TemporalAmount interface
public long get(TemporalUnit unit)

// Inherited from TemporalAmount interface
public List<TemporalUnit> getUnits()

public long getSeconds()  
public int getNano()  

// -----------------------------------------------------------------
// withX()
// -----------------------------------------------------------------

public Duration withSeconds(long seconds)  
public Duration withNanos(int nanoOfSecond)

// -----------------------------------------------------------------
// plusX()
// -----------------------------------------------------------------
public Duration plus(Duration duration)  
public Duration plus(long amountToAdd, TemporalUnit unit)  

public Duration plusDays(long daysToAdd)  
public Duration plusHours(long hoursToAdd)  
public Duration plusMinutes(long minutesToAdd)  
public Duration plusSeconds(long secondsToAdd)  
public Duration plusMillis(long millisToAdd)  
public Duration plusNanos(long nanosToAdd)  

// -----------------------------------------------------------------
// minusX()
// -----------------------------------------------------------------
public Duration minus(Duration duration)  
public Duration minus(long amountToSubtract, TemporalUnit unit)

public Duration minusDays(long daysToSubtract)
public Duration minusHours(long hoursToSubtract)  
public Duration minusMinutes(long minutesToSubtract)  
public Duration minusSeconds(long secondsToSubtract)  
public Duration minusMillis(long millisToSubtract)
public Duration minusNanos(long nanosToSubtract)  

// -----------------------------------------------------------------
// toX()
// -----------------------------------------------------------------

public long toDays()  
public long toHours()  
public long toMinutes()  
public long toSeconds()  
public long toMillis()  
public long toNanos()  

// -----------------------------------------------------------------
// toXPart()
// -----------------------------------------------------------------

public long toDaysPart()  
public int toHoursPart()  
public int toMinutesPart()
public int toSecondsPart()  
public int toMillisPart()  
public int toNanosPart()  

// -----------------------------------------------------------------
// Other methods:
// -----------------------------------------------------------------

public Duration truncatedTo(TemporalUnit unit)
public Duration multipliedBy(long multiplicand)
public Duration dividedBy(long divisor)
public long dividedBy(Duration divisor)
public Duration negated()  
public Duration abs()  

// Inherited from TemporalAmount interface
public Temporal addTo(Temporal temporal)

// Inherited from TemporalAmount interface
public Temporal subtractFrom(Temporal temporal)  

4- toX() *

The toNanos() method returns the total nanoseconds in this Duration.
Other methods like toMillis(), toSeconds(), toMinutes(), toHours(), toDays() are also understood as their names.

public long toNanos()  
public long toMillis()  
public long toSeconds()  
public long toMinutes()  
public long toHours()  
public long toDays() 
Specifically we have:
  • toMinutes() equals the integer part of toSeconds() division by 60.
  • toHours() equals the integer part of toSeconds() division by 60*60.
  • toDays() equals the integer part of toSeconds() division by 24*60*60.
(*) If toNanos() > Long.MAX_VALUE or toMillis() > Long.MAX_VALUE then ArithmeticException will be thrown.
toX() Same as: Equals to Ranges
toNanos()      0-Long.MAX_VALUE
toMillis()      
toSeconds() getSeconds()  
toMinutes()   toSeconds() / 60
toHours()    toSeconds() / (60*60)
toDays()     toSeconds() / (24*60*60)
Example:
Duration_toX_ex1.java

// 10 days 10 hours 10 minutes 10 seconds.
long seconds = 10 * 24 * 60 * 60 + 10 * 60 * 60 + 10 * 60 + 10;

//
// 1 milliseconds = 10^6 nanoseconds
// 1 seconds = 10^9 nanoseconds
//
// (10 days 10 hours 10 minutes 10 seconds) + (10 milliseconds 10 nanoseconds).
long nanos = seconds * 1000000000L + 10 * 1000000L + 10L;

Duration duration = Duration.ofNanos(nanos);

System.out.println(duration); // PT250H10M10.01000001S

System.out.println("duration.toDays(): " + duration.toDays()); // 10
System.out.println("duration.toHours(): " + duration.toHours()); // 250
System.out.println("duration.toMinutes(): " + duration.toMinutes()); // 15010
System.out.println("duration.toSeconds(): " + duration.toSeconds()); // 900610
System.out.println("duration.toMillis(): " + duration.toMillis()); // 900610010
System.out.println("duration.toNanos(): " + duration.toNanos()); // 900610010000010

System.out.println("duration.getSeconds(): " + duration.getSeconds()); // 900610

5- toXPart() *

The toNanosPart() method returns the nanosecond part of this Duration, which is not the total nanoseconds in the Duration.
The methods toMillisPart(), toMinutesPart(), toHoursPart() are also understood as their names.
The toDaysPart() method returns the same result as toDays().

public int toNanosPart()  
public int toMillisPart()  
public int toSecondsPart()  
public int toMinutesPart()
public int toHoursPart()  
public long toDaysPart() 
toX() Same as: Equals to Range
toDaysPart()   toDays() toDays() 0-Long.MAX_VALUE
toHoursPart()     toHours() % 24 0-23
toMinutesPart()   toMinutes() % 60 0-59
toSecondsPart()     toSeconds() % 60 0-59
toMillisPart()     toMillis() % 10^3 0-10^3-1
toNanosPart()  getNano() toNanos() % 10^9 0-10^9-1
Example:
Duration_toXPart_ex1.java

// 10 days 10 hours 10 minutes 10 seconds.
long seconds = 10 * 24 * 60 * 60 + 10 * 60 * 60 + 10 * 60 + 10;

//
// 1 milliseconds = 10^6 nanoseconds
// 1 seconds = 10^9 nanoseconds
//
// (10 days 10 hours 10 minutes 10 seconds) + (10 milliseconds 10 nanoseconds).
long nanos = seconds * 1000000000L + 10 * 1000000L + 10;

Duration duration = Duration.ofNanos(nanos);

System.out.println(duration); // PT250H10M10.01000001S

System.out.println("duration.toDaysPart(): " + duration.toDaysPart()); // 10
System.out.println("duration.toHoursPart(): " + duration.toHoursPart()); // 10
System.out.println("duration.toMinutesPart: " + duration.toMinutesPart()); // 10
System.out.println("duration.toSecondsPart: " + duration.toSecondsPart()); // 10

System.out.println("duration.toMillisPart: " + duration.toMillisPart()); // 10
System.out.println("duration.toNanosPart: " + duration.toNanosPart()); // 10000010

System.out.println("duration.getNano(): " + duration.getNano()); // 10000010

6- isZero()

Return true if the total number of nanoseconds in this Duration is 0, otherwise return false.

public boolean isZero() 
Example:
Duration_isZero_ex1.java

// Duration using parse() method
Duration duration1 = Duration.parse("P0DT0H0M");
System.out.println("duration1: " + duration1); // PT0S
System.out.println("duration1.isZero(): " + duration1.isZero()); // true

Duration duration2 = Duration.parse("P0DT0H20M");
System.out.println("\nduration2: " + duration2); // PT20M
System.out.println("duration2.isZero(): " + duration2.isZero()); // false

Duration duration3 =  duration2.minus(Duration.ofMinutes(20));
System.out.println("\nduration3: " + duration3); // PT0S
System.out.println("duration3.isZero(): " + duration3.isZero()); // true

Duration duration4 =  Duration.parse("P0DT1H-60M"); // 1 hour & -60 minutes
System.out.println("\nduration4: " + duration4); // PT0S
System.out.println("duration4.isZero(): " + duration4.isZero()); // true
Output:

duration1: PT0S
duration1.isZero(): true

duration2: PT20M
duration2.isZero(): false

duration3: PT0S
duration3.isZero(): true

duration4: PT0S
duration4.isZero(): true

7- isNegative()

Return true if the total number of nanoseconds in this Duration is less than 0, otherwise return false.

public boolean isNegative()  
Example:
Duration_isNegative_ex1.java

// 10 hours 10 minutes
Duration duration1 = Duration.ofMinutes(10 * 60 + 10);
// 20 hours 20 seconds
Duration duration2 = Duration.ofSeconds(20 * 60 * 60 + 20);

// duration1 - duration2
Duration duration = duration1.minus(duration2);
System.out.println("duration: " + duration); // PT-9H-50M-20S
System.out.println("duration.isNegative(): " + duration.isNegative()); // true
Output:

duration: PT-9H-50M-20S
duration.isNegative(): true

8- get(TemporalUnit)

This method only supports 2 units, ChronoUnit.SECONDS and ChronoUnit.NANOS. Other units will throw an exception.
  • get(ChronoUnit.SECONDS) returns the total seconds in this Duration, which is equivalent to toSeconds().
  • get(ChronoUnit.NANOS) returns the nanosecond part in this Duration, which is equivalent to toNanosPart(), the value is in the range 0 to 10^9-1.

// Inherited from TemporalAmount interface
public long get(TemporalUnit unit)
get(TemporalUnit) Same as Example
    2 minutes 12345 nanos
get(ChronoUnit.SECONDS) toSeconds() getSeconds() 2*60 seconds
get(ChronoUnit.NANOS) toNanosPart() getNano() 12345 nanos
Example:
Duration_get_unit_ex1.java

// 2 minutes 12345 nanoseconds.
Duration duration = Duration.ofSeconds(2 * 60, 12345);
System.out.println("duration: " + duration); // PT2M0.000012345S
System.out.println();  
 
System.out.println("SECONDS: " + duration.get(ChronoUnit.SECONDS)); // 120
System.out.println("NANOS: " + duration.get(ChronoUnit.NANOS)); // 12345

9- getUnits()

Return the units supported by this Duration.

// Inherited from TemporalAmount interface
public List<TemporalUnit> getUnits()
This method always returns List.of(ChronoUnit.SECONDS,ChronoUnit.NANOS).

10- getX() *

The getSeconds() method returns the total seconds in this Duration, which is similar to the toSeconds() method.
The getNano() method returns the nanosecond part of this Duration, which is the same as the toNanosPart() method.

public long getSeconds()  
public int getNano()  
getX() Same as: Range Example
      2 minutes 12345 nanos
getSeconds() toSeconds() 0-Long.MAX_VALUE 120 seconds
getNano() toNanosPart() 0-10^9-1 12345 nano
Example:
Duration_getX_ex1.java

// 10 minutes 10 seconds 10 nanos.  (1 seconds = 10^9 nanos)
long nano = 10 * 60 * 1000000000L + 10 * 1000000000L + 10;

System.out.println("nano: " + nano); // 610000000010

Duration duration = Duration.ofNanos(nano);

System.out.println(duration); // PT10M10.00000001S

System.out.println("duration.getSeconds(): " + duration.getSeconds()); // 610
System.out.println("duration.getNano(): " + duration.getNano()); // 10

System.out.println("duration.toSeconds(): " + duration.toSeconds()); // 610
System.out.println("duration.toNanosPart(): " + duration.toNanosPart()); // 10
System.out.println("duration.toNanos(): " + duration.toNanos()); // 610000000010

11- withX(..) *


public Duration withSeconds(long seconds)  
public Duration withNanos(int nanoOfSecond)
The withSeconds(seconds) method returns a copy of this Duration, preserving the nanoseconds, and replacing the total seconds with the given seconds.
Duration_withSeconds_ex1.java

// 10 minutes 10 seconds 99 nanos
Duration duration = Duration.ofSeconds(10 * 60 + 10, 99);
System.out.println("duration: " + duration); // PT10M10.000000099S

// with 120 seconds (2 minutes)
Duration duration1 = duration.withSeconds(120);

System.out.println("duration1: " + duration1); // PT2M0.000000099S (2 minutes 99 nanos)
The withNanos(nanos) method returns a copy of this Duration, replace the nanosecond part and keep the others. The nanos parameter has a value from 0 to 10^9-1.
Duration_withNanos_ex1.java

// 10 minutes 10 seconds 99 nanos
Duration duration = Duration.ofSeconds(10 * 60 + 10, 99);
System.out.println("duration: " + duration); // PT10M10.000000099S

// with 111 nanos
Duration duration1 = duration.withNanos(111);
System.out.println("duration1: " + duration1); // PT10M10.000000111S (10 minutes 10 seconds 111 nanos)

12- plus(Duration)

Add this Duration with a specified Duration to create a new Duration. The returned result is normalized.

public Duration plus(Duration duration)  
Example:
Duration_plus_duration_ex1.java

// 20 hours 20 minutes 20 seconds
Duration duration1 = Duration.ofSeconds(20 * 60 * 60 + 20 * 60 + 20);
System.out.println("duration1: " + duration1); // PT20H20M20S

// 50 minutes 10 seconds
Duration duration2 = Duration.ofSeconds(50 * 60 + 10);
System.out.println("duration1: " + duration2); // PT50M10S

Duration duration3 = duration1.plus(duration2);
System.out.println("duration3: " + duration3); // PT21H10M30S (21 Hours 10 minutes 30 seconds) 
Output:

duration1: PT20H20M20S
duration1: PT50M10S
duration3: PT21H10M30S

13- plus(long, TemporalUnit)

Return a copy of this Duration with an amount of time added in the given unit.

public Duration plus(long amountToAdd, TemporalUnit unit)  
Note: This method only supports TemporalUnit(s) with exact duration. For example ChronoUnit.DAYS is equivalent to 24 hours. ChronoUnit.MONTHS is considered an estimate because its length is not fixed. In short, a temporalUnit is supported by this method if temporalUnit.isDurationEstimated() returns false.
The following standard units are supported:
  • ChronoUnit.NANOS
  • ChronoUnit.MICROS
  • ChronoUnit.MILLIS
  • ChronoUnit.SECONDS
  • ChronoUnit.MINUTES
  • ChronoUnit.HOURS
  • ChronoUnit.HALF_DAYS
  • ChronoUnit.DAYS
Example:
Duration_plus_unit_ex1.java

// 10 hours 10 minutes 10 seconds
Duration duration1 = Duration.ofSeconds(10 * 60 * 60 + 10 * 60 + 10);
System.out.println("duration1: " + duration1); // PT10H10M10S

// plus 1 DAY (24 Hours)
Duration duration2 = duration1.plus(1, ChronoUnit.DAYS);
System.out.println("duration2: " + duration2); // PT34H10M10S (34 hours 12 minutes 40 seconds)

// plus 150 Seconds  (2 minutes 30 seconds)
Duration duration3 = duration1.plus(150, ChronoUnit.SECONDS);
System.out.println("duration3: " + duration3); // PT10H12M40S (10 hours 12 minutes 40 seconds)

14- plusX(..) *

The plusNanos(nanosToAdd) method returns a copy of this Duration with the specified amount of nanoseconds added.
Other methods like plusMillis(millisToAdd), plusSeconds(secondsToAdd), plusMinutes(long minutesToAdd), plusHours(hoursToAdd), plusDays(daysToAdd) are also understood in the same way as their names.

public Duration plusNanos(long nanosToAdd)  
public Duration plusMillis(long millisToAdd)  
public Duration plusSeconds(long secondsToAdd)  
public Duration plusMinutes(long minutesToAdd)  
public Duration plusHours(long hoursToAdd)  
public Duration plusDays(long daysToAdd)
Example:
Duration_plusX_ex1.java

// 10 hours 10 minutes 10 seconds
Duration duration1 = Duration.ofSeconds(10 * 60 * 60 + 10 * 60 + 10);
System.out.println("duration1: " + duration1); // PT10H10M10S

// plus 150 seconds. (2 minutes 30 seconds)
Duration duration2 = duration1.plusSeconds(150);
System.out.println("duration2: " + duration2); // PT10H12M40S (10 hours 12 minutes 40 seconds)

// plus -30 minutes.
Duration duration3 = duration1.plusMinutes(-30);
System.out.println("duration3: " + duration3); // PT9H40M10S (9 hours 10 minutes 10 seconds)

15- minus(Duration)

Subtract a specified Duration from this Duration and return a new Duration. The returned result is normalized.

public Duration minus(Duration duration)  
Example:
Duration_minus_duration_ex1.java

// 20 hours 20 minutes 20 seconds
Duration duration1 = Duration.ofSeconds(20 * 60 * 60 + 20 * 60 + 20);
System.out.println("duration1: " + duration1); // PT20H20M20S

// 50 minutes 10 seconds
Duration duration2 = Duration.ofSeconds(50 * 60 + 10);
System.out.println("duration2: " + duration2); // PT50M10S

// duration1 - duration2
Duration duration3 = duration1.minus(duration2);
System.out.println("duration3: " + duration3); // PT19H30M10S (19 Hours 30 minutes 10 seconds) 
Output:

duration1: PT20H20M20S
duration2: PT50M10S
duration3: PT19H30M10S

16- minus(long, TemporalUnit)

Return a copy of this Duration with the specified amount of time subtracted in the given unit.

public Duration minus(long amountToSubtract, TemporalUnit unit)
Note: This method only supports TemporalUnit(s) with exact duration. For example ChronoUnit.DAYS is equivalent to 24 hours. ChronoUnit.MONTHS is considered an estimate because its length is not fixed. In short, a temporalUnit is supported by this method if temporalUnit.isDurationEstimated() returns false.
The following standard units are supported:
  • ChronoUnit.NANOS
  • ChronoUnit.MICROS
  • ChronoUnit.MILLIS
  • ChronoUnit.SECONDS
  • ChronoUnit.MINUTES
  • ChronoUnit.HOURS
  • ChronoUnit.HALF_DAYS
  • ChronoUnit.DAYS
Example:
Duration_minus_unit_ex1.java

// 25 hours 10 minutes 10 seconds
Duration duration1 = Duration.ofSeconds(25 * 60 * 60 + 10 * 60 + 10);
System.out.println("duration1: " + duration1); // PT25H10M10S

// minus 1 DAY (24 Hours)
Duration duration2 = duration1.minus(1, ChronoUnit.DAYS);
System.out.println("duration2: " + duration2); // PT1H10M10S (1 hours 10 minutes 10 seconds)

// minus 150 Seconds  (2 minutes 30 seconds)
Duration duration3 = duration1.minus(150, ChronoUnit.SECONDS);
System.out.println("duration3: " + duration3); // PT25H7M40S (25 hours 7 minutes 40 seconds)

17- minusX(..) *

The minusNanos(nanosToSubtract) method returns a copy of this Duration with the specified amount of nanoseconds subtracted.
Other methods such as minusMillis(millisToSubtract), minusSeconds(secondsToSubtract), minusMinutes(long minutesToSubtract), minusHours(hoursToSubtract), minusDays(daysToSubtract) are also understood in the same way as their names.

public Duration minusNanos(long nanosToSubtract)  
public Duration minusMillis(long millisToSubtract)
public Duration minusSeconds(long secondsToSubtract)  
public Duration minusMinutes(long minutesToSubtract)  
public Duration minusHours(long hoursToSubtract)  
public Duration minusDays(long daysToSubtract)
Example:
Duration_minusX_ex1.java

// 10 hours 10 minutes 10 seconds
Duration duration1 = Duration.ofSeconds(10 * 60 * 60 + 10 * 60 + 10);
System.out.println("duration1: " + duration1); // PT10H10M10S

// minus 30 seconds.
Duration duration2 = duration1.minusSeconds(30);
System.out.println("duration2: " + duration2); // PT10H7M40S (10 hours 9 minutes 40 seconds)

18- truncatedTo(TemporalUnit)

Return a copy of this Duration object truncated to the specified unit.

public Duration truncatedTo(TemporalUnit unit)
This method supports ChronoUnit.DAYS and all time-based units, meaning temporalUnit.isTimeBased() is true. Other units will throw an exception.
Example:
Duration_truncatedTo_ex1.java

// 10 hours 10 minutes 10 seconds
Duration duration1 = Duration.ofSeconds(10 * 60 * 60 + 10 * 60 + 10);
System.out.println("duration1: " + duration1); // PT10H10M10S

// Truncated to ChronoUnit.MINUTES
Duration duration2 = duration1.truncatedTo(ChronoUnit.MINUTES);
System.out.println("duration2: " + duration2); // PT10H10M (10 hours 10 minutes)

19- negated()

Return a negative copy of this Duration. That is, the addition of returned Duration and this Duration equals to 0.

public Duration negated()  
Example:
Duration_negated_ex1.java

// 20 hours 20 minutes 20 seconds
Duration duration1 = Duration.ofSeconds(20 * 60 * 60 + 20 * 60 + 20);
System.out.println("duration1: " + duration1); // PT20H20M20S

// duration2 = 0 - duration1
Duration duration2 = duration1.negated();
System.out.println("duration2: " + duration2); // PT-20H-20M-20S 

20- abs()

Return a copy of this Duration's absolute value, this means that the returned Duration length is a positive value.

public Duration abs()  
Example:
Duration_abs_ex1.java

// -20 hours, 20 minutes, -20 seconds
Duration duration1 = Duration.parse("PT-20H20M-20S");
System.out.println("duration1: " + duration1); // PT-19H-40M-20S (-19H, -40M, -20S)

// duration2 = duration1 < 0 ? -duration1 : duration1;
Duration duration2 = duration1.abs();
System.out.println("duration2: " + duration2); // PT19H40M20S (19H 40M 20S)

21- addTo(Temporal)

Add this Duration to a specified Temporal object and return a copy of this Temporal object.

// Inherited from TemporalAmount interface
public Temporal addTo(Temporal temporal)
This method is equivalent to the Temporal.plus(TemporalAmount) method.

Temporal copiedTemporal = temporal.plus(duration);

Temporal copiedTemporal = duration.addTo(temporal);
Example:
Duration_addTo_ex1.java

// Create Temporal object.
LocalDateTime localDateTime = LocalDateTime.of(1985, 12, 31, 23, 30, 0);
System.out.println("localDateTime: " + localDateTime); // 1985-12-31T23:30

// 1 Hour
Duration duration = Duration.ofHours(1);

LocalDateTime newLocalDateTime = (LocalDateTime) duration.addTo(localDateTime);
System.out.println("newLocalDateTime: " + newLocalDateTime); // 1986-01-01T00:30
On January 1st, 1986, the Nepalese government announced its time zone as +5:45 GMT. That means they are 15 minutes faster than neighboring India. This action is intended to make a difference with the giant neighbor and show the national pride of the Nepalese people.
Thus, January 1st, 1986, at 0 hours 0 minutes 0 seconds in India is equivalent to 0 hours 15 minutes 0 seconds in Nepal. The Nepalese have turned their clocks 15 minutes faster than the Indians.
We will see what happens at 23:30 on December 31st, 1985 in Nepal, and then 1 hour later:
Duration_addTo_ex2.java

ZoneId nepalZoneId = ZoneId.of("Asia/Kathmandu");

// Create Temporal object.
ZonedDateTime zonedDateTime = ZonedDateTime.of(1985, 12, 31, 23, 30, 0, 0, nepalZoneId);
System.out.println("zonedDateTime: " + zonedDateTime); // 1985-12-31T23:30+05:30[Asia/Kathmandu]

// 1 Hour
Duration duration = Duration.ofHours(1);

ZonedDateTime newZonedDateTime = (ZonedDateTime) duration.addTo(zonedDateTime);
System.out.println("newZonedDateTime: " + newZonedDateTime); // 1986-01-01T00:45+05:45[Asia/Kathmandu]
Output:

zonedDateTime: 1985-12-31T23:30+05:30[Asia/Kathmandu]
newZonedDateTime: 1986-01-01T00:45+05:45[Asia/Kathmandu]

22- subtractFrom(Temporal)

Subtract this Duration from the specified Temporal object and return a copy of this Temporal object.

// Inherited from TemporalAmount interface
public Temporal subtractFrom(Temporal temporal)  
This method is equivalent to the Temporal.minus(TemporalAmount) method.

Temporal copiedTemporal = temporal.minus(duration);

Temporal copiedTemporal = duration.subtractFrom(temporal);
Example:
Duration_subtractFrom_ex1.java

// Create Temporal object.
LocalDateTime localDateTime = LocalDateTime.of(2000, 1, 1, 0, 30, 0);
System.out.println("localDateTime: " + localDateTime); // 2000-01-01T00:30

// 1 Hour
Duration duration = Duration.ofHours(1);

LocalDateTime newLocalDateTime = (LocalDateTime) duration.subtractFrom(localDateTime);
System.out.println("newLocalDateTime: " + newLocalDateTime); // 1999-12-31T23:30

23- multipliedBy(long)

Multiply this Duration with a specified value and return a new Duration. The returned result is normalized.

public Duration multipliedBy(long multiplicand)
Example:
Duration_multipliedBy_ex1.java

// 30 minutes 25 seconds
Duration duration1 = Duration.ofSeconds(30 * 60 + 25);
System.out.println("duration1: " + duration1); // PT30M25S

// duration2 = duration1 * 2
Duration duration2 = duration1.multipliedBy(2);
System.out.println("duration2: " + duration2); // PT1H50S (1 Hour 50 seconds)

24- dividedBy(long)

This Duration divides by a specified value and returns a new Duration. The returned result is normalized.

public Duration dividedBy(long divisor)
Example:
Duration_dividedBy_ex1.java

// 1 hour 30 minutes 25 seconds
Duration duration1 = Duration.ofSeconds(1 * 60 * 60 + 30 * 60 + 25);
System.out.println("duration1: " + duration1); // PT1H30M25S

// duration2 = duration1 / 2
Duration duration2 = duration1.dividedBy(2);
System.out.println("duration2: " + duration2); // PT45M12.5S (45 minutes 12.5 seconds)

25- dividedBy(Duration)

Divide this Duration by another Duration and return an integer.

public long dividedBy(Duration divisor)
Example:
Duration_dividedBy_ex2.java

// 1 hour 30 minutes 25 seconds
Duration duration1 = Duration.ofSeconds(1 * 60 * 60 + 30 * 60 + 25);
System.out.println("duration1: " + duration1); // PT1H30M25S
System.out.println("duration1.toNanos(): " + duration1.toNanos()); // 5425000000000

// 1 minute 30 seconds
Duration duration2 = Duration.ofSeconds(1 * 60 + 30);
System.out.println("duration2: " + duration2); // PT1M30S
System.out.println("duration2.toNanos(): " + duration2.toNanos()); // 90000000000

// duration1 / duration2
long result1 = duration1.dividedBy(duration2);
double result2 = (double) duration1.toNanos() / duration2.toNanos();
System.out.println("result1: " + result1); // 60
System.out.println("result2: " + result2); // 60.27777777777778

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.