Java LocalDate Plus Method

JavaJavaBeginner
Practice Now

Introduction

In Java, the LocalDate class represents a date without a time zone, and it has plus() method to add a specified amount of time to a date object. The plus() method takes two parameters: the first parameter specifies the amount of the unit to add and the second parameter specifies the TemporalUnit.


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/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-117838{{"`Java LocalDate Plus Method`"}} java/packages_api -.-> lab-117838{{"`Java LocalDate Plus Method`"}} java/identifier -.-> lab-117838{{"`Java LocalDate Plus Method`"}} java/operators -.-> lab-117838{{"`Java LocalDate Plus Method`"}} java/output -.-> lab-117838{{"`Java LocalDate Plus Method`"}} java/strings -.-> lab-117838{{"`Java LocalDate Plus Method`"}} java/system_methods -.-> lab-117838{{"`Java LocalDate Plus Method`"}} end

Create a Java file

Create a Java file named LocalDatePlusMethod.java in the ~/project/ directory using the following command.

touch ~/project/LocalDatePlusMethod.java

Import LocalDate and ChronoUnit Classes

In this step, you need to import the necessary classes and interfaces to create LocalDate and ChronoUnit objects. You can import them using the following code.

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

Create a LocalDate Object

In this step, you will create a LocalDate object with an initial date value using the of() method.

Add the following code to create a LocalDate object.

LocalDate date = LocalDate.of(2022, 05, 20);
System.out.println("Initial Date: "+date);

Add Days to the Date Object

In this step, you will add 10 days to the LocalDate object created in step 3 using the plus() method.

Add the following code block to add 10 days to the current date object.

date = date.plus(10, ChronoUnit.DAYS);
System.out.println("Date after adding 10 days: "+date);

Add Weeks to the Date Object

In this step, you will add 2 weeks to the LocalDate object created in step 3 using the plus() method.

Add the following code block to add 2 weeks to the current date object.

date = date.plus(2, ChronoUnit.WEEKS);
System.out.println("Date after adding 2 weeks: "+date);

Add Months to the Date Object

In this step, you will add 5 months to the LocalDate object created in step 3 using the plus() method.

Add the following code block to add 5 months to the current date object.

date = date.plus(5, ChronoUnit.MONTHS);
System.out.println("Date after adding 5 months: "+date);

Add Years to the Date Object

In this step, you will add 1 year to the LocalDate object created in step 3 using the plus() method.

Add the following code block to add 1 year to the current date object.

date = date.plus(1, ChronoUnit.YEARS);
System.out.println("Date after adding 1 year: "+date);

Add Centuries to the Date Object

In this step, you will add 2 centuries to the LocalDate object created in step 3 using the plus() method.

Add the following code block to add 2 centuries to the current date object.

date = date.plus(2, ChronoUnit.CENTURIES);
System.out.println("Date after adding 2 centuries: "+date);

Add Millennia to the Date Object

In this step, you will add 1 millennium to the LocalDate object created in step 3 using the plus() method.

Add the following code block to add 1 millennium to the current date object.

date = date.plus(1, ChronoUnit.MILLENNIA);
System.out.println("Date after adding 1 millennium: "+date);

Compile and Run the Program

To compile the program, run the following command.

javac LocalDatePlusMethod.java

And to run the program, use the following command.

java LocalDatePlusMethod

Summary

In this lab, you learned how to add a specified amount of time to a LocalDate object using the plus() method. You also learned to use different time units like days, weeks, months, years, centuries, and millennia. The plus() method returns a new LocalDate object with the updated date after adding the specified time to the original date object.

Other Java Tutorials you may like