Signup/Sign In

Java LocalDate plus(Temporal) Method

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

It takes an argument of TemporalAmount type and returns a copy of this date with the specified amount added.

The amount is typically Period but may be any other type implementing the TemporalAmount interface.

We can use Period class to add (days, month, year) to the date because it implements the TemporalAmount interface. The Period class provides the following methods:

  • of(int years, int months, int days)

  • ofDays(int days)

  • ofMonths(int months)

  • ofWeeks(int weeks)

  • ofYears(int years)

Syntax

public LocalDate plus(TemporalAmount amountToAdd)

Parameters:

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

Returns:

It returns a new localdate.

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.Period;
public class DateDemo {
	
	public static void main(String[] args){  
		LocalDate localDate = LocalDate.of(2011,02,28);
		System.out.println(localDate);
		localDate = localDate.plus(Period.ofDays(10));
		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 ofMonths() method of Period class to add 2 months the date and getting a date of after 2 months.

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


2011-02-28
2011-04-28

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.