To add days to the date-time in Java, you can use the plusDays() method of the LocalDateTime class.
This method was added to Java 8 date-time API. So, can be used in Java 8 and higher versions such as Java 11, Java 17, etc.
The plusDays() method returns a local date-time object after adding the specified days.
For example, if you want to add 2 days to this "2022-03-14T17:28:13.048999208" then the resulted date-time will be "2022-03-16T17:28:13.048999208".
Let's understand with the running examples.
This code example explains how we first created a LocalDateTime object from a string date-time by using the parse() method and then used the plusDays() method to add 2 days
If you already have the object of LocalDateTime class the don't need to use the parse() method.
You can directly call the plusDays() method. You can also use this method to increment date-time by 1, 2, or any number of days.
/*
* Code example to add days to local date 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 days to the date
LocalDateTime newDate = date.plusDays(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2022-03-16T17:28:13.048999208
Let's see the signature of this method:
public LocalDateTime plusDays(long days)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of localdatetime after adding the specified number of days, 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.
Version: Since 1.8
Let's see some more examples.
We can use this method to add days to the current date-time as well. We first used the now() method to get the current date-time and then used the plusDays() method.
/*
* Code example to add days to local date 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 days to the date
LocalDateTime newDate = date.plusDays(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T11:53:43.720695963
New Date : 2022-03-17T11:53:43.720695963
The plusDays() method accepts a negative value as an argument as well. This method subtracts days if the argument is negative. So, we can use this method to subtract days as well along with addition.
Here, we first used the now() method to get the current date-time and then used the plusDays() method to subtract days by passing a negative value.
/*
* Code example to add days to local date 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 days to the date
LocalDateTime newDate = date.plusDays(-2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T11:54:46.410240010
New Date : 2022-03-13T11:54:46.410240010
Use this code to get results in a single line of code. If you are a beginner, skip this code.
LocalDateTime.now().plusDays(5);
This code will return a local date-time object after adding 5 days to the current date. You just replace the value with your input and get the result instantly.