Home Java LocalDateTime Methods withNano() Java LocalDateTime withNano() Method 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.
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 Call What 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.time.LocalDateTime
public LocalDateTime withNano(int nanoOfSecond) Parameters Parameter Description nanoOfSecond (int)The new nanosecond of the second, from 0 to 999,999,999.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the nanosecond replaced. The original is not changed.
Important
The value is the fraction of one second . To set 250 milliseconds, pass 250_000_000. To drop the fraction completely, pass 0. For a one-call way to drop the fraction, use truncatedTo(ChronoUnit.SECONDS).
Compare with Similar Methods Ways to change one part of a date-time:
Java Examples 1. Set milliseconds 2. Drop the fraction 3. Largest value Example 1 — Set a Fraction of a Second Take 10:30:15 and set the fraction to 250 milliseconds.
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);
}
} start: 2026-03-15T10:30:15
fraction: 2026-03-15T10:30:15.250 How It Works The start. 10:30:15 has no fraction.250 milliseconds. A millisecond is 1,000,000 nanoseconds, so 250 milliseconds is 250_000_000.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.
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));
}
} before: 2026-03-15T10:30:15.987654321
after: 2026-03-15T10:30:15 How It Works A very precise time. The fraction .987654321 uses all nine digits.Set the fraction to 0. withNano(0) removes it completely.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.
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");
}
}
} 999999999 -> 2026-03-15T10:30:15.999999999
1000000000 -> DateTimeException How It Works The largest valid value. 999_999_999 is the last nanosecond inside the second, so the result is 10:30:15.999999999.One more is too much. A billion nanoseconds is a whole second, so withNano refuses it.A DateTimeException is thrown. The catch block prints its name.Frequently Asked Questions How do I change only the nanosecond of a LocalDateTime in Java? Call dt.withNano(value). It returns a new LocalDateTime with that part replaced and everything else copied.
Does withNano() change the original LocalDateTime? No. LocalDateTime is immutable, so withNano() returns a new object and leaves the original as it was.
What happens if the value passed to withNano() is not valid? A value below 0 or above 999,999,999 throws a DateTimeException, because a second has only a billion nanoseconds.
How do I set the milliseconds of a LocalDateTime in Java? Multiply by 1,000,000 and use withNano. For example dt.withNano(250_000_000) sets 250 milliseconds.
How do I remove the milliseconds from a LocalDateTime? Use dt.withNano(0) or dt.truncatedTo(ChronoUnit.SECONDS).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion