×

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

How to Return Value from Lambda Expression Java?

What is Lambda Expression in Java? In Java 8, Lambda Expressions were introduced.A lambda expression is a brief section of code that accepts input and outputs a value. Similar to methods,...

4 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Java String split() method

Java String split() method split current String against given regular expression and returns a char array. Syntax: public String split(String regex)                 public String split(String regex, int...

2 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 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.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.