How to add 4 Hours to the Current Date in Java?
In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would reach a java code to finally understand the proper way to accomplish this task.
To add hours to the current date, there exists a method known as the plus hours()method, which belongs to the java class called LocalDate class.
This method is encompassed in Java version 8throughDataTime API.
Users can easily increase the months as per their requirements.
To be able to use this class, the needs need to import a package known as java.time package.
Syntax:
LocalTimet2 = t1.plusHours(hours_to_be_increased);
Understanding the above syntax
- It is a variable to store the local or the current time.
- it is a method that adds the required no. of hours to the current time.
- It fetches the local or the current time.
- It refers to the number of hours that is to be added to the current time.
Let us see some of the examples to understand the working of the method.
Examples:
- Input - 15:41:39.843631 Output - 19:41:39.843631[ After adding 4 hours]
- Input - 14:41:39.843631 Output - 18:41:39.843631 [ After adding 4 hours]
- Input - 12:41:39.843631 Output - 16:41:39.843631 [ After adding 4 hours]
- Input - 10:41:39.843631 Output - 14:41:39.843631 [ After adding 4 hours]
- Input - 09:41:39.843631 Output - 13:41:39.843631 [ After adding 4 hours]
Now, let us see a java code to understand the topic.
Import the following package for the Calendar class in Java.
import java.util. time;
Implementation:
import java.time.*;
public class addingHours {
public static void main(String[] args)
{
LocalTime time1 = LocalTime.now();
System.out.println("Current Time : " + time1);
// adding four hours to the current time
LocalTime newTime1 = time1.plusHours(4);
System.out.println();
System.out.println("Time after 4 hours : " + newTime1);
System.out.println();
}
}
Output:

Explanation: In the above java code, we have added 4 hours to the current date with the help of a method called the plus hours () method in a class named adding hours().
Summary:
We learned how to add 4 hours to the current date in Java language. It was interesting to understand we can accomplish this task simply by taking a method called plus hours () into account and providing 4 as the parameter.
However, we can increase any number of hours by giving that number as a parameter in the method. We can also decrease the no. of hours by giving a negative integer as a parameter to the plus hours () method.