Home Java LocalDateTime Methods minusYears() Java LocalDateTime minusYears() Method Definition In Java, minusYears(years) returns a new LocalDateTime that is the given number of years earlier than the original. The month, the day, and the time of day stay the same.
LocalDateTime is immutable , so the original object never changes. Always keep the result: dt = dt.minusYears(1);. There is one exception: 29 February, explained under "Important" below.
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 and pass the number of years. A negative number goes the other way.
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.minusYears(3); // 2023-03-15T10:30
System.out.println(start); // 2026-03-15T10:30 Call What it does dt.minusYears(n)Returns a new date-time with n years subtracted. dt.plusYears(n)The same idea in the opposite direction. dt.minus(n, ChronoUnit.YEARS)The general form. It does the same thing for this unit. date.minusYears(n)LocalDate has the same method, with the same leap-day rule.
The result is a new object . Calling minusYears() without using the returned value does nothing useful.
Library declarations // java.time.LocalDateTime
public LocalDateTime minusYears(long years) Parameters Parameter Description years (long)The number of years to subtract. It can be zero or negative.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the years subtracted. The original is not changed.
Important
If the starting date is 29 February and the earlier year is not a leap year, Java uses 28 February . Remember that 2000 was a leap year (divisible by 400), while 1900 and 2100 are not. The method only fails when the year would go below the supported minimum.
Compare with Similar Methods Methods that move a date-time in time:
Method How it differs plusYears(n)The opposite direction for years. minus(n, unit)The general method: you choose the unit as an argument. minusMonths(n)Subtracts months, so minusMonths(12) equals minusYears(1). minusDays(n)Subtracts exact days, so a year may be 365 or 366 of them.
Java Examples 1. Years ago 2. Leap day 3. Age check Example 1 — Find the Same Moment Years Ago Print the same date and time 1, 10, and 20 years before 15 March 2026.
import java.time.LocalDateTime;
public class LdtMinusYearsDemo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.of(2026, 3, 15, 10, 30);
System.out.println("1 year ago: " + now.minusYears(1));
System.out.println("10 years ago: " + now.minusYears(10));
System.out.println("20 years ago: " + now.minusYears(20));
}
} 1 year ago: 2025-03-15T10:30
10 years ago: 2016-03-15T10:30
20 years ago: 2006-03-15T10:30 How It Works The starting moment. 15 March 2026 at 10:30.Only the year changes. minusYears(1) gives 2025, with the same month, day, and time.Bigger steps work the same way. 10 years back is 2016, and 20 years back is 2006.Example 2 — Go Back from 29 February Start on 29 February 2028 and subtract 1, 4, and 28 years.
import java.time.LocalDateTime;
public class LdtMinusYearsLeap {
public static void main(String[] args) {
LocalDateTime leapDay = LocalDateTime.of(2028, 2, 29, 12, 0);
System.out.println("-1 year: " + leapDay.minusYears(1));
System.out.println("-4 years: " + leapDay.minusYears(4));
System.out.println("-28 years: " + leapDay.minusYears(28));
}
} -1 year: 2027-02-28T12:00
-4 years: 2024-02-29T12:00
-28 years: 2000-02-29T12:00 How It Works 2027 is not a leap year. One year back has no 29 February, so Java uses 28 February 2027.2024 is a leap year. Four years back the leap day exists again.2000 is a leap year too. It is divisible by 400, so 28 years back gives 29 February 2000.Example 3 — Check if Someone Is at Least 18 Subtract 18 years from the current moment, and test a birth date against that cut-off.
import java.time.LocalDateTime;
public class LdtMinusYearsAge {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.of(2026, 3, 15, 9, 0);
LocalDateTime cutoff = now.minusYears(18);
LocalDateTime born = LocalDateTime.of(2008, 3, 15, 0, 0);
System.out.println("cut-off: " + cutoff);
System.out.println("born: " + born);
System.out.println("18 or older: " + born.isBefore(cutoff));
}
} cut-off: 2008-03-15T09:00
born: 2008-03-15T00:00
18 or older: true How It Works The cut-off. minusYears(18) gives 15 March 2008 at 09:00. Anyone born before this moment is at least 18.The birth moment. This person was born at midnight on 15 March 2008.isBefore decides. Midnight is earlier than 09:00 on the same day, so the answer is true.Frequently Asked Questions How do I subtract years from a LocalDateTime in Java? Call dt.minusYears(n). It returns a new LocalDateTime, so store it: dt = dt.minusYears(n).
Does minusYears() change the original LocalDateTime? No. LocalDateTime is immutable, so minusYears() returns a new object and leaves the original as it was.
Can I pass a negative number to minusYears()? Yes. A negative number moves the other way, so dt.minusYears(-n) gives the same result as dt.plusYears(n).
What is 29 February minus one year in Java? 28 February of the earlier year, because that year is not a leap year. Four years back it is 29 February again.
How do I get the same date last year in Java? Call dt.minusYears(1). The month, day, and time stay the same (except 29 February).
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.
11 people found this page helpful
Helpful Share Copy link Suggestion