Java LocalDate MinusMonth Method

JavaJavaBeginner
Practice Now

Introduction

The Java minusMonth() method is used to subtract or remove the specified number of months from a date and returns a new LocalDate. In this lab, we will learn how to use the minusMonth() method to subtract months from a date.

Create a new Java file

Create a new Java file named LocalDateMinusMonth.java in the ~/project directory using any text editor of your choice. We can use the following command to create the file:

touch ~/project/LocalDateMinusMonth.java

Import the necessary classes

We need to import the necessary classes from the java.time package to use LocalDate class and minusMonths() method. Add the following code at the beginning of the file:

import java.time.LocalDate;

Subtract months from a date

Create a LocalDate object with the specified date and use the minusMonths() method to subtract the number of months. Finally, print the new date. Add the following code inside the main method:

// create LocalDate object with specified date
LocalDate localDate = LocalDate.of(2021, 8, 10);

// subtract 3 months from the date
localDate = localDate.minusMonths(3);

// print the new date
System.out.println("New date: " + localDate);

Subtract months from the current date

Create a LocalDate object with the current date using the now() method and use the minusMonths() method to subtract the number of months. Finally, print the new date. Add the following code inside the main method:

// create LocalDate object with current date
LocalDate localDate = LocalDate.now();

// subtract 2 months from the date
localDate = localDate.minusMonths(2);

// print the new date
System.out.println("New date: " + localDate);

Run the program

Save the changes to the file and exit the text editor. Now, compile the Java file using the following command:

javac LocalDateMinusMonth.java

Run the compiled class file with the following command:

java LocalDateMinusMonth

The output will be similar to the following:

New date: 2021-05-10
New date: 2021-06-10

Subtract months from a date with negative months

Create a LocalDate object with the specified date and subtract negative months using the minusMonths() method. Finally, print the new date. Add the following code inside the main method:

// create LocalDate object with specified date
LocalDate localDate = LocalDate.of(2021, 8, 10);

// subtract negative 3 months from the date
localDate = localDate.minusMonths(-3);

// print the new date
System.out.println("New date: " + localDate);

Run the program

Save the changes to the file and exit the text editor. Now, compile the Java file using the following command:

javac LocalDateMinusMonth.java

Run the compiled class file with the following command:

java LocalDateMinusMonth

The output will be similar to the following:

New date: 2021-11-10

Subtract months from a date using a variable

Create a LocalDate object with the specified date and store the number of months in a variable. Use the variable to subtract months from the date. Finally, print the new date. Add the following code inside the main method:

// create LocalDate object with specified date
LocalDate localDate = LocalDate.of(2021, 8, 10);

// store the number of months in a variable
int monthsToSubtract = 3;

// subtract the number of months from the date using the variable
localDate = localDate.minusMonths(monthsToSubtract);

// print the new date
System.out.println("New date: " + localDate);

Run the program

Save the changes to the file and exit the text editor. Now, compile the Java file using the following command:

javac LocalDateMinusMonth.java

Run the compiled class file with the following command:

java LocalDateMinusMonth

The output will be similar to the following:

New date: 2021-05-10

Summary

In this lab, we learned how to use the minusMonth() method to subtract months from a LocalDate object in Java. We also learned how to use variables to specify the number of months to subtract.

Other Java Tutorials you may like