Java LocalDateTime get() Method

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

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.

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

Parameters

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

Return Value

Return ValueDescription
intThe value of the field.

Compare with Similar Methods

Ways to read part of a date-time:

MethodHow it differs
getLong(field)Same idea, but returns a long.
getHour()A shortcut for get(ChronoField.HOUR_OF_DAY).
isSupported(field)Tells you if a field can be read.

Java Examples

Example 1 — Read Several Fields

Read the year, month, weekday, and two hour fields from a 14:30 time.

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

How It Works

  1. Fields are constants. Each ChronoField names one part of the date-time.
  2. 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.
  3. 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.
  4. 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.

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

How It Works

  1. Three tricky fields. NANO_OF_DAY and EPOCH_DAY are too big for an int. OFFSET_SECONDS does not exist in a LocalDateTime.
  2. get() throws for all three. Each failure is an UnsupportedTemporalTypeException, and the catch block prints the field name.
  3. 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.

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

How It Works

  1. Minutes since midnight. 14 hours and 30 minutes make 870 minutes.
  2. Seconds since midnight. The same time with 45 seconds gives 52,245.
  3. Day of the year. 15 March is day 74 of 2026.
  4. Smaller fractions. A quarter of a second is 250 milliseconds or 250,000 microseconds.

Frequently Asked Questions

Use dt.get(ChronoField.X), for example dt.get(ChronoField.HOUR_OF_DAY).
get() returns an int and fails for big fields. getLong() returns a long and works for every supported field.
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.
The field is either too big for an int (use getLong) or not part of a LocalDateTime, such as OFFSET_SECONDS.
Use dt.get(ChronoField.MINUTE_OF_DAY).

Next: getDayOfMonth()

Read the day of the month, from 1 to 31.

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