How to Add Hours to The Date Object in Java?10 Sept 2024 | 3 min read Java plusHours() function of the LocalDateTime class can be used to add hours to a time value. In this section we will going to learn how to add hours to the date object in Java In addition to the current date, we will also add hours to something like a string of dates. Using LocalDateTime ClassTo obtain the localdatetime object if you have a date in a String, first, parse the string using the parse() method. Use the plusHours() method for adding hours to it after that. File name: AddHours.java Output: The date is : 2023-01-19T17:28:13.048909208 The date after adding hours is : 2023-01-19T20:28:13.048909208 If we already have a localdatetime object, we can use the plusHours() function instead of the parse() method. Syntax The plusHours() function has the following signature. Here h denotes hours. Return Value: It does not return null, but rather a copy from LocalDateTime that has the requested number of hours added. It only accepts one long type value as a parameter. Exceptions: If the result is greater than the supported (either MIN or MAX) date range, a DateTimeException is thrown. This method call has no bearing on the initial instance (LocalDateTime), which is immutable. Adjusting the Time By an HourIn this case, the current time and date were obtained using the static now() function of the LocalDateTime class. In order to add hours to this date, we then utilized the plusHours() function. In Java, the LocalDateTime class represents local date and time. Year, month, day, hour, minute, second, and nanoseconds are all included in this class. File name: AddHours.java Output: The date is : 2023-01-19T17:28:13.048909208 The date after adding hours is : 2023-01-19T20:28:13.048909208 Subtraction of Hours in Date ObjectNegative values can also be used with the plusHours() method. It will therefore subtract the hours first from the date if we passed a negative integer. So, if we pass the negative value, we can claim that the hours are subtracted from the date rather than added. Check out the code example below. FileName: SubtractHours.java Output: The date is : 2023-01-19T17:28:13.048909208 The date after subtracting hours is : 2023-01-19T14:28:13.048909208 |
In Java, object reference equality is a fundamental concept that distinguishes between the comparison of object references and the comparison of the contents or state of the objects. Understanding this concept is crucial for developers to avoid common pitfalls in object comparisons, especially when working...
5 min read
An array of non-negative integers is given such that every number is occurring even number of times, barring one number which is occurring an odd number of times. The task is to find that number that is occurring an odd number of times. Examples: Input: a[] = {7,...
9 min read
Java is a versatile programming language recognized for its flexibility in managing various data structures. A vital idea in Java, referred to as padding, performs a critical function in managing memory, aligning records successfully, and optimizing statistics processing. In this section, we will discuss what padding...
5 min read
In Java, the Void class is a final and uninstantiated placeholder that holds a reference to the class object representing the Java void keyword. This class belongs to the java.lang package and does not have any methods, inheriting all its methods from the java.lang.Object class. Syntax: public...
5 min read
Multiplying polynomials is something everyone who does algebra or computer science needs to know and it is used in things such as signal processing, control systems and computation algebra. It may involve two polynomial and involving multiplying these two polynomials and add the term in the resultant...
5 min read
Introduction: In Java, knowing how many objects were created for a specific class is frequently helpful in Monitoring memory usage, keeping track of resource usage, and other performance-related tasks. Problem Statement: The problem is to count the number of objects created for a specific class in Java in an...
3 min read
? Terminating an application in Java might also look like a straightforward challenge, however there are various techniques to gracefully terminate a given program or forcefully terminate it in case of surprising issues. In this section, we will discuss various ways to terminate a Java program and...
4 min read
The javax.naming.CompositeName class has equals() function. The CompositeName class is used to determine whether or not two objects are equal by comparing this CompositeName with the given object that was passed as a parameter. The equals() method returns true if the objects are equal; otherwise, it...
6 min read
Java is a popular programming language used in the development of applications across various platforms. When working with dates, it is often necessary to extract specific information such as the day of the week from a given date. In this section, we will discuss how to...
4 min read
In Java, a package is a group of classes, interfaces, enumeration, and annotations. Java contains many pre-defined packages such as java.lang, java.io, java.net, etc. When we create any Java program the java.lang package is imported by default. We need not to write the package name at...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India