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,
- plusDays() – Returns a copy of invoking
LocalDate
with the specified number of days added - plusWeeks() – Returns a copy of invoking
LocalDate
with the specified number of weeks added - plusMonths() – Returns a copy of invoking
LocalDate
with the specified number of months added - plusYears() – Returns a copy of invoking
LocalDate
with the specified number of years added
- plusDays() – Returns a copy of invoking
- In the below illustration, we are going to do below operations with current LocalDate,
- Add 5 Days to current LocalDate using plusDays() method
- Add 2 Weeks to current LocalDate using plusWeeks() method
- Add 3 Months to current LocalDate using plusMonths() method
- 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:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!