Java Date Time - Duration withSeconds(long seconds) example








Duration withSeconds(long seconds) returns a copy of this duration with the specified amount of seconds.

Syntax

withSeconds has the following syntax.

public Duration withSeconds(long seconds)

Example

The following example shows how to use withSeconds.

import java.time.Duration;
import java.time.LocalTime;
/*w  w  w  . j  a v a 2  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.withSeconds(1000);
    System.out.println(duration.getSeconds());
  }
}

The code above generates the following result.