Java LocalDate Minus Method

JavaJavaBeginner
Practice Now

Introduction

The LocalDate class in Java allows us to perform date-based operations. One of the operations we may need to perform is to subtract a certain number of days, weeks or months from a given date. The minus() method is used to subtract a specified amount of time from a LocalDate object.


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/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/date -.-> lab-117814{{"`Java LocalDate Minus Method`"}} java/packages_api -.-> lab-117814{{"`Java LocalDate Minus Method`"}} java/identifier -.-> lab-117814{{"`Java LocalDate Minus Method`"}} java/comments -.-> lab-117814{{"`Java LocalDate Minus Method`"}} java/operators -.-> lab-117814{{"`Java LocalDate Minus Method`"}} java/output -.-> lab-117814{{"`Java LocalDate Minus Method`"}} java/strings -.-> lab-117814{{"`Java LocalDate Minus Method`"}} java/system_methods -.-> lab-117814{{"`Java LocalDate Minus Method`"}} end

Create a Java file

We will create a new Java file in the ~/project directory called DateDemo.java using the following command:

touch ~/project/DateDemo.java

Import LocalDate and ChronoUnit

We will import the necessary classes LocalDate and ChronoUnit using the import statement:

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

Subtract days from a date

We will create an instance of LocalDate representing a specific date using the of() method:

LocalDate localDate = LocalDate.of(2021, 8, 15);

Next, we will subtract a certain number of days from the above date using the minus() method. We will subtract 2 days from the above date using the following code:

//Subtract 2 days from the date
localDate = localDate.minus(2, ChronoUnit.DAYS);

The above code will subtract 2 days from the LocalDate instance localDate.

Subtract months from a date

We can use the minus() method to subtract months from a LocalDate object. We will use the same localDate object created in step 3, and subtract 2 months from it using the following code:

// Subtract 2 months from the date
localDate = localDate.minus(2, ChronoUnit.MONTHS);

The above code will subtract 2 months from the localDate instance.

Print the new date

We will print the original date and the new date after the subtraction of 2 days and 2 months respectively, using the System.out.println() method:

System.out.println("Original date: " + LocalDate.of(2021, 8, 15));
System.out.println("New date after subtracting 2 days: " + localDate);
System.out.println("New date after subtracting 2 months: " + localDate1);

Compile and run the code

To run the code, we will compile the DateDemo.java file using the following command:

javac ~/project/DateDemo.java

If there are no compilation errors, we can run the program using the following command:

java DateDemo

Review the output

The output of the program should display the original date and the new date after subtracting 2 days and 2 months respectively. The output should look similar to the following:

Original date: 2021-08-15
New date after subtracting 2 days: 2021-08-13
New date after subtracting 2 months: 2021-06-13

Summary

In this lab, we learned how to use the minus() method to subtract a specified number of days or months from a LocalDate object. We also learned how to print the original date and the new date after the subtraction. The minus() method is useful when we need to perform date calculations in Java.

Other Java Tutorials you may like