×

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

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Enum

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 iterators named Club, Diamond,...

3 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.