Javaexercise.com

How To Minus Minutes From LocalDateTime In Java

To subtract minutes from the local date-time, Java provides a class i.e. LocalDateTime, and a method i.e. minusMinutes() method.

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

Let's see the code below.

/* 
 *  Code example to minus minutes from date and time 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);

		// Minus 1 minute to the date
		LocalDateTime newDate = date.minusMinutes(1); 

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

Output:

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

The subtracted minutes 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 minusMinutes() method.

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

public LocalDateTime minusMinutes(long minutes)

Package Name: java.time;

Class Name: LocalDateTime

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

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

If you wish to subtract minutes from the current local date-time in Java, then use the below code.

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

/* 
 *  Code example to minus minutes from date and time in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// current date time
	    LocalDateTime date = LocalDateTime.now();

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

		// Minus 1 minute to the date
		LocalDateTime newDate = date.minusMinutes(1); 

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

Output:

Date : 2022-03-14T20:40:53.801755847
New Date : 2022-03-14T20:39:53.801755847

 

The minusMinutes() method accepts negative arguments as well. 

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

/* 
 *  Code example to minus minutes from date and time in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// current date time
	    LocalDateTime date = LocalDateTime.now();

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

		// Minus 1 minute to the date
		LocalDateTime newDate = date.minusMinutes(-1); 

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

Output:

Date : 2022-03-14T20:43:05.023837456
New Date : 2022-03-14T20:44:05.023837456