Home Java LocalDateTime Methods getLong() Java LocalDateTime getLong() Method 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.
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 Call What 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.time.LocalDateTime
public long getLong(TemporalField field) Parameters Parameter Description field (TemporalField)The field to read, usually a ChronoField.
Return Value Return Value Description longThe value of the field.
Important
A field that a LocalDateTime does not have, such as OFFSET_SECONDS or INSTANT_SECONDS, still throws an UnsupportedTemporalTypeException. A LocalDateTime has no zone, so it cannot give you seconds since 1970. For that, convert it with atZone() or toEpochSecond(offset).
Compare with Similar Methods Ways to read part of a date-time:
Java Examples 1. Nanoseconds of the day 2. Days since 1970 3. get vs getLong Example 1 — Read the Nanoseconds Since Midnight Read NANO_OF_DAY at midnight, at 01:00, and one nanosecond after midnight.
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));
}
} midnight: 0
01:00: 3600000000000
tick: 1 How It Works Midnight is 0. No time has passed since the start of the day.One hour. An hour has 3,600 seconds, and a second has a billion nanoseconds, so 01:00 gives 3,600,000,000,000.One nanosecond. A time one nanosecond after midnight gives 1.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.
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));
}
} 1970-01-01: 0
1970-01-02: 1
2026-03-15: 20527 How It Works Day 0. The epoch day counts from 1 January 1970, so that day is 0.Day 1. The time of day does not matter, so 23:59 on 2 January is still day 1.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.
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");
}
}
} get(HOUR_OF_DAY): 14
getLong(HOUR_OF_DAY): 14
getLong(PROLEPTIC_MONTH): 24314
get(PROLEPTIC_MONTH) -> UnsupportedTemporalTypeException How It Works Normal fields agree. Both methods give 14 for the hour, one as an int and one as a long.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.get() refuses the big field. The range of this field does not fit into an int, so get() throws an UnsupportedTemporalTypeException.Frequently Asked Questions When should I use getLong() instead of get()? Use getLong() for fields whose values do not fit in an int, such as NANO_OF_DAY, EPOCH_DAY, and PROLEPTIC_MONTH.
How do I get the nanoseconds since midnight in Java? Use dt.getLong(ChronoField.NANO_OF_DAY).
How do I get the number of days since 1970? Use dt.getLong(ChronoField.EPOCH_DAY), or dt.toLocalDate().toEpochDay().
Can I get the Unix timestamp with getLong()? No. A LocalDateTime has no zone, so INSTANT_SECONDS is not available. Use dt.toEpochSecond(ZoneOffset.UTC) or dt.atZone(zone).toEpochSecond().
Does getLong() return the same value as get()? Yes, for every field that get() supports. The only difference is the return type.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion