×

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:

  1. Input - 15:41:39.843631 Output - 19:41:39.843631[ After adding 4 hours]
  2. Input - 14:41:39.843631 Output - 18:41:39.843631 [ After adding 4 hours]
  3. Input - 12:41:39.843631 Output - 16:41:39.843631 [ After adding 4 hours]
  4. Input - 10:41:39.843631 Output - 14:41:39.843631 [ After adding 4 hours]
  5. 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:

How to Add 4 Hours to the Current Date in Java

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.


Related Topics

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Polygonal Number in Java

What does a Java Polygonal Number Mean? In mathematics, a polygonal number refers to a number that is expressed through dots or pebbles arranged in a regular polygonal pattern. Alphas are...

4 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Streams in Java

The conventional Java SE 8 version came with many new peculiarities, out of which the most striking of which are assuredly lambda expressions and the method references. Streams and Streams...

14 minutes read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Java Do While Loop

When we wish to test the exit condition at the end of the loop, we use a do-while loop. The do-while loop always executes its body at least once, because...

1 minute read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.