Java LocalDateTime withYear() Method

The withYear() method in Java, part of the java.time.LocalDateTime class, is used to return a copy of the LocalDateTime with the year altered. This method is useful for manipulating date-time values by changing the year while keeping other fields unchanged.

Table of Contents

  1. Introduction
  2. withYear() Method Syntax
  3. Understanding withYear()
  4. Examples
    • Basic Usage
    • Using withYear() in Conditional Statements
  5. Real-World Use Case
  6. Conclusion

Introduction

The withYear() method allows you to create a new LocalDateTime instance with the specified year. This is particularly useful when you need to adjust the year while preserving the rest of the date-time fields.

withYear() Method Syntax

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

public LocalDateTime withYear(int year)

Parameters:

  • year: The year to set in the resulting LocalDateTime.

Returns:

  • A LocalDateTime based on this date-time with the specified year, not null.

Throws:

  • DateTimeException if the year value is invalid or if the resulting LocalDateTime exceeds the supported date range.

Understanding withYear()

The withYear() method creates a new LocalDateTime instance with the specified year while keeping the other fields (month, day of month, hour, minute, second, and nanosecond) unchanged.

Examples

Basic Usage

To demonstrate the basic usage of withYear(), we will change the year of a LocalDateTime instance.

Example

import java.time.LocalDateTime;

public class LocalDateTimeWithYearExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2023, 6, 15, 10, 30);
        LocalDateTime newDateTime = dateTime.withYear(2025); // Change year to 2025

        System.out.println("Original DateTime: " + dateTime);
        System.out.println("New DateTime: " + newDateTime);
    }
}

Output:

Original DateTime: 2023-06-15T10:30
New DateTime: 2025-06-15T10:30

Using withYear() in Conditional Statements

This example shows how to use the withYear() method in conditional statements to perform actions based on the adjusted date-time.

Example

import java.time.LocalDateTime;

public class LocalDateTimeConditionalExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2023, 6, 15, 10, 30);
        LocalDateTime newDateTime = dateTime.withYear(2024); // Change year to 2024

        if (newDateTime.getYear() == 2024) {
            System.out.println("The year has been changed to 2024.");
        } else {
            System.out.println("The year has not been changed to 2024.");
        }
    }
}

Output:

The year has been changed to 2024.

Real-World Use Case

Scheduling Tasks in Specific Years

In real-world applications, the withYear() method can be used to schedule tasks or events in specific years.

Example

import java.time.LocalDateTime;

public class TaskSchedulerExample {
    public static void main(String[] args) {
        LocalDateTime taskDateTime = LocalDateTime.of(2023, 6, 15, 10, 30);
        LocalDateTime newTaskDateTime = taskDateTime.withYear(2026); // Schedule task in the year 2026

        System.out.println("Original Task DateTime: " + taskDateTime);
        System.out.println("New Task DateTime: " + newTaskDateTime);
    }
}

Output:

Original Task DateTime: 2023-06-15T10:30
New Task DateTime: 2026-06-15T10:30

Conclusion

The LocalDateTime.withYear() method is used to create a new LocalDateTime instance with the specified year while keeping other fields unchanged. This method is particularly useful for adjusting the year in date-time calculations. By understanding and using the withYear() method, you can effectively manage and manipulate date-time data in your Java applications.

Comments