Java LocalDate withYear Method

JavaJavaBeginner
Practice Now

Introduction

The withYear() method of the Java LocalDate class is used to get a new date with the specified year. It returns a copy of the current LocalDate instance with the year changed to the specified value. This lab will guide you through using the withYear() method in a step-by-step manner.


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

Create a Java file

Open the terminal and create a Java file with the touch command.

touch ~/project/WithYearExample.java

This creates a Java file named WithYearExample.java in the ~/project/ directory.

Import the required packages

A package is a namespace that organizes a set of related classes and interfaces. To use the LocalDate class, we have to import the java.time.LocalDate package. Add the following code to the WithYearExample.java file.

import java.time.LocalDate;

Create an instance of LocalDate

In this step, we will create an instance of LocalDate class using the of() method. The of() method takes three arguments; the year, month and day. Add the following code below the import statement.

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

Here, we set the date as January 15th, 2022.

Change the year using withYear()

In this step, we will use the withYear() method to change the year of the LocalDate instance. Add the following code below step 3.

LocalDate newDate = date.withYear(2030);  // Set year as 2030
System.out.println("New Date: " + newDate);

Here, we create a new variable newDate and set the year to 2030 using the withYear() method.

Handle invalid day-of-month

If the current LocalDate instance has a day-of-month that is invalid for the new year, then withYear() method adjusts the day-of-month value to the last valid day of the month. Add the following code below step 4.

LocalDate dateWithInvalidDayOfMonth = LocalDate.of(2021, 2, 31);
LocalDate newDateWithAdjustedDayOfMonth = dateWithInvalidDayOfMonth.withYear(2022);
System.out.println("Old Date: " + dateWithInvalidDayOfMonth + "\nNew Date: " + newDateWithAdjustedDayOfMonth);

Here, we set a date with an invalid day-of-month, 31st of February 2021 which doesn't exist. We then apply the withYear() method to change the year to 2022. Since 2022 is not a leap year, and February has only 28 days, the day-of-month is adjusted to 28, which is the last valid day of February.

Compile and run the program

In order to compile and run the program, navigate to the project directory and execute the following command.

javac WithYearExample.java && java WithYearExample

The output of the program after executing the above command should be:

Initial Date: 2022-01-15
New Date: 2030-01-15
Old Date: 2021-02-31
New Date: 2022-02-28

Summary

In this lab, we learned how to use the withYear() method of the LocalDate class in Java to get a date with the specified year. We also learned how to handle invalid day-of-month when using the withYear() method. By following these steps, you should now be able to use the withYear() method to work effectively with LocalDate instances in your Java programs.

Other Java Tutorials you may like