How to Add Days Weeks Months and Years to LocalDate in Java

This example shows how to add days, weeks, months and years to LocalDate in Java.

LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. You might use a LocalDate to track a significant event, such as birth date or wedding date.

Read more about LocalDate class with an example at https://www.javaguides.net/2018/08/java-8-localdate-class-api-guide.html.

Add Days Weeks Months  and Years to LocalDate in Java

LocalDate class provides below APIs to add days, months, weeks and years to LocalDate.
  • LocalDate plusDays(long daysToAdd) - Returns a copy of this LocalDate with the specified number of days added.
  • LocalDate plusMonths(long monthsToAdd) - Returns a copy of this LocalDate with the specified number of months added.
  • LocalDate plusWeeks(long weeksToAdd) - Returns a copy of this LocalDate with the specified number of weeks added.
  • LocalDate plusYears(long yearsToAdd) - Returns a copy of this LocalDate with the specified number of years added.
import java.time.LocalDate;
/**
 * Program to demonstrate LocalDate Class APIs.
 * @author javaguides.net
 *
 */
public class LocalDateExamples {
 
    public static void main(String[] args) {
        addOrSubstractUsingLocalDate();
    }

    public static void addOrSubstractUsingLocalDate() {

        LocalDate localDate = LocalDate.now();

        // LocalDate's plus methods
        System.out.println("Addition of days : " + localDate.plusDays(5));
        System.out.println("Addition of months : " + localDate.plusMonths(15));
        System.out.println("Addition of weeks : " + localDate.plusWeeks(45));
        System.out.println("Addition of years : " + localDate.plusYears(5));
    }
}
Output:
Addition of days : 2018-08-15
Addition of months : 2019-11-10
Addition of weeks : 2019-06-21
Addition of years : 2023-08-10

Reference


Comments