Home Java LocalDateTime Methods get() Java LocalDateTime get() Method Definition In Java, get(field) reads one field of a LocalDateTime and returns it as an int. A field is a named part of a date-time, listed in the ChronoField enum, such as YEAR, HOUR_OF_DAY, or MINUTE_OF_DAY.
The methods getYear(), getHour() and others are shortcuts for the most common fields. get() reaches all the others.
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, 14, 30);
int year = dt.get(ChronoField.YEAR); // 2026
int hour = dt.get(ChronoField.HOUR_OF_DAY); // 14
int minuteOfDay = dt.get(ChronoField.MINUTE_OF_DAY); // 870 Call What it does dt.get(ChronoField.YEAR)Reads the year as an int. dt.get(ChronoField.HOUR_OF_AMPM)Reads the hour on a 12-hour clock, 0 to 11. dt.getLong(field)The same, but returns a long, so it also works for very large fields. dt.isSupported(field)Checks first whether the field can be read.
get() works only for fields whose value fits in an int. Use getLong() for the rest.
Library declarations // java.time.LocalDateTime
public int get(TemporalField field) Parameters Parameter Description field (TemporalField)The field to read, usually a ChronoField such as ChronoField.YEAR.
Return Value Return Value Description intThe value of the field.
Important
Some fields are too big for an int, such as NANO_OF_DAY and EPOCH_DAY. For these, get() throws an UnsupportedTemporalTypeException, and you should call getLong(). Fields that a LocalDateTime does not have at all, such as OFFSET_SECONDS, throw the same exception.
Compare with Similar Methods Ways to read part of a date-time:
Java Examples 1. Read fields 2. Fields that do not fit 3. Handy fields Example 1 — Read Several Fields Read the year, month, weekday, and two hour fields from a 14:30 time.
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LdtGetDemo {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30, 45);
System.out.println("YEAR: " + dt.get(ChronoField.YEAR));
System.out.println("MONTH_OF_YEAR: " + dt.get(ChronoField.MONTH_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + dt.get(ChronoField.DAY_OF_WEEK));
System.out.println("HOUR_OF_DAY: " + dt.get(ChronoField.HOUR_OF_DAY));
System.out.println("HOUR_OF_AMPM: " + dt.get(ChronoField.HOUR_OF_AMPM));
System.out.println("AMPM_OF_DAY: " + dt.get(ChronoField.AMPM_OF_DAY));
}
} YEAR: 2026
MONTH_OF_YEAR: 3
DAY_OF_WEEK: 7
HOUR_OF_DAY: 14
HOUR_OF_AMPM: 2
AMPM_OF_DAY: 1 How It Works Fields are constants. Each ChronoField names one part of the date-time.Date fields. The year is 2026, the month is 3, and the weekday is 7 because Sunday is the seventh day of the ISO week.Hour on two clocks. HOUR_OF_DAY is 14, and HOUR_OF_AMPM is 2 because 14:00 is 2 o'clock in the afternoon.AM or PM. AMPM_OF_DAY is 0 for the morning and 1 for the afternoon, so 14:30 gives 1.Example 2 — See Which Fields get() Refuses Try fields that are too big for an int, and a field that a LocalDateTime does not have.
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.UnsupportedTemporalTypeException;
public class LdtGetTooBig {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);
ChronoField[] fields = {ChronoField.NANO_OF_DAY, ChronoField.EPOCH_DAY, ChronoField.OFFSET_SECONDS};
for (ChronoField field : fields) {
try {
dt.get(field);
} catch (UnsupportedTemporalTypeException e) {
System.out.println(field + " -> UnsupportedTemporalTypeException");
}
}
System.out.println("getLong(NANO_OF_DAY) = " + dt.getLong(ChronoField.NANO_OF_DAY));
}
} NanoOfDay -> UnsupportedTemporalTypeException
EpochDay -> UnsupportedTemporalTypeException
OffsetSeconds -> UnsupportedTemporalTypeException
getLong(NANO_OF_DAY) = 52200000000000 How It Works Three tricky fields. NANO_OF_DAY and EPOCH_DAY are too big for an int. OFFSET_SECONDS does not exist in a LocalDateTime.get() throws for all three. Each failure is an UnsupportedTemporalTypeException, and the catch block prints the field name.getLong() is the fix for big fields. NANO_OF_DAY for 14:30 is 52,200,000,000,000, a number that needs a long.Example 3 — Use Fields with No Shortcut Method Read minutes since midnight, seconds since midnight, the day of the year, and the milli- and microseconds.
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LdtGetHandy {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30, 45, 250_000_000);
System.out.println("MINUTE_OF_DAY: " + dt.get(ChronoField.MINUTE_OF_DAY));
System.out.println("SECOND_OF_DAY: " + dt.get(ChronoField.SECOND_OF_DAY));
System.out.println("DAY_OF_YEAR: " + dt.get(ChronoField.DAY_OF_YEAR));
System.out.println("MILLI_OF_SECOND: " + dt.get(ChronoField.MILLI_OF_SECOND));
System.out.println("MICRO_OF_SECOND: " + dt.get(ChronoField.MICRO_OF_SECOND));
}
} MINUTE_OF_DAY: 870
SECOND_OF_DAY: 52245
DAY_OF_YEAR: 74
MILLI_OF_SECOND: 250
MICRO_OF_SECOND: 250000 How It Works Minutes since midnight. 14 hours and 30 minutes make 870 minutes.Seconds since midnight. The same time with 45 seconds gives 52,245.Day of the year. 15 March is day 74 of 2026.Smaller fractions. A quarter of a second is 250 milliseconds or 250,000 microseconds.Frequently Asked Questions How do I get a field of a LocalDateTime in Java? Use dt.get(ChronoField.X), for example dt.get(ChronoField.HOUR_OF_DAY).
What is the difference between get() and getLong()? get() returns an int and fails for big fields. getLong() returns a long and works for every supported field.
What is ChronoField? It is an enum in java.time.temporal that lists the parts of a date-time, such as YEAR, DAY_OF_WEEK, and MINUTE_OF_DAY.
Why do I get UnsupportedTemporalTypeException? The field is either too big for an int (use getLong) or not part of a LocalDateTime, such as OFFSET_SECONDS.
How do I get the minutes since midnight in Java? Use dt.get(ChronoField.MINUTE_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