Java LocalDateTime getNano() Method

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

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.

Java
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
CallWhat 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
// java.time.LocalDateTime
public int getNano()

Parameters

ParameterDescription
NonegetNano() takes no parameters.

Return Value

Return ValueDescription
intThe nanosecond of the second, from 0 to 999,999,999.

Compare with Similar Methods

Other ways to read part of a date-time:

MethodHow it differs
getSecond()Reads the whole seconds.
getLong(field)Reads a field as a long, such as NANO_OF_DAY.
withNano(n)Sets the fraction instead of reading it.

Java Examples

Example 1 — Read the Nanoseconds of a Time

Read the nanosecond value of a time with a fraction.

Java
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());
    }
}

How It Works

  1. A time with a fraction. The value 250_000_000 is a quarter of a second.
  2. Printed as .250. The text shows the fraction as three digits.
  3. 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.

Java
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);
    }
}

How It Works

  1. A precise fraction. The time has .123456789 seconds.
  2. Microseconds. Dividing by 1,000 drops the last three digits, giving 123456.
  3. 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.

Java
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);
    }
}

How It Works

  1. No fraction. A time on a whole second returns 0.
  2. Add 1,500 nanoseconds. That is 1.5 microseconds, so getNano() returns 1500.
  3. It prints with nine digits. 1,500 nanoseconds is not a whole number of microseconds, so the text shows .000001500.

Frequently Asked Questions

Call dt.getNano(). It returns a number from 0 to 999,999,999.
No. It only reads a value. LocalDateTime is immutable, so no method can change it.
Use dt.get(ChronoField.MILLI_OF_SECOND), or dt.getNano() / 1_000_000.
1,000,000,000. getNano() never returns that value, because the largest fraction is 999,999,999.
It is only the fraction of one second, which always fits in an int. For nanoseconds since midnight, use getLong(ChronoField.NANO_OF_DAY).

Next: getSecond()

Read the second of the minute, from 0 to 59.

getSecond() 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