Java LocalDateTime plusNanos() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, plusNanos(nanos) returns a new LocalDateTime that is the given number of nanoseconds later than the original. A nanosecond is one billionth of a second, the smallest step a LocalDateTime can hold.

LocalDateTime is immutable, so the original object never changes. Always keep the result: dt = dt.plusNanos(1);. When the nanoseconds pass 999,999,999, the seconds go up by one.

An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file.

Syntax

Call it on a LocalDateTime and pass the number of nanoseconds. A negative number goes the other way.

Java
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.plusNanos(500_000_000);   // 2026-03-15T10:30:00.500
System.out.println(start);   // 2026-03-15T10:30
CallWhat it does
dt.plusNanos(n)Returns a new date-time with n nanoseconds added.
dt.minusNanos(n)The same idea in the opposite direction.
dt.plus(n, ChronoUnit.NANOS)The general form. It does the same thing for this unit.
time.plusNanos(n)LocalTime has the same method, but it wraps around at midnight and has no date.

The result is a new object. Calling plusNanos() without using the returned value does nothing useful.

Library declarations
Java
// java.time.LocalDateTime
public LocalDateTime plusNanos(long nanos)

Parameters

ParameterDescription
nanos (long)The number of nanoseconds to add. It can be zero or negative.

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the nanoseconds added. The original is not changed.

Compare with Similar Methods

Methods that move a date-time in time:

MethodHow it differs
minusNanos(n)The opposite direction for nanoseconds.
plus(n, unit)The general method: you choose the unit as an argument.
plusSeconds(n)Adds whole seconds.
withNano(n)Replaces the nanosecond part instead of adding to it.

Java Examples

Example 1 — Add Half a Second

Add 500,000,000 nanoseconds, which is half a second, to 10:30.

Java
import java.time.LocalDateTime;

public class LdtPlusNanosDemo {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime later = start.plusNanos(500_000_000);

        System.out.println("start: " + start);
        System.out.println("later: " + later);
    }
}

How It Works

  1. The start. 10:30 on 15 March 2026.
  2. Half a second. 500_000_000 nanoseconds is 0.5 seconds. The underscores only make the number easier to read.
  3. A fraction appears. The result prints as 10:30:00.500, so the zero seconds are shown once there is a fraction.

Example 2 — See How the Fraction Prints

Add 1, 1,000, and 1,000,000 nanoseconds and look at the number of digits that Java prints.

Java
import java.time.LocalDateTime;

public class LdtPlusNanosPrint {
    public static void main(String[] args) {
        LocalDateTime base = LocalDateTime.of(2026, 3, 15, 10, 30, 15);

        System.out.println("+1 nano:       " + base.plusNanos(1));
        System.out.println("+1000 nanos:   " + base.plusNanos(1_000));
        System.out.println("+1000000 nanos: " + base.plusNanos(1_000_000));
    }
}

How It Works

  1. One nanosecond. The fraction needs all nine digits: .000000001.
  2. One microsecond. 1,000 nanoseconds fit in six digits, so Java prints .000001.
  3. One millisecond. 1,000,000 nanoseconds fit in three digits, so Java prints .001.
  4. Shortest form. Java prints 3, 6, or 9 fraction digits, whichever is the shortest that is exact.

Example 3 — Roll Nanoseconds into the Next Second

Add one nanosecond to the last nanosecond of a minute, and compare a billion nanoseconds with one second.

Java
import java.time.LocalDateTime;

public class LdtPlusNanosRollover {
    public static void main(String[] args) {
        LocalDateTime almost = LocalDateTime.of(2026, 3, 15, 10, 30, 59, 999_999_999);

        System.out.println("before: " + almost);
        System.out.println("after:  " + almost.plusNanos(1));
        System.out.println("1e9 nanos equals 1 second: "
                + almost.plusNanos(1_000_000_000).equals(almost.plusSeconds(1)));
    }
}

How It Works

  1. The last nanosecond of the minute. The time is 10:30:59.999999999.
  2. One more nanosecond. The fraction goes back to zero and the minute moves on, giving 10:31.
  3. A billion nanoseconds is a second. plusNanos(1_000_000_000) equals plusSeconds(1), so the last line prints true.

Frequently Asked Questions

Call dt.plusNanos(n). It returns a new LocalDateTime, so store it: dt = dt.plusNanos(n).
No. LocalDateTime is immutable, so plusNanos() returns a new object and leaves the original as it was.
Yes. A negative number moves the other way, so dt.plusNanos(-n) gives the same result as dt.minusNanos(n).
1,000,000. So plusNanos(1_000_000) adds one millisecond.
The type can hold nanoseconds, but the real precision depends on the system clock. On most computers it is microseconds or milliseconds.

Next: plusSeconds()

Return a copy that is a number of seconds later.

plusSeconds() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful