Home Java LocalDateTime Methods minusMonths() Java LocalDateTime minusMonths() Method Definition In Java, minusMonths(months) returns a new LocalDateTime that is the given number of months earlier than the original. The year goes back by itself when the months pass January, and the time of day stays the same.
LocalDateTime is immutable , so the original object never changes. Always keep the result: dt = dt.minusMonths(1);. Months have different lengths, so this method has one special rule, 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 months. A negative number goes the other way.
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 8, 0);
LocalDateTime result = start.minusMonths(2); // 2026-01-15T08:00
System.out.println(start); // 2026-03-15T08:00 Call What it does dt.minusMonths(n)Returns a new date-time with n months subtracted. dt.plusMonths(n)The same idea in the opposite direction. dt.minus(n, ChronoUnit.MONTHS)The general form. It does the same thing for this unit. date.minusMonths(n)LocalDate has the same method, with the same end-of-month rule.
The result is a new object . Calling minusMonths() without using the returned value does nothing useful.
Library declarations // java.time.LocalDateTime
public LocalDateTime minusMonths(long months) Parameters Parameter Description months (long)The number of months to subtract. It can be zero or negative.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the months subtracted. The original is not changed.
Important
If the day does not exist in the earlier month, Java uses the last valid day of that month: 31 March minus 1 month is 28 February (or 29 February in a leap year). No exception is thrown. Because the day is cut back, subtracting a month and then adding a month does not always return to the starting date.
Compare with Similar Methods Methods that move a date-time in time:
Method How it differs plusMonths(n)The opposite direction for months. minus(n, unit)The general method: you choose the unit as an argument. minusYears(n)Subtracts whole years, which keeps the month and day. minusDays(n)Subtracts exact days, with no end-of-month rule.
Java Examples 1. Last month report 2. End of month 3. Months vs years Example 1 — Find the Same Moment in Earlier Months A report runs on 15 March. Print the same moment 1, 3, and 12 months earlier.
import java.time.LocalDateTime;
public class LdtMinusMonthsDemo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.of(2026, 3, 15, 8, 0);
System.out.println("1 month ago: " + now.minusMonths(1));
System.out.println("3 months ago: " + now.minusMonths(3));
System.out.println("12 months ago: " + now.minusMonths(12));
}
} 1 month ago: 2026-02-15T08:00
3 months ago: 2025-12-15T08:00
12 months ago: 2025-03-15T08:00 How It Works The starting moment. 15 March 2026 at 08:00.One month back. The day stays 15 and the month becomes February.Three months back. March minus three months goes into the previous year, giving 15 December 2025.Twelve months back. The result is 15 March 2025, exactly one year earlier.Example 2 — See the End-of-Month Rule Going Back Subtract one month from the 31st of March, from 30 March in a leap year, and from 31 May.
import java.time.LocalDateTime;
public class LdtMinusMonthsEnd {
public static void main(String[] args) {
LocalDateTime mar31 = LocalDateTime.of(2026, 3, 31, 9, 0);
LocalDateTime mar30 = LocalDateTime.of(2024, 3, 30, 9, 0);
LocalDateTime may31 = LocalDateTime.of(2026, 5, 31, 9, 0);
System.out.println("31 Mar 2026 - 1 month: " + mar31.minusMonths(1));
System.out.println("30 Mar 2024 - 1 month: " + mar30.minusMonths(1));
System.out.println("31 May 2026 - 1 month: " + may31.minusMonths(1));
}
} 31 Mar 2026 - 1 month: 2026-02-28T09:00
30 Mar 2024 - 1 month: 2024-02-29T09:00
31 May 2026 - 1 month: 2026-04-30T09:00 How It Works February 2026 is short. It has no 31st, so 31 March minus 1 month becomes 28 February.Leap year. February 2024 has 29 days, so 30 March minus 1 month becomes 29 February.April has 30 days. 31 May minus 1 month becomes 30 April.No error. Java chooses the last valid day, so your program keeps running.Example 3 — Compare Months with Years Subtract 12 months and 1 year and compare them, then subtract 18 months.
import java.time.LocalDateTime;
public class LdtMinusMonthsYears {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 8, 0);
LocalDateTime byMonths = start.minusMonths(12);
LocalDateTime byYears = start.minusYears(1);
System.out.println("12 months: " + byMonths);
System.out.println("1 year: " + byYears);
System.out.println("same: " + byMonths.equals(byYears));
System.out.println("18 months: " + start.minusMonths(18));
}
} 12 months: 2025-03-15T08:00
1 year: 2025-03-15T08:00
same: true
18 months: 2024-09-15T08:00 How It Works 12 months is one year. minusMonths(12) gives 15 March 2025.minusYears(1) agrees. Both objects are equal, so the third line prints true.18 months is a year and a half. The result is 15 September 2024, which shows how the year and month both change.Frequently Asked Questions How do I subtract months from a LocalDateTime in Java? Call dt.minusMonths(n). It returns a new LocalDateTime, so store it: dt = dt.minusMonths(n).
Does minusMonths() change the original LocalDateTime? No. LocalDateTime is immutable, so minusMonths() returns a new object and leaves the original as it was.
Can I pass a negative number to minusMonths()? Yes. A negative number moves the other way, so dt.minusMonths(-n) gives the same result as dt.plusMonths(n).
What is 31 March minus one month in Java? 28 February in a normal year and 29 February in a leap year, because Java uses the last valid day of February.
How do I get the same day last month in Java? Use dt.minusMonths(1). If last month is shorter and has no such day, you get its last 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.
7 people found this page helpful
Helpful Share Copy link Suggestion