Wednesday, July 3, 2024
HomeLanguagesJavaLocalDate minusMonths() method in Java with Examples

LocalDate minusMonths() method in Java with Examples

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.

Syntax:

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:
Program 1:




// Java program to demonstrate
// LocalDate.minusMonths() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDate object
        LocalDate date
            = LocalDate.parse("2018-11-13");
  
        // print instance
        System.out.println("LocalDate before"
                           + " subtracting months: " + date);
  
        // subtract 15 months
        LocalDate returnvalue
            = date.minusMonths(15);
  
        // print result
        System.out.println("LocalDate after "
                           + " subtracting months: " + returnvalue);
    }
}


Output:

LocalDate before subtracting months: 2018-11-13
LocalDate after  subtracting months: 2017-08-13

Program 2:




// Java program to demonstrate
// LocalDate.minusMonths() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDate object
        LocalDate date
            = LocalDate.parse("2018-12-31");
  
        // print instance
        System.out.println("LocalDate before"
                           + " subtracting months: " + date);
  
        // subtract 3 months
        LocalDate returnvalue
            = date.minusMonths(3);
  
        // print result
        System.out.println("LocalDate after "
                           + " subtracting months: " + returnvalue);
    }
}


Output:

LocalDate before subtracting months: 2018-12-31
LocalDate after  subtracting months: 2018-09-30

References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#minusMonths(long)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments