Java Date Time - Duration plus(long amountToAdd, TemporalUnit unit) example








Duration plus(long amountToAdd, TemporalUnit unit) returns a copy of this duration with the specified duration added. The duration amount is measured in terms of the specified unit.

Syntax

plus has the following syntax.

public Duration plus(long amountToAdd,  TemporalUnit unit)

Example

The following example shows how to use plus.

import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
/*from  w w  w  . j  a v a2 s  . co  m*/
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    System.out.println(duration.getSeconds());
    duration = duration.plus(4,ChronoUnit.DAYS);
    System.out.println(duration.getSeconds());

  }
}

The code above generates the following result.