LAST UPDATED: NOVEMBER 24, 2020
Java LocalDate minusWeeks() Method
Java minusWeeks()
method is used to minus the specified number of weeks from a date. It subtracts the weeks and returns a new LocalDate
. For example, 2009-10-10 minus one week would result in 2009-10-03.
It takes a long type argument that represents the number of weeks and returns a new LocalDate
. The syntax of the method is given below.
Syntax
public LocalDate minusWeeks(long weeksToSubtract)
Parameters:
It takes a parameter of long type that represents number of weeks.
Returns:
It returns a local-date after subtracting the specified weeks from a date.
Time for an Example:
Let's take an example to subtract weeks from a date. Here, we are subtracting 2 weeks by using the minusWeeks()
method that returns a new local-date. See the below example.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2016, 10, 21);
System.out.println(localDate);
localDate = localDate.minusWeeks(2);
System.out.println("New date : "+localDate);
}
}
2016-10-21
New date : 2016-10-07
Time for another Example:
Let's take another example to understand the minusWeeks()
method. Here, we are subtracting 2 weeks from the current date. See the example below.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
localDate = localDate.minusWeeks(2);
System.out.println("New date : "+localDate);
}
}
2020-06-10
New date : 2020-05-27
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.