Java LocalDateTime plusYears() Method

The plusYears() method in Java, part of the java.time.LocalDateTime class, is used to add a specified number of years to a LocalDateTime instance. This method is useful for manipulating date-time values by adding years.

Table of Contents

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

Introduction

The plusYears() method allows you to add a specified number of years to a LocalDateTime instance. This is particularly useful when you need to calculate future dates based on a given LocalDateTime.

plusYears() Method Syntax

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

public LocalDateTime plusYears(long years)

Parameters:

  • years: The number of years to add, may be negative.

Returns:

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

Throws:

  • DateTimeException if the result exceeds the supported date range.
  • ArithmeticException if numeric overflow occurs.

Understanding plusYears()

The plusYears() method adds the specified number of years to the LocalDateTime instance and returns a new LocalDateTime instance representing the adjusted date-time. This method is immutable and does not modify the original LocalDateTime instance.

Examples

Basic Usage

To demonstrate the basic usage of plusYears(), we will add a specified number of years to a LocalDateTime instance.

Example

import java.time.LocalDateTime;

public class LocalDateTimePlusYearsExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2023, 6, 15, 10, 30);
        LocalDateTime newDateTime = dateTime.plusYears(5); // Add 5 years

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

Output:

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

Using plusYears() in Conditional Statements

This example shows how to use the plusYears() 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 currentDateTime = LocalDateTime.now();
        LocalDateTime futureDateTime = currentDateTime.plusYears(3); // Add 3 years

        if (futureDateTime.isAfter(currentDateTime)) {
            System.out.println("The future date-time is after the current date-time.");
        } else {
            System.out.println("The future date-time is not after the current date-time.");
        }
    }
}

Output:

The future date-time is after the current date-time.

Real-World Use Case

Scheduling Future Events

In real-world applications, the plusYears() method can be used to schedule events in the future, such as setting a reminder for a certain number of years from now.

Example

import java.time.LocalDateTime;

public class EventSchedulerExample {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        LocalDateTime futureEventDateTime = currentDateTime.plusYears(2); // Schedule event 2 years from now

        System.out.println("Current DateTime: " + currentDateTime);
        System.out.println("Event DateTime: " + futureEventDateTime);
    }
}

Output:

Current DateTime: 2024-07-07T09:57:36.803901400
Event DateTime: 2026-07-07T09:57:36.803901400

Conclusion

The LocalDateTime.plusYears() method is used to add a specified number of years to a LocalDateTime instance. This method is particularly useful for calculating future dates. By understanding and using the plusYears() method, you can effectively manage and manipulate date-time data in your Java applications.

Continue Learning with Java Guides

Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.

By Ramesh Fadatare - Udemy Instructor Partner

Comments