Javaexercise.com

How To Add Years To Local date-time In Java 8 and Higher Versions?

To add years to date-time in Java, you can use the plusYears() method of the LocalDateTime class.

This method was added to Java 8 date-time API. So, can be used in Java 8 and higher versions such as Java 11, Java 17, etc.

This method returns a copy of the LocalDateTime object after adding the specified years.

For example, if we want to add 2 years to this date "2022-03-15T11:31:26.661639657" then the resulted date-time will be "2024-03-15T11:31:26.661639657".

Let's understand with the running example.

Adding Years to the current date-time in Java

Here, first, we used the now() method to get the current date-time and then used the plusYears() method to add years to the current date-time.

/* 
 *  Code example to add year to local date time in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// Take current date and time
	    LocalDateTime date = LocalDateTime.now();

		// Displaying date and time
		System.out.println("Date : "+date);

		// Add 2 years to the date
		LocalDateTime newDate = date.plusYears(2); 

		// Display result
		System.out.println("Year : "+newDate);
	}
}

Output:

Date : 2022-03-15T11:31:26.661639657
Year : 2024-03-15T11:31:26.661639657
 

Let's see the signature of this method:

public LocalDateTime plusYears(long years)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns a copy of LocalDateTime object after adding the specified number of years, not null.

Parameters: It takes a single long type value. It may be negative.

Exceptions: It throws a DateTimeException if the result exceeds the supported(either MIN or MAX) date range.

Version: Since 1.8

This method adds the specified amount to the year's field in three steps:

  1. Add the input years to the year field
  2. Check if the resulting date would be invalid
  3. Adjust the day-of-month to the last valid day if necessary

For example, 2008-02-29 (leap year) plus one year would result in the invalid date 2009-02-29 because 2009 is not a leap year. So, this mehtod, instead of returning an invalid result, the last valid day of the month, 2009-02-28, is selected instead.

Let's see some more examples.

Adding Year to a String date-time in Java

Here, we first created a LocalDateTime object by using the parse() method and then used the plusYears() method to add years.

/* 
 *  Code example to add year to local date time in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// String  date is given
		String strDate = "2022-03-14T17:28:13.048999208";
		// parse the date into date time
		LocalDateTime date = LocalDateTime.parse(strDate);

		// Displaying date and time
		System.out.println("Date : "+date);

		// Add 2 years to the date
		LocalDateTime newDate = date.plusYears(2); 

		// Display result
		System.out.println("Year : "+newDate);
	}
}

Output:

Date : 2022-03-14T17:28:13.048999208
Year : 2024-03-14T17:28:13.048999208
 

Subtract Years from the date-time in Java

The plusYears() method accepts a negative value as an argument as well. So, we can pass a negative value to subtract years from the date-time as well. This method works with both positive and negative values. So, we can use it to add or subtract years both. See the below Java code.

/* 
 *  Code example to add year to local date time in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// String  date is given
		String strDate = "2022-03-14T17:28:13.048999208";
		// parse the date into date time
		LocalDateTime date = LocalDateTime.parse(strDate);

		// Displaying date and time
		System.out.println("Date : "+date);

		// Add 2 years to the date
		LocalDateTime newDate = date.plusYears(-2); 

		// Display result
		System.out.println("Year : "+newDate);
	}
}

Output:

Date : 2022-03-14T17:28:13.048999208
Year : 2020-03-14T17:28:13.048999208
 

Single Line Solution - Final Shot

Use this code to get results in a single line of code. If you are a beginner, skip this code.

LocalDateTime.now().plusYears(5);

This code will return a LocalDateTime object after adding 5 years to the current date-time. You just replace the value with your input and get the result instantly.