Home Java LocalDateTime Methods getYear() Java LocalDateTime getYear() Method 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.
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);
int year = dt.getYear(); // 2026 Call What 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.time.LocalDateTime
public int getYear() Parameters Parameter Description None getYear() takes no parameters.
Return Value Return Value Description intThe year, from -999999999 to 999999999.
Important
The year is the year of the date only. It does not say if the date-time is before or after another one: use isBefore() or compareTo() for that. Subtracting two years, as in now.getYear() - born.getYear(), gives only a rough age, because it ignores the month and the day.
Compare with Similar Methods Other ways to read part of a date-time:
Java Examples 1. Read the year 2. Leap year test 3. Count by year Example 1 — Read the Year and Do Some Math Read the year of two dates and use the numbers in a simple calculation.
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()));
}
} born in: 1990
now: 2026
rough age: 36 How It Works Two date-times. One in 1990 and one in 2026.getYear returns an int. The first two lines print 1990 and 2026.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.
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);
}
}
} 2024 is a leap year: true
2100 is a leap year: false
2000 is a leap year: true How It Works Three dates. They are in 2024, 2100, and 2000.The loop reads each year. getYear() gives the number that the rule needs.The leap-year rule. A year is leap when it divides by 4 but not by 100, or when it divides by 400.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.
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 Five events. Two are in 2025 and three are in 2026.Check the year of each one. getYear() == 2025 compares the int with a number.Two counters. One goes up for each 2025 event and the other for each 2026 event.The totals. The program prints 2 and 3.Frequently Asked Questions How do I get the year from a LocalDateTime in Java? Call dt.getYear(). It returns an int such as 2026.
Does getYear() change the LocalDateTime? No. It only reads a value. LocalDateTime is immutable, so no method can change it.
How do I get the current year in Java? Use LocalDateTime.now().getYear(), or LocalDate.now().getYear().
Why is my age calculation off by one with getYear()? Subtracting years ignores the month and day. Use ChronoUnit.YEARS.between(born, now) or Period.between() for an exact age.
Can getYear() return a negative number? 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 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.
15 people found this page helpful
Helpful Share Copy link Suggestion