Display Before some Hours and Minutes from Current Datetime


This Java program demonstrates how to subtract a certain amount of hours and minutes from the current date and time using the LocalDateTime class.

The program first obtains the current date and time using the now() method of the LocalDateTime class. It then subtracts 7 hours and 30 minutes from the current date and time using the minusHours() and minusMinutes() methods of the LocalDateTime class.

The result is printed to the console using the println() method of the System.out object. The first line displays the current date and time, while the second line shows the date and time 7 hours and 30 minutes earlier.

Source Code

import java.time.*;
public class Before_HourMinute
{
	public static void main(String[] args)
	{	
		LocalDateTime dateTime = LocalDateTime.now().minusHours(7).minusMinutes(30);
		System.out.println("Current Date and Time :  " + LocalDateTime.now());
		System.out.println("Before 5 Hours and 30 Minutes: " + dateTime);  
	}
}

Output

Current Date and Time :  2022-11-05T13:15:46.978329700
Before 5 Hours and 30 Minutes: 2022-11-05T05:45:46.962705300

Example Programs