The minusMonths() method of LocalDate class in Java is used to subtract the number of specified months from this LocalDate and return a copy of LocalDate.
This method subtracts the months field in the following steps:
- subtract the months from the month-of-year field.
- Check if the date after subtracting months is valid or not.
- If date is invalid then method adjust the day-of-month to the last valid day.
For example, 2018-07-31 minus one month gives date 2018-06-31 but this is invalid result, so the last valid day of the month, 2018-06-30, is returned.This instance is immutable and unaffected by this method call.
public LocalDate minusMonths(long monthsToSubtract)
Parameters: This method accepts a single parameter monthsToSubtract which represents the months to subtract, may be negative.
Return value: This method returns a LocalDate based on this date with the months subtracted, not null.
Exception: This method throws DateTimeException if the result exceeds the supported date range.
Below programs illustrate the minusMonths() method:
LocalDate before subtracting months: 2018-11-13 LocalDate after subtracting months: 2017-08-13
LocalDate before subtracting months: 2018-12-31 LocalDate after subtracting months: 2018-09-30
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.