Home Java LocalDateTime Methods getNano() Java LocalDateTime getNano() Method Definition In Java, getNano() returns the fraction of the current second in nanoseconds . A nanosecond is one billionth of a second.
The value is only the fraction inside the second, from 0 to 999,999,999. It is not the number of nanoseconds since midnight.
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. It takes no arguments.
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30, 15, 250_000_000);
int nano = dt.getNano(); // 250000000
int millis = nano / 1_000_000; // 250 Call What it does dt.getNano()Returns the nanosecond of the second, for example 250000000. dt.getSecond()Returns the second of the minute, 0 to 59. dt.get(ChronoField.MILLI_OF_SECOND)Returns the milliseconds, 0 to 999. dt.getLong(ChronoField.NANO_OF_DAY)Returns the nanoseconds since midnight, as a long.
Divide by 1,000,000 to get milliseconds, and by 1,000 to get microseconds.
Library declarations // java.time.LocalDateTime
public int getNano() Parameters Parameter Description None getNano() takes no parameters.
Return Value Return Value Description intThe nanosecond of the second, from 0 to 999,999,999.
Important
The result is an int, so it can only hold the fraction of one second. The total nanoseconds since midnight do not fit into an int, which is why getLong(ChronoField.NANO_OF_DAY) exists. A time without a fraction gives 0.
Compare with Similar Methods Other ways to read part of a date-time:
Java Examples 1. Read the fraction 2. Millis and micros 3. Zero and small values Example 1 — Read the Nanoseconds of a Time Read the nanosecond value of a time with a fraction.
import java.time.LocalDateTime;
public class LdtGetNanoDemo {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30, 15, 250_000_000);
System.out.println("time: " + dt);
System.out.println("nano: " + dt.getNano());
}
} time: 2026-03-15T10:30:15.250
nano: 250000000 How It Works A time with a fraction. The value 250_000_000 is a quarter of a second.Printed as .250. The text shows the fraction as three digits.getNano returns the number. It prints 250000000, the same value in nanoseconds.Example 2 — Get Milliseconds and Microseconds Divide the nanoseconds to get smaller units.
import java.time.LocalDateTime;
public class LdtGetNanoUnits {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30, 15, 123_456_789);
int nano = dt.getNano();
System.out.println("nanoseconds: " + nano);
System.out.println("microseconds: " + nano / 1_000);
System.out.println("milliseconds: " + nano / 1_000_000);
}
} nanoseconds: 123456789
microseconds: 123456
milliseconds: 123 How It Works A precise fraction. The time has .123456789 seconds.Microseconds. Dividing by 1,000 drops the last three digits, giving 123456.Milliseconds. Dividing by 1,000,000 keeps the first three digits, giving 123.Example 3 — See What Zero and Small Values Mean Read the nanoseconds of a time without a fraction, and of the same time after adding 1,500 nanoseconds.
import java.time.LocalDateTime;
public class LdtGetNanoZero {
public static void main(String[] args) {
LocalDateTime whole = LocalDateTime.of(2026, 3, 15, 10, 30, 15);
LocalDateTime tiny = whole.plusNanos(1_500);
System.out.println("whole second: " + whole.getNano());
System.out.println("plus 1500 ns: " + tiny.getNano());
System.out.println("printed as: " + tiny);
}
} whole second: 0
plus 1500 ns: 1500
printed as: 2026-03-15T10:30:15.000001500 How It Works No fraction. A time on a whole second returns 0.Add 1,500 nanoseconds. That is 1.5 microseconds, so getNano() returns 1500.It prints with nine digits. 1,500 nanoseconds is not a whole number of microseconds, so the text shows .000001500.Frequently Asked Questions How do I get the nanosecond from a LocalDateTime in Java? Call dt.getNano(). It returns a number from 0 to 999,999,999.
Does getNano() change the LocalDateTime? No. It only reads a value. LocalDateTime is immutable, so no method can change it.
How do I get the milliseconds from a LocalDateTime in Java? Use dt.get(ChronoField.MILLI_OF_SECOND), or dt.getNano() / 1_000_000.
How many nanoseconds are in one second? 1,000,000,000. getNano() never returns that value, because the largest fraction is 999,999,999.
Why does getNano() return an int and not a long? It is only the fraction of one second, which always fits in an int. For nanoseconds since midnight, use getLong(ChronoField.NANO_OF_DAY).
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.
13 people found this page helpful
Helpful Share Copy link Suggestion