Java LocalDateTime withNano() Method

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

Definition

In Java, withNano(nanoOfSecond) returns a new LocalDateTime with the nanosecond set to the value you pass. The date, the hour, the minute, and the second are copied from the original.

LocalDateTime is immutable, so the original object never changes. Always keep the result: dt = dt.withNano(123_000_000);. The "nano of second" is the fraction of the current second, not the total time.

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 new nanosecond.

Java
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.withNano(123_000_000);   // 2026-03-15T10:30:00.123
System.out.println(start);   // 2026-03-15T10:30
CallWhat it does
dt.withNano(n)Returns a copy with the nanosecond replaced by n.
dt.with(ChronoField.NANO_OF_SECOND, n)The general form for any field. It does the same thing here.
dt.plusNanos(n)Moves by an amount instead of setting a value.
time.withNano(n)LocalTime has the same method, but it has no date.

The result is a new object. Everything except the nanosecond is copied from the original.

Library declarations
Java
// java.time.LocalDateTime
public LocalDateTime withNano(int nanoOfSecond)

Parameters

ParameterDescription
nanoOfSecond (int)The new nanosecond of the second, from 0 to 999,999,999.

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the nanosecond replaced. The original is not changed.

Compare with Similar Methods

Ways to change one part of a date-time:

MethodHow it differs
with(field, value)The general method: you choose the field as an argument.
plusNanos(n)Adds nanoseconds to the time instead of setting the fraction.
truncatedTo(unit)Drops all the parts below a unit in one call.

Java Examples

Example 1 — Set a Fraction of a Second

Take 10:30:15 and set the fraction to 250 milliseconds.

Java
import java.time.LocalDateTime;

public class LdtWithNanoDemo {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30, 15);
        LocalDateTime fraction = start.withNano(250_000_000);

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

How It Works

  1. The start. 10:30:15 has no fraction.
  2. 250 milliseconds. A millisecond is 1,000,000 nanoseconds, so 250 milliseconds is 250_000_000.
  3. The seconds stay. The result is 10:30:15.250.

Example 2 — Remove the Fraction of a Second

Take a time with a nine-digit fraction and set the nanoseconds to 0.

Java
import java.time.LocalDateTime;

public class LdtWithNanoDrop {
    public static void main(String[] args) {
        LocalDateTime precise = LocalDateTime.of(2026, 3, 15, 10, 30, 15, 987_654_321);

        System.out.println("before: " + precise);
        System.out.println("after:  " + precise.withNano(0));
    }
}

How It Works

  1. A very precise time. The fraction .987654321 uses all nine digits.
  2. Set the fraction to 0. withNano(0) removes it completely.
  3. The seconds are not rounded. The result is 10:30:15, not 10:30:16. The fraction is simply dropped.

Example 3 — Try the Largest and Too Large Values

Set the fraction to 999,999,999 and to 1,000,000,000.

Java
import java.time.DateTimeException;
import java.time.LocalDateTime;

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

        System.out.println("999999999 -> " + start.withNano(999_999_999));
        try {
            start.withNano(1_000_000_000);
        } catch (DateTimeException e) {
            System.out.println("1000000000 -> DateTimeException");
        }
    }
}

How It Works

  1. The largest valid value. 999_999_999 is the last nanosecond inside the second, so the result is 10:30:15.999999999.
  2. One more is too much. A billion nanoseconds is a whole second, so withNano refuses it.
  3. A DateTimeException is thrown. The catch block prints its name.

Frequently Asked Questions

Call dt.withNano(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withNano() returns a new object and leaves the original as it was.
A value below 0 or above 999,999,999 throws a DateTimeException, because a second has only a billion nanoseconds.
Multiply by 1,000,000 and use withNano. For example dt.withNano(250_000_000) sets 250 milliseconds.
Use dt.withNano(0) or dt.truncatedTo(ChronoUnit.SECONDS).

Next: withSecond()

Return a copy with a different second.

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

12 people found this page helpful