Java LocalDateTime getYear() Method

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

Definition

In Java, getYear() returns the year of a LocalDateTime as a plain int, such as 2026.

It reads the proleptic ISO year, so year 0 and negative years are possible. For normal dates you simply get the usual four-digit number.

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);
int year = dt.getYear();   // 2026
CallWhat it does
dt.getYear()Returns the year, for example 2026.
dt.getMonthValue()Returns the month as a number, 1 to 12.
dt.getDayOfMonth()Returns the day of the month, 1 to 31.
dt.get(ChronoField.YEAR)The general form: it gives the same number.

The result is a primitive int, so you can use it in a calculation straight away.

Library declarations
Java
// java.time.LocalDateTime
public int getYear()

Parameters

ParameterDescription
NonegetYear() takes no parameters.

Return Value

Return ValueDescription
intThe year, from -999999999 to 999999999.

Compare with Similar Methods

Other ways to read part of a date-time:

MethodHow it differs
getMonthValue()Reads the month number.
getDayOfMonth()Reads the day of the month.
withYear(y)Sets the year instead of reading it.

Java Examples

Example 1 — Read the Year and Do Some Math

Read the year of two dates and use the numbers in a simple calculation.

Java
import java.time.LocalDateTime;

public class LdtGetYearDemo {
    public static void main(String[] args) {
        LocalDateTime born = LocalDateTime.of(1990, 7, 4, 8, 0);
        LocalDateTime now = LocalDateTime.of(2026, 3, 15, 10, 30);

        System.out.println("born in: " + born.getYear());
        System.out.println("now:     " + now.getYear());
        System.out.println("rough age: " + (now.getYear() - born.getYear()));
    }
}

How It Works

  1. Two date-times. One in 1990 and one in 2026.
  2. getYear returns an int. The first two lines print 1990 and 2026.
  3. A rough age. Subtracting the two years gives 36, but the person is only 35 until 4 July, so this ignores the month and the day.

Example 2 — Test for a Leap Year

Use the year number with the leap-year rule for three different dates.

Java
import java.time.LocalDateTime;

public class LdtGetYearLeap {
    public static void main(String[] args) {
        LocalDateTime[] dates = {
            LocalDateTime.of(2024, 1, 1, 0, 0),
            LocalDateTime.of(2100, 1, 1, 0, 0),
            LocalDateTime.of(2000, 1, 1, 0, 0)
        };

        for (LocalDateTime dt : dates) {
            int y = dt.getYear();
            boolean leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
            System.out.println(y + " is a leap year: " + leap);
        }
    }
}

How It Works

  1. Three dates. They are in 2024, 2100, and 2000.
  2. The loop reads each year. getYear() gives the number that the rule needs.
  3. The leap-year rule. A year is leap when it divides by 4 but not by 100, or when it divides by 400.
  4. The results. 2024 is leap, 2100 is not (divisible by 100 but not 400), and 2000 is (divisible by 400).

Example 3 — Count Events per Year

Go through a list of events and count how many fall in each year.

Java
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

public class LdtGetYearGroup {
    public static void main(String[] args) {
        List<LocalDateTime> events = Arrays.asList(
            LocalDateTime.of(2025, 2, 10, 9, 0),
            LocalDateTime.of(2026, 1, 5, 14, 0),
            LocalDateTime.of(2025, 11, 20, 18, 30),
            LocalDateTime.of(2026, 3, 15, 10, 30),
            LocalDateTime.of(2026, 8, 1, 7, 15)
        );

        int in2025 = 0;
        int in2026 = 0;
        for (LocalDateTime event : events) {
            if (event.getYear() == 2025) {
                in2025++;
            } else if (event.getYear() == 2026) {
                in2026++;
            }
        }
        System.out.println("2025: " + in2025);
        System.out.println("2026: " + in2026);
    }
}

How It Works

  1. Five events. Two are in 2025 and three are in 2026.
  2. Check the year of each one. getYear() == 2025 compares the int with a number.
  3. Two counters. One goes up for each 2025 event and the other for each 2026 event.
  4. The totals. The program prints 2 and 3.

Frequently Asked Questions

Call dt.getYear(). It returns an int such as 2026.
No. It only reads a value. LocalDateTime is immutable, so no method can change it.
Use LocalDateTime.now().getYear(), or LocalDate.now().getYear().
Subtracting years ignores the month and day. Use ChronoUnit.YEARS.between(born, now) or Period.between() for an exact age.
Yes, for dates before year 1 in the proleptic ISO calendar. For modern dates it is always positive.

Next: hashCode()

Get a number that goes with equals, for use in hash-based collections.

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

15 people found this page helpful