Java LocalDate withDayOfYear() Method

The withDayOfYear() method in Java, part of the java.time.LocalDate class, is used to return a copy of the LocalDate instance with the day-of-year altered. This method is useful for creating a new date with a specific day of the year.

Table of Contents

  1. Introduction
  2. withDayOfYear() Method Syntax
  3. Understanding withDayOfYear()
  4. Examples
    • Basic Usage
    • Using withDayOfYear() for Validations
  5. Real-World Use Case
  6. Conclusion

Introduction

The withDayOfYear() method allows you to create a new LocalDate instance with the day-of-year altered. This is particularly useful when you need to adjust the day of the year while keeping the year unchanged.

withDayOfYear() Method Syntax

The syntax for the withDayOfYear() method is as follows:

public LocalDate withDayOfYear(int dayOfYear)

Parameters:

  • dayOfYear: The day of the year to set in the resulting LocalDate.

Returns:

  • A LocalDate representing the specified day of the year.

Throws:

  • DateTimeException if the day-of-year value is invalid for the year.

Understanding withDayOfYear()

The withDayOfYear() method returns a new LocalDate instance with the day-of-year altered to the specified value, while the year remains the same. The method ensures that the day-of-year value is valid for the given year.

Examples

Basic Usage

To demonstrate the basic usage of withDayOfYear(), we will change the day of the year of a LocalDate instance.

Example

import java.time.LocalDate;

public class LocalDateWithDayOfYearExample {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2024, 1, 1);
        LocalDate newDate = date.withDayOfYear(100); // Change the day to the 100th day of the year

        System.out.println("Original Date: " + date);
        System.out.println("New Date: " + newDate);
    }
}

Output:

Original Date: 2024-01-01
New Date: 2024-04-09

Using withDayOfYear() for Validations

This example shows how to use the withDayOfYear() method to ensure that a date is set to the first day of the year.

Example

import java.time.LocalDate;

public class FirstDayOfYearExample {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2024, 6, 27);
        LocalDate firstDayOfYear = date.withDayOfYear(1); // Set to the first day of the year

        System.out.println("Original Date: " + date);
        System.out.println("First Day of Year: " + firstDayOfYear);
    }
}

Output:

Original Date: 2024-06-27
First Day of Year: 2024-01-01

Real-World Use Case

Adjusting Dates to Specific Day of the Year

In real-world applications, the withDayOfYear() method can be used to adjust dates to specific days of the year, such as setting all dates to the 100th day of the year.

Example

import java.time.LocalDate;

public class AdjustDateExample {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        LocalDate adjustedDate = currentDate.withDayOfYear(100); // Set the date to the 100th day of the year

        System.out.println("Current Date: " + currentDate);
        System.out.println("Adjusted Date: " + adjustedDate);
    }
}

Output:

Current Date: 2024-07-06
Adjusted Date: 2024-04-09

Conclusion

The LocalDate.withDayOfYear() method is used to create a new LocalDate instance with a specific day of the year. This method is particularly useful for adjusting the day of the year while keeping the year unchanged. By understanding and using this method, you can effectively manage and manipulate date-based data in your Java applications.

Comments