Java Date Time - Duration minusHours(long hoursToSubtract) example








Duration minusHours(long hoursToSubtract) returns a copy of this duration with the specified duration in hours subtracted.

Syntax

minusHours has the following syntax.

public Duration minusHours(long hoursToSubtract)

Example

The following example shows how to use minusHours.

import java.time.Duration;
import java.time.LocalTime;
/*from   w w  w.java 2s .  c  om*/
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    System.out.println(duration.getSeconds());
    duration = duration.minusHours(3);
    System.out.println(duration.getSeconds());

  }
}

The code above generates the following result.