×

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 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

  • LocalTime- It is a variable to store the local or the current time.
  • .plusHours () – it is a method that adds the required no. of hours to the current time.
  • LocalTime.now– It fetches the local or the current time.
  • hours_to_be_increased– 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 above method.

Examples:

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

import java.time.*;
public class addingHours {
   public static void main(String[] args)
    {
LocalTime time1 = LocalTime.now(); 
System.out.println("Current Time  : " + time1);
      // adding 24 hours to the current time
LocalTime newTime1 = time1.plusHours(24);
System.out.println();
System.out.println("Time after 24 hours : " + newTime1);
System.out.println();
    }
}

Output:

How to Add 24 Hours to Date in Java

Explanation: In the above code, we have added 24 hours to the current time. Here, the output is the same as the input since the time will be the same after 24 hours.

The method LocalTime.now() is used to fetch the current time.

The method time1.plusHours() is used to increase the time by 24 hours.

Summary:

We learned how to add 24 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 24 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

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.