Java Date Time - Duration withNanos(int nanoOfSecond) example








Duration withNanos(int nanoOfSecond) returns a copy of this duration with the specified nano-of-second.

Syntax

withNanos has the following syntax.

public Duration withNanos(int nanoOfSecond)

Example

The following example shows how to use withNanos.

import java.time.Duration;
import java.time.LocalTime;
//from  www . ja va 2 s.c o  m
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    duration = duration.withNanos(1000);
    System.out.println(duration.getNano());

  }
}

The code above generates the following result.