To add hours to time in Java, you can use plusHours() method of the LocalDateTime class in Java.
This article introduces how to add hours to localdatetime in Java.
We will add hours to a string date and current date as well. Let's see the running examples.
If you have a date in String then first parse it using the parse() method to get the localdatetime object.
After that use the plusHours() method to add hours to it.
/*
* Code example to add hours to date and time in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:28:13.048999208";
// parse the date into date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Add 2 hours to the date
LocalDateTime newDate = date.plusHours(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2022-03-14T19:28:13.048999208
If you already have localdatetime object then no need to use the parse() method, just use the plusHours() method directly.
The signature of the plusHours() method is as below.
public LocalDateTime plusHours(long hours)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of localdatetime after adding the specified number of hours, not null.
Parameters: It takes a single long type value. It may be negative.
Exceptions: It throws a DateTimeException if the result exceeds the supported(either MIN or MAX) date range.
This original instance(localdatetime) is immutable and unaffected by this method call.
The LocalDateTime is a class that represents local date and time in Java.
This class consists of year, month, date, hour, minute, second, and nanoseconds. See the below datetime formation.
Here, we used the static now() method of LocalDateTime class to get the current date and time.
After that, we used the plusHours() to add hours to this date.
/*
* Code example to add hours to date and time in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// Current date and time
LocalDateTime date = LocalDateTime.now();
// Displaying date and time
System.out.println("Date : "+date);
// Add 2 hours to the date
LocalDateTime newDate = date.plusHours(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T18:18:18.704649698
New Date : 2022-03-14T20:18:18.704649698
The plusHours() method allows negative value as well. So if we passed a negative number then it subtract the hours from the date.
So we can say that if we pass a negative value then it subtracts the hours rather than adding to the date. See the code example below.
/*
* Code example to add hours to date and time in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:28:13.048999208";
// parse the date into date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Add 2 hours to the date
LocalDateTime newDate = date.plusHours(-2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2022-03-14T15:28:13.048999208