Javaexercise.com

How To Subtract Hours From LocalDateTime In Java?

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

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

Let's see the code below.

/* 
 *  Code example to minus hours 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);

		// Add 2 hours to the date
		LocalDateTime newDate = date.minusHours(2); 

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

Output:

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

The subtracted hours 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 minusHours() method.

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

public LocalDateTime minusHours(long hours)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns a copy of localdatetime after subtracting the specified number of hours, 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 hours from  the current local date-time in Java

If you wish to subtract the hours 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 minusHours() method. See the below code.

/* 
 *  Code example to minus hours 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);
		
		// Add 2 hours to the date
		LocalDateTime newDate = date.minusHours(2); 
		
		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2022-03-14T20:12:01.352442320
New Date : 2022-03-14T18:12:01.352442320

 

The minusHours() method accepts negative arguments as well. 

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

package javaexample;
/* 
 *  Code example to minus hours 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 date into date time
		LocalDateTime date = LocalDateTime.parse(strDate);

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

		// Add 2 hours to the date
		LocalDateTime newDate = date.minusHours(-2); 

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

Output:

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