Javaexercise.com

How To Subtract Days From LocalDateTime In Java?

To subtract/minus days from the local date-time, Java provides a class i.e. LocalDateTime, and a method i.e. minusDays() method.

In this article, we are going to subtract days from local date-time with several running examples.

Let's see the code below.

/* 
 *  Code example to subtract days from localdatetime in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// String  date is given
		String strDate = "2022-03-14T17:28:13.048999208";
		// parse the string date into date time
		LocalDateTime date = LocalDateTime.parse(strDate);

		// Displaying date and time
		System.out.println("Date : "+date);

		// Subtract 2 days to the date
		LocalDateTime newDate = date.minusDays(2);

		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2022-03-14T17:28:13.048999208
New Date : 2022-03-12T17:28:13.048999208
 

The subtracted days are highlighted in black in the output

 

Here, we first get parsed the String date to the LocalDateTime object by using the parse() method. 

If you already have locadatetime object, then you don't need to parse it. 

You can directly call the minusDays() method.

Now,  let's have a look at this method signature:

public LocalDateTime minusDays(long days)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns a copy of localdatetime after subtracting the specified number of days, not null.

Parameters: It takes a single long type value. It may be negative.

Exceptions: It throws a DateTimeException if the result exceeds the supported(either MIN or MAX) date range.

Version: Since 1.8

Note: Since the LocalDateTime instance is immutable, the date-time will not be affected by this method call.

How to subtract days from the current local date-time in Java

If you want to subtract days from the current local date-time then use the below code.

Here, we first get the current local date-time by using the now() method, and then we used the minusDays() to subtract the days.

/* 
 *  Code example to subtract days from localdatetime in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time
		LocalDateTime date = LocalDateTime.now();

		// Displaying date and time
		System.out.println("Date : "+date);

		// Subtract 2 days to the date
		LocalDateTime newDate = date.minusDays(2);

		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2022-03-15T18:30:38.132379387
New Date : 2022-03-13T18:30:38.132379387
 

The minusDays() method accepts negative arguments as well. 

If we pass the days as negative values then it does the reversal operation and adds the days rather than subtracting. See the code below.

/* 
 *  Code example to subtract days from localdatetime in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time
		LocalDateTime date = LocalDateTime.now();

		// Displaying date and time
		System.out.println("Date : "+date);

		// Subtract 2 days to the date
		LocalDateTime newDate = date.minusDays(-2); // negative value

		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2022-03-15T18:31:35.570283745
New Date : 2022-03-17T18:31:35.570283745
 

Any value that goes out of the valid date range leads to DataTimeException. See the code below.

/* 
 *  Code example to subtract days from localdatetime in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time
		LocalDateTime date = LocalDateTime.MAX;

		// Displaying date and time
		System.out.println("Date : "+date);

		// Subtract 2 days to the date
		LocalDateTime newDate = date.minusDays(-2); // negative value

		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : +999999999-12-31T23:59:59.999999999
Exception in thread "main" java.time.DateTimeException: Invalid value for Year (valid values -999999999 - 999999999): 1000000000