To subtract weeks from the date-time in Java, you can use minusWeeks() 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 minusWeeks() method returns a copy of the local date-time object after subtracting the specified weeks.
For example, if you want to minus 2 weeks from the date "2022-03-14T17:28:13.048999208" then the resulted date-time will be "2022-02-28T17:28:13.048999208".
Let's understand with the running examples.
Here, we first created a date-time object by using the parse() method and then used the minusWeeks() method.
/*
* Code example to subtract weeks from 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);
// Subtract 2 weeks to the date
LocalDateTime newDate = date.minusWeeks(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2022-02-28T17:28:13.048999208
Let's see the method signature
public LocalDateTime minusWeeks(long weeks)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of LocalDateTime after subtracting the specified number of weeks, not null.
Parameters: It takes a single long type argument. 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.
This code minus 2 weeks from the current date-time. We first used the now() method to get the current date-time and then used the minusWeeks() method to subtract weeks from the current date-time.
/*
* Code example to subtract weeks from 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);
// Subtract 2 weeks to the date
LocalDateTime newDate = date.minusWeeks(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T18:20:33.085796037
New Date : 2022-03-01T18:20:33.085796037
This method accepts a negative value as an argument as well. So, if we pass a negative value to this method then it does the addition of weeks rather than subtraction.
We can use this method to add and subtract weeks. See the below Java code.
/*
* Code example to subtract weeks from 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);
// Subtract 2 weeks to the date
LocalDateTime newDate = date.minusWeeks(-2); // negative value
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T18:21:45.905681522
New Date : 2022-03-29T18:21:45.905681522
This method returns an exception if the date is not in the valid range. Here, we are trying to subtract weeks from the min date-time that's why it throws an exception.
/*
* Code example to subtract weeks from 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.MIN;
// Displaying date and time
System.out.println("Date : "+date);
// Subtract 2 weeks to the date
LocalDateTime newDate = date.minusWeeks(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : -999999999-01-01T00:00
Exception in thread "main" java.time.DateTimeException: Invalid value for EpochDay (valid values -365243219162 - 365241780471): -365243219176
Use this code to get results in a single line of code. If you are a beginner, skip this code.
LocalDateTime.now().minusWeeks(5);
This code will return a LocalDateTime object after subtracting 5 weeks from the current date-time. You just replace the value with your input and get the result instantly.