How to add hours to LocalDateTime 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.
Adding hours to localdatetime in Java
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.
package javaexample;
/*
* 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.
Syntax
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.
Adding hours to the current local date and time in Java
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.
package javaexample;
/*
* 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
Passing negative argument to the plusHours()
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.
package javaexample;
/*
* 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