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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/date -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/oop -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/packages_api -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/identifier -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/comments -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/data_types -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/operators -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/output -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/strings -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/variables -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} java/system_methods -.-> lab-117818{{"`Java LocalDate MinusMonth Method`"}} end

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