plusNanos()
is an instance method of the Duration
class that is used to add the specified duration (in nanoseconds) to the Duration
object.
The plusNanos()
method is defined in the Duration
class. The Duration
class is defined in the java.time
package. To import the Duration
class, use the import statement below.
import java.time.Duration;
public Duration plusNanos(long nanosToAdd)
long nanosToAdd
: The number of nanoseconds to add; this parameter can be positive or negative.This method returns a new instance of the Duration
class, with the number of nanoseconds added.
In the code below, we add 100000
nanoseconds to the baseDuration
object with the help of the plusNanos()
method and print the new object to the console.
import java.time.Duration;public class Main{public static void main(String[] args) {Duration baseDuration = Duration.ofSeconds(3);int nanoSecondsToAdd = 100000;Duration newDuration = baseDuration.plusNanos(nanoSecondsToAdd);System.out.printf("%s + %s nanoseconds = %s", baseDuration, nanoSecondsToAdd, newDuration);System.out.println();}}