Javaexercise.com

Java LocalDate plusDays() Method

Java plusDays() method is included in Java 8 version with DataTime API. It is used to add days to a date created by LocalDate class. It is helpful when we want to increase a date by adding some number of days. It takes an argument of long type and returns a copy of the date.

In this topic, we will learn how to add days to a date with examples. Syntax of the method is given below.

 

Syntax

public LocalDate plusDays (long daysToAdd)

This method belongs to Java 8 LocalDate class which is stored into java.time package and method description is given below..

 

Parameters:

daysToAdd - Number of days to add

Returns:

It returns a copy of the LocalDate by adding days.

Exception:

It throws an exception: DateTimeException if the result exceeds the supported date range.

 

How to Add Days to Date in Java

Let's take an example in which we are adding days to a date by using plusDays() method. We can see after adding days date is increased.

import java.time.LocalDate;

// Code to Add Days in a Java 8 Date
public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
	    LocalDate date = LocalDate.parse("2016-05-03");
		// Displaying date
		System.out.println("Date : "+date);
		// Adding days to date
		LocalDate newDate = date.plusDays(5);
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2016-05-03
New Date : 2016-05-08
 

 

Adding days to current date in Java

Let's take another example in which we are adding days to the current date. We get current date using now() method of LocalDate class that returns current date.

import java.time.LocalDate;

// Code to Add Days in a Java 8 Date
public class Demo {  
	public static void main(String[] args){  
		
		// Take current date
	    LocalDate date = LocalDate.now();
		// Displaying date
		System.out.println("Date : "+date);
		// Adding days to date
		LocalDate newDate = date.plusDays(2);
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2020-05-31
New Date : 2020-06-02
 

How to Subtract days from a date in Java

If we provide negative value as an argument in plusDays() method then it will subtract the days from the date. Notice in the below example, we passed negative integer in the method and the result is a 2 days previous date.

import java.time.LocalDate;

// Program to Subtract Days in a Java 8 Date
public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
	    LocalDate date = LocalDate.parse("2016-05-03");
		// Displaying date
		System.out.println("Date : "+date);
		// subtracting days to date
		LocalDate newDate = date.plusDays(-2); // negative integer
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2016-05-03
New Date : 2016-05-01
 

 


Conclusion

Well, in this topic, we learnt To add number of days to a date which is created by LocalDate class. This method returns a copy of date after adding the days, notice the examples we provided.

If we missed something, you can suggest us at - info.javaexercise@gmail.com