Java LocalDateTime minusNanos() Method

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

Definition

In Java, minusNanos(nanos) returns a new LocalDateTime that is the given number of nanoseconds earlier 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.minusNanos(1);. When the nanoseconds go below zero, the seconds go down 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, 15);
LocalDateTime result = start.minusNanos(1);   // 2026-03-15T10:30:14.999999999
System.out.println(start);   // 2026-03-15T10:30:15
CallWhat it does
dt.minusNanos(n)Returns a new date-time with n nanoseconds subtracted.
dt.plusNanos(n)The same idea in the opposite direction.
dt.minus(n, ChronoUnit.NANOS)The general form. It does the same thing for this unit.
time.minusNanos(n)LocalTime has the same method, but it wraps around at midnight and has no date.

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

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

Parameters

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

Return Value

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

Compare with Similar Methods

Methods that move a date-time in time:

MethodHow it differs
plusNanos(n)The opposite direction for nanoseconds.
minus(n, unit)The general method: you choose the unit as an argument.
minusSeconds(n)Subtracts whole seconds.
withNano(n)Replaces the nanosecond part instead of subtracting from it.

Java Examples

Example 1 — Go Back by One Nanosecond

Subtract one nanosecond and half a second from 10:30:15.

Java
import java.time.LocalDateTime;

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

        System.out.println("base:        " + base);
        System.out.println("minus 1 ns:  " + base.minusNanos(1));
        System.out.println("minus 0.5 s: " + base.minusNanos(500_000_000));
    }
}

How It Works

  1. The base. 10:30:15 with no fraction.
  2. One nanosecond back. The time falls into the previous second, 10:30:14, with a fraction of .999999999.
  3. Half a second back. 500_000_000 nanoseconds gives 10:30:14.500.

Example 2 — Subtract Smaller and Smaller Steps

Start at 10:30:15.500 and subtract one millisecond, one microsecond, and one nanosecond.

Java
import java.time.LocalDateTime;

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

        System.out.println("base:          " + base);
        System.out.println("minus 1 ms:    " + base.minusNanos(1_000_000));
        System.out.println("minus 1 us:    " + base.minusNanos(1_000));
        System.out.println("minus 1 ns:    " + base.minusNanos(1));
    }
}

How It Works

  1. The base. The fraction .500 is half a second.
  2. One millisecond. 1,000,000 nanoseconds leaves .499, which needs three digits.
  3. One microsecond. 1,000 nanoseconds leaves .499999, which needs six digits.
  4. One nanosecond. The fraction needs all nine digits: .499999999.

Example 3 — Roll Back into the Previous Minute

Subtract one nanosecond from 10:31, and compare a billion nanoseconds with one second.

Java
import java.time.LocalDateTime;

public class LdtMinusNanosRollover {
    public static void main(String[] args) {
        LocalDateTime minute = LocalDateTime.of(2026, 3, 15, 10, 31);

        System.out.println("before: " + minute);
        System.out.println("after:  " + minute.minusNanos(1));
        System.out.println("1e9 nanos equals 1 second: "
                + minute.minusNanos(1_000_000_000).equals(minute.minusSeconds(1)));
    }
}

How It Works

  1. The start of a minute. 10:31 means 10:31:00.000000000.
  2. One nanosecond earlier. The result is the very last instant of the previous minute: 10:30:59.999999999.
  3. A billion nanoseconds is a second. minusNanos(1_000_000_000) equals minusSeconds(1), so the last line prints true.

Frequently Asked Questions

Call dt.minusNanos(n). It returns a new LocalDateTime, so store it: dt = dt.minusNanos(n).
No. LocalDateTime is immutable, so minusNanos() returns a new object and leaves the original as it was.
Yes. A negative number moves the other way, so dt.minusNanos(-n) gives the same result as dt.plusNanos(n).
10:30:59.999999999, the last instant of the previous minute.
No. Both move the time back by n nanoseconds.

Next: minusSeconds()

Return a copy that is a number of seconds earlier.

minusSeconds() 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.

8 people found this page helpful