Java LocalDateTime getLong() Method

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

Definition

In Java, getLong(field) reads one field of a LocalDateTime and returns it as a long. It works for every field that the date-time supports.

Use it instead of get() for the fields whose values are too big for an int, such as the nanoseconds since midnight or the days since 1970.

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. It comes from TemporalAccessor, which LocalDateTime implements.

Syntax

Pass a ChronoField constant.

Java
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 1, 0);
long nanos = dt.getLong(ChronoField.NANO_OF_DAY);        // 3600000000000
long epochDay = dt.getLong(ChronoField.EPOCH_DAY);       // days since 1970-01-01
CallWhat it does
dt.getLong(ChronoField.NANO_OF_DAY)The nanoseconds since midnight.
dt.getLong(ChronoField.EPOCH_DAY)The number of days since 1 January 1970.
dt.getLong(ChronoField.PROLEPTIC_MONTH)The number of months since year 0.
dt.get(field)The same, but returns an int and fails for big fields.

Every field that get() can read, getLong() can read too. The opposite is not true.

Library declarations
Java
// java.time.LocalDateTime
public long getLong(TemporalField field)

Parameters

ParameterDescription
field (TemporalField)The field to read, usually a ChronoField.

Return Value

Return ValueDescription
longThe value of the field.

Compare with Similar Methods

Ways to read part of a date-time:

MethodHow it differs
get(field)Same idea, but returns an int.
getNano()The fraction of the current second.
isSupported(field)Tells you if a field can be read.

Java Examples

Example 1 — Read the Nanoseconds Since Midnight

Read NANO_OF_DAY at midnight, at 01:00, and one nanosecond after midnight.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;

public class LdtGetLongDemo {
    public static void main(String[] args) {
        LocalDateTime midnight = LocalDateTime.of(2026, 3, 15, 0, 0);
        LocalDateTime oneAm = LocalDateTime.of(2026, 3, 15, 1, 0);
        LocalDateTime tick = LocalDateTime.of(2026, 3, 15, 0, 0, 0, 1);

        System.out.println("midnight: " + midnight.getLong(ChronoField.NANO_OF_DAY));
        System.out.println("01:00:    " + oneAm.getLong(ChronoField.NANO_OF_DAY));
        System.out.println("tick:     " + tick.getLong(ChronoField.NANO_OF_DAY));
    }
}

How It Works

  1. Midnight is 0. No time has passed since the start of the day.
  2. One hour. An hour has 3,600 seconds, and a second has a billion nanoseconds, so 01:00 gives 3,600,000,000,000.
  3. One nanosecond. A time one nanosecond after midnight gives 1.
  4. Too big for int. The second number is larger than an int can hold, which is why getLong() is needed.

Example 2 — Count the Days Since the Epoch

Read EPOCH_DAY for 1 January 1970, the next day, and 15 March 2026.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;

public class LdtGetLongEpochDay {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(1970, 1, 1, 0, 0);
        LocalDateTime next = LocalDateTime.of(1970, 1, 2, 23, 59);
        LocalDateTime march = LocalDateTime.of(2026, 3, 15, 10, 30);

        System.out.println("1970-01-01: " + start.getLong(ChronoField.EPOCH_DAY));
        System.out.println("1970-01-02: " + next.getLong(ChronoField.EPOCH_DAY));
        System.out.println("2026-03-15: " + march.getLong(ChronoField.EPOCH_DAY));
    }
}

How It Works

  1. Day 0. The epoch day counts from 1 January 1970, so that day is 0.
  2. Day 1. The time of day does not matter, so 23:59 on 2 January is still day 1.
  3. A modern date. 15 March 2026 is day 20,527, which is more than 56 years of days.

Example 3 — Compare get() and getLong()

Read a normal field with both methods, and a big field with each.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.UnsupportedTemporalTypeException;

public class LdtGetLongCompare {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);

        System.out.println("get(HOUR_OF_DAY):         " + dt.get(ChronoField.HOUR_OF_DAY));
        System.out.println("getLong(HOUR_OF_DAY):     " + dt.getLong(ChronoField.HOUR_OF_DAY));
        System.out.println("getLong(PROLEPTIC_MONTH): " + dt.getLong(ChronoField.PROLEPTIC_MONTH));
        try {
            dt.get(ChronoField.PROLEPTIC_MONTH);
        } catch (UnsupportedTemporalTypeException e) {
            System.out.println("get(PROLEPTIC_MONTH) -> UnsupportedTemporalTypeException");
        }
    }
}

How It Works

  1. Normal fields agree. Both methods give 14 for the hour, one as an int and one as a long.
  2. PROLEPTIC_MONTH counts months. It is the year times 12 plus the month minus 1, here 2026 times 12 plus 2, which is 24,314.
  3. get() refuses the big field. The range of this field does not fit into an int, so get() throws an UnsupportedTemporalTypeException.

Frequently Asked Questions

Use getLong() for fields whose values do not fit in an int, such as NANO_OF_DAY, EPOCH_DAY, and PROLEPTIC_MONTH.
Use dt.getLong(ChronoField.NANO_OF_DAY).
Use dt.getLong(ChronoField.EPOCH_DAY), or dt.toLocalDate().toEpochDay().
No. A LocalDateTime has no zone, so INSTANT_SECONDS is not available. Use dt.toEpochSecond(ZoneOffset.UTC) or dt.atZone(zone).toEpochSecond().
Yes, for every field that get() supports. The only difference is the return type.

Next: getMinute()

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

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

9 people found this page helpful