Java LocalDate withDayOfYear Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to use the withDayOfYear() method in Java LocalDate class to create a date with a new day-of-year value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) 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/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") 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/scope -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/classes_objects -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/class_methods -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/date -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/exceptions -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/modifiers -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/oop -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/packages_api -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/identifier -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/arrays -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/comments -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/data_types -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/operators -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/output -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/strings -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} java/system_methods -.-> lab-117860{{"`Java LocalDate withDayOfYear Method`"}} end

Create a Java file

Create a file LocalDateWithDayOfYear.java under the ~/project directory with the following code:

import java.time.LocalDate;

public class LocalDateWithDayOfYear {
    public static void main(String[] args) {
        System.out.println("Original date: ");
        LocalDate localDate = LocalDate.of(2002, 01, 10);
        System.out.println(localDate);
    }
}

Use the following command to compile and run the code:

javac LocalDateWithDayOfYear.java && java LocalDateWithDayOfYear

The output should show the original date:

Original date:
2002-01-10

Use withDayOfYear() to Set the New Day-of-Year

To create a new date by setting a new value of a day-of-year, we can use the withDayOfYear() method to set a new day for the date.

Add the following code after the System.out.println(localDate); line:

// set day-of-year as 30
localDate = localDate.withDayOfYear(30);
System.out.println("New date with day-of-year set as 30: ");
System.out.println(localDate);

// set day-of-year as 300
localDate = localDate.withDayOfYear(300);
System.out.println("New date with day-of-year set as 300: ");
System.out.println(localDate);

Use the following command to compile and run the code:

javac LocalDateWithDayOfYear.java && java LocalDateWithDayOfYear

The output should show the new dates with the new day-of-year values:

Original date:
2002-01-10
New date with day-of-year set as 30:
2002-01-30
New date with day-of-year set as 300:
2002-10-27

Handle Invalid Date

If the resulting date is invalid, an exception will be thrown. We can handle the exception using try-catch block as follows:

try {
    // set day-of-year as 366 (invalid for non-leap year)
    localDate = localDate.withDayOfYear(366);
    System.out.println(localDate);
} catch (Exception e) {
    System.out.println("Invalid date: " + e.getMessage());
}

Use the following command to compile and run the code:

javac LocalDateWithDayOfYear.java && java LocalDateWithDayOfYear

The output should show the message of invalid date exception:

Original date:
2002-01-10
New date with day-of-year set as 30:
2002-01-30
New date with day-of-year set as 300:
2002-10-27
Invalid date: Invalid date 'DayOfYear 366' as '2002' is not a leap year

Summary

In this lab, we have learned how to use the withDayOfYear() method in Java LocalDate class to create a date with a new day-of-year value. We have also learned how to handle the invalid date exception when setting a new day-of-year value.

Other Java Tutorials you may like