Java LocalDate plusWeeks Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the plusWeeks() method in Java to add a specified number of weeks to a given date using the LocalDate class.


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/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") 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/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/class_methods -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/date -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/modifiers -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/packages_api -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/identifier -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/arrays -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/comments -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/data_types -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/operators -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/output -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/strings -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} java/system_methods -.-> lab-117844{{"`Java LocalDate plusWeeks Method`"}} end

Import the required package

In order to use the LocalDate class, you need to import the java.time.LocalDate package. Add the following line of code at the top of your DateDemo.java file.

import java.time.LocalDate;

Define the main method

Define the main method in your DateDemo.java file.

public static void main(String[] args){
    // Add code here
}

Create a LocalDate object

Create a LocalDate object with the date you want to add weeks to.

LocalDate localDate = LocalDate.of(2021, 12, 31);

Print the initial date

Print the initial date using the System.out.println() method.

System.out.println("Initial date: " + localDate);

Add weeks to the date using the plusWeeks() method

Use the plusWeeks() method to add a specified number of weeks to the localDate object and store the result in the localDate object.

localDate = localDate.plusWeeks(2);

Print the updated date

Print the updated date using the System.out.println() method.

System.out.println("Updated date: " + localDate);

Compile and run the program

Compile the program using the following command in the terminal:

javac DateDemo.java

Run the program using the following command:

java DateDemo

Update date with current date

Create a LocalDate object to represent the current date.

LocalDate currentDate = LocalDate.now();

Print the updated date

Use the plusWeeks() method to add 5 weeks to the current date and print the updated date.

currentDate = currentDate.plusWeeks(5);
System.out.println("New date: " + currentDate);

Summary

Congratulations! You have successfully learned how to use the plusWeeks() method in Java to add a specified number of weeks to a given date using the LocalDate class. This feature can be useful when calculating dates in the future or past based on specific requirements.

Other Java Tutorials you may like