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 |
The java.io.ObjectInputStream class is used to deserialize objects and primitive data iously serialized using the ObjectOutputStream. It allows the reconstruction of object graphs and ensures compatibility between the serialized object's class and the current JVM (Java Virtual Machine) class definitions. ObjectOutputStream and ObjectInputStream collaborate to save and...
22 min read
The Sieve of Eratosthenes is an ancient and efficient algorithm used to find all prime numbers up to a given limit. Named after the Greek mathematician Eratosthenes of Cyrene, this algorithm has stood the test of time and remains a fundamental concept in number theory and...
4 min read
Index mapping, also known as trivial hashing, is a technique used to map an array element to an index in a new array. This can be used to efficiently perform operations such as finding duplicates or counting occurrences of elements in an array. One common implementation...
10 min read
The Word Break Problem is about deciding whether a specific string can be divided into valid words that are present in a given dictionary. The objective is to determine if the string can be segmented into one or more words from this list. This problem can...
16 min read
The ability to do basic mathematical calculations on numeric data types makes arithmetic operators one of Java's most often-used operators. Java has multiple kinds of arithmetic operators, each with a unique set of features and syntax. The numerous kinds of arithmetic operators in Java will...
3 min read
The Classes and the objects are the foundation of the Java programming language because it is an object-oriented language. We have used a variables of type Object whenever we need to store just one object in our programme. However, using an Array of items is better...
3 min read
We are already familiar with the JUnit testing framework. It is used to perform unit testing in Java. The JUnit test case is the set of code that ensures whether our program code works as expected or not. JUnit is a widely-used testing framework in Java...
4 min read
Initially, there are many methods and logic to find the first non-repeating character in a string, and it just needs implementation. For implementation, we need to understand the logic, and we need to have a full grip on the programming language. Before implementing the logic using...
7 min read
Java is a popular programming language used by developers around the world to build a wide range of applications. Despite its popularity and reliability, Java programs are prone to errors and exceptions. One of the most common exceptions in Java is the ClassNotFoundException. In this article,...
4 min read
Java is one of the most popular programming languages. Java provides a rich set of libraries, and its standard Java library is a very powerful that contains libraries such as java.lang, java.util, and java.math, etc. Java provides more than thousands of libraries except standard libraries. Some...
5 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