Javaexercise.com

How To Subtract Months From LocalDateTime In Java?

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

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

Let's see the code below.

/* 
 *  Code example to subtract months 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 4 months to the date
		LocalDateTime newDate = date.minusMonths(4);

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

Output:

Date : 2022-03-14T17:28:13.048999208
New Date : 2021-11-14T17:28:13.048999208
 

The subtracted months are highlighted in black in the output

 

In the above code, 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 minusMonths() method.

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

public LocalDateTime minusMonths(long months)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns a copy of localdatetime after subtracting the specified number of months, 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

This method subtracts the specified amount from the months field in three steps:

  1. Subtract the input months from the month-of-year field
  2. Check if the resulting date would be invalid
  3. Adjust the day-of-month to the last valid day if necessary

For example, 2007-03-31 minus one month would result in the invalid date 2007-04-31. Instead of returning an invalid result, the last valid day of the month, 2007-04-30, is selected instead.

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

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

Here, we used the now() method to get the current date-time and then used the minusMonths() method. See the below code.

/* 
 *  Code example to subtract months 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 1 months to the date
		LocalDateTime newDate = date.minusMonths(1);

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

Output:

Date : 2022-03-15T18:37:53.741945937
New Date : 2022-02-15T18:37:53.741945937
 

The minusMonths() method accepts negative arguments as well. 

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

/* 
 *  Code example to subtract months 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);

		// add 2 months to the date
		LocalDateTime newDate = date.minusMonths(-2);

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

Output:

Date : 2022-03-15T18:38:45.263821920
New Date : 2022-05-15T18:38:45.263821920