Java 8 – How to add Year, Month and Day fields to LocalDate ?

In this article, we will learn how to add Year or Month or DayOfMonth fields to LocalDate

Adding Day/Week/Month/Year to LocalDate:

  • It is very simple to add Day or Week or Month or Year fields to LocalDate using below methods,
    1. plusDays() – Returns a copy of invoking LocalDate with the specified number of days added
    2. plusWeeks() – Returns a copy of invoking LocalDate with the specified number of weeks added
    3. plusMonths() – Returns a copy of invoking LocalDate with the specified number of months added
    4. plusYears() – Returns a copy of invoking LocalDate with the specified number of years added
  • In the below illustration, we are going to do below operations with current LocalDate,
    1. Add 5 Days to current LocalDate using plusDays() method
    2. Add 2 Weeks to current LocalDate using plusWeeks() method
    3. Add 3 Months to current LocalDate using plusMonths() method
    4. Add 1 Year to current LocalDate using plusYears() method
  • Finally, print LocalDate to the console

AddToLocalDate.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;

public class AddToLocalDate {

	public static void main(String[] args) {

		// 1. get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- " + localDate);


		// 1.1 add 5 days with current system date
		LocalDate add_5_Days = localDate.plusDays(5);
		System.out.println("\n1. After adding 5 Days to Current System Date is :- "
				+ add_5_Days);


		// 1.2 add 2 weeks to current system date
		LocalDate add_2_Weeks = localDate.plusWeeks(2);
		System.out.println("2. After adding 2 Weeks to Current System Date is :- "
				+ add_2_Weeks);


		// 1.3 add 3 months to current system date
		LocalDate add_3_Months = localDate.plusMonths(3);
		System.out.println("3. After adding 3 Months to Current System Date is :- "
				+ add_3_Months);


		// 1.4 add 1 year to current system date
		LocalDate add_1_Year = localDate.plusYears(1);
		System.out.print("4. After adding 1 Year to Current System Date is :- "
				+ add_1_Year);

	}
}

Output:

Current System Date is :- 2022-07-30

1. After adding 5 Days to Current System Date is :- 2022-08-04
2. After adding 2 Weeks to Current System Date is :- 2022-08-13
3. After adding 3 Months to Current System Date is :- 2022-10-30
4. After adding 1 Year to Current System Date is :- 2023-07-30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
Java 8 – How to convert LocalDate in different Format Style ?