-->
Skip to main content

Localdate Add Days in Java [2021]

Localdate Add Days in Java [2021]

We can add days in LocalDate using three methods provided by LocalDate Class
  1. Add days using LocalDate.plusDays(long daysToAdd)
  2. Add days Using LocalDate.plus(TemporalAmount amountToAdd)
  3. Add days Using LocalDate.plus(long amountToAdd, TemporalUnit unit)

In recent post we have covered now() method of LocalDate Java LocalDate now() Method Example

LocalDate Add Days Example

LocalDate Add Days Examples:

1. Add days using LocalDate.plusDays(long daysToAdd)

In this method, we just need to pass noOfDays to add in LocalDate today date and a new date with added number will be there in new LocalDate instance.

Example:

 import java.time.LocalDate;

 public class LocalDateAddDaysEx1 {
  public static void main(String[] args) {
    LocalDate todaysDate = LocalDate.now(); // 2020-07-09
    System.out.println("Current date : " + todaysDate);
		
    // Using LocalDate.plusDays(long daysToAdd)
    LocalDate newDate = todaysDate.plusDays(2);
    System.out.println("updated date : " + newDate);// 2020-07-11

    LocalDate date = LocalDate.of(2020, 8, 21);
    System.out.println("Created date : " + date);
    System.out.println("updated date : " + date.plusDays(2));// 2020-08-23
  }
 }

Output:

Current date : 2020-07-09
updated date : 2020-07-11
Created date : 2020-08-21
updated date : 2020-08-23

2. Add days Using LocalDate.plus(TemporalAmount amountToAdd)

In this method, we have to pass the TemporalAmount type variable as TemporalAmount is the interface we have to use its implementation class which is Period class which has various methods. To add days Period class have ofDays(long amountToAdd) method which takes number of days as an input parameter.

Example:

 import java.time.LocalDate;
 import java.time.Period;

 public class LocalDateAddDaysEx2 {
   public static void main(String[] args) {
     LocalDate date = LocalDate.now();
     System.out.println("Current Date : " + date);
		
     // Using LocalDate.plus(TemporalAmount amountToAdd)
     LocalDate newDate = date.plus(Period.ofDays(2));
     System.out.println("Updated Date : " + newDate);
   }
 }

Output:

Current Date : 2020-07-09
Updated Date : 2020-07-11

3. Add days Using LocalDate.plus(long amountToAdd, TemporalUnit unit)

In this method, we have two parameters one is amountToAdd which is number type and second one is TemporalUnit which is an interface. its implementing class ChronoUnit has various constants like DAYS, HOURS, MINUTES, etc. so to add number of days in LocalDate we pass ChronoUnit.DAYS as second parameter.

Example:

 import java.time.LocalDate;
 import java.time.temporal.ChronoUnit;

 public class LocalDateAddDaysEx3 {
   public static void main(String[] args) {
     LocalDate date = LocalDate.now();
     System.out.println("Current date : " + date);
		
     // Using LocalDate.plus(long amountToAdd, TemporalUnit unit);
     LocalDate newDate = date.plus(2, ChronoUnit.DAYS);
     System.out.println("Updated date : " + newDate);
   }
 }

Output:

Current date : 2020-07-09
Updated date : 2020-07-11

Conclusion:

In this tutorial, we have covered LocalDate add days process using there methods of LocalDate class
  1. Add days using LocalDate.plusDays(long daysToAdd)
  2. Add days Using LocalDate.plus(TemporalAmount amountToAdd)
  3. Add days Using LocalDate.plus(long amountToAdd, TemporalUnit unit)

 

Comments