Signup/Sign In
LAST UPDATED: NOVEMBER 27, 2020

Java LocalDate plus(long, Temporal) Method

The plus() method is used to add amount of unit to the date. The unit can be a day, month, week etc.

This method returns a copy of this date with the specified amount added.

We can use ChronoUnit enum to add unit to the date because it implements the TemporalUnit interface. The ChronoUnit provides the following fields:

  • DAYS

  • WEEKS

  • MONTHS

  • YEARS

  • DECADES

  • CENTURIES

  • MILLENNIA

  • ERAS

Syntax

public LocalDate plus(long amountToAdd, TemporalUnit unit)

Parameters:

amountToAdd - the amount of the unit to add to the date.

unit - the unit of the amount to add like: days, months etc.

Returns:

It returns a new localdate after adding unit to the date.

Time for an Example:

Let's take an example to add days to a date. Here, we are using plus() method to add 10 days to the localdate. In result, it returns a date of next month after 10 days.

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DateDemo {
	
	public static void main(String[] args){  
		LocalDate localDate = LocalDate.of(2011,02,28);
		System.out.println(localDate);
		localDate = localDate.plus(10, ChronoUnit.DAYS);
		System.out.println(localDate);		
	}
}


2011-02-28
2011-03-10

Time for another Example:

Let's take another example to understand the plus() method. Here, we are using WEEKS unit to add 2 weeks the date and getting a date of after 2 weeks.

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DateDemo {
	
	public static void main(String[] args){  
		LocalDate localDate = LocalDate.of(2011,02,28);
		System.out.println(localDate);
		localDate = localDate.plus(2, ChronoUnit.WEEKS);
		System.out.println(localDate);		
	}
}


2011-02-28
2011-03-14

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.