LocalDateTime class is used to create dates and times in Java. This class contains several methods to perform date and time-related operations.
In this article, we will learn to add weeks to the date-time in Java,
To add weeks, we can use plusWeeks() method of this class that returns a copy of the local date-time object after adding the specified weeks.
For example, if we want to add 1 week to "2022-03-14T17:28:13.048999208", then the result will be "2022-03-21T17:28:13.048999208".
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.
Let's understand with the running examples.
In this example, we are adding 1 week to the date by using the plusWeeks() method but first, we created a date-time object by using the parse() method and then used the method.
/*
* Code example to add weeks 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 1 week to the date
LocalDateTime newDate = date.plusWeeks(1);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2022-03-21T17:28:13.048999208
Now, let's see its signature
public LocalDateTime plusWeeks(long weeks)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of localdatetime after adding the specified number of weeks, 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.
Adding Weeks to the Current Date-Time in Java
Here, we used the now() method of the LocalDateTime class to get the current date-time and then used the plusWeeks() method to add 1 week to it. See the below Java code.
/*
* Code example to add weeks 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 1 week to the date
LocalDateTime newDate = date.plusWeeks(1);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T12:01:23.828421713
New Date : 2022-03-22T12:01:23.828421713
The plusWeeks() method accepts a negative value as an argument as well. It works the opposite in the case of a negative argument which means it minus weeks from date-time rather than adding.
/*
* Code example to add weeks 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 weeks to the date
LocalDateTime newDate = date.plusWeeks(-2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T12:02:05.947599532
New Date : 2022-03-01T12:02:05.947599532
Use this code to get results in a single line of code. If you are a beginner, skip this code.
LocalDateTime.now().plusWeeks(5);
This code returns a LocalDateTime object after adding 5 weeks to the current date-time. You just replace the value with your input and get the result instantly.