Javaexercise.com

Java LocalDate minusDays() Method

Java minusDays() method is included in Java 8 version with DataTime API. It is used to subtract days from a date created by LocalDate class. It is helpful when we want to decrease a date by subtracting some number of days.

For example, 2020-01-01 minus one day would result in 2019-12-31.

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

Syntax

public LocalDate minusDays (long daysToSubtract)

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

Parameters:

daysToSubtract - It represents number of days to subtract, may be negative

Returns:

It returns a copy of the date after subtracting the days.

Exception:

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

How to Subtract Days from Date in Java

Lets takes an example in which we are subtracting days from a date by using minusDays() method. We can see after subtracting days date is decreased.

import java.time.LocalDate;

//Program to Subtract Days from a 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);
		// subtract days to date
		LocalDate newDate = date.minusDays(5);
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2016-05-03
New Date : 2016-04-28
 

How to Subtract days from the current date in Java

Let's take another example in which we are subtracting days from the current date.

We get the current date using now() method of LocalDate class that returns current date.

import java.time.LocalDate;

//Program to Subtract Days from a Date
public class Demo {  
	public static void main(String[] args){  
		
		// Take current date
	    LocalDate date = LocalDate.now();
		// Displaying date
		System.out.println("Date : "+date);
		// Subtract days to date
		LocalDate newDate = date.minusDays(4);
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2020-05-31
New Date : 2020-05-27
 

How to Add days to a date in Java

If we provide negative value as an argument in minusDays() method then it will add the days to the date.

Notice, in the below example, we passed negative integer in the method and the result is a 5 days ahead date.

import java.time.LocalDate;

//Program to Add Days to a 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);
		// add days to date
		LocalDate newDate = date.minusDays(-5); // negative integer
		System.out.println("New Date : "+newDate);
	}
}

Output:

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

Conclusion

Well, in this topic, we learnt To subtract number of days from a date which is created by LocalDate class. We used minusDays() method that returns a copy of date after subtracting the days.

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

 

Trending Topics