Java Date Time - Duration plusHours(long hoursToAdd) example








Duration plusHours(long hoursToAdd) returns a copy of this duration with the specified duration in hours added. This instance is immutable and unaffected by this method call.

Syntax

plusHours has the following syntax.

public Duration plusHours(long hoursToAdd)

Example

The following example shows how to use plusHours.

import java.time.Duration;
import java.time.LocalTime;
/*from   w  ww.j ava  2  s.com*/
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    System.out.println(duration.getSeconds());
    duration = duration.plusHours(4);
    System.out.println(duration.getSeconds());

  }
}

The code above generates the following result.