Java Date Time - Duration minus(long amountToSubtract, TemporalUnit unit) example








Duration minus(long amountToSubtract, TemporalUnit unit) returns a copy of this duration with the specified duration subtracted.

Syntax

minus has the following syntax.

public Duration minus(long amountToSubtract,  TemporalUnit unit)

Example

The following example shows how to use minus.

import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
/* w w  w .ja v a  2  s.c o  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.minus(3,ChronoUnit.DAYS);
    System.out.println(duration.getSeconds());

  }
}

The code above generates the following result.