This article includes working examples of the LocalDate class in Java. Here, we used the minusDays() method of the LocalDate class to subtract days from the date. This method is known as the date adjustment method and returns a date object of the LocalDate class. Let's see the examples.
We used the now() method to get the current/today's date and then by using the minusDays() method, we subtracted the days from today's date. See the below example.
import java.time.LocalDate;
/*
* Code example to minus days from local date in Java
*/
public class JExercise {
public static void main(String[] args) {
// Current Date
LocalDate localDate = LocalDate.now();
System.out.println("Current Date: "+localDate);
// Minus days
LocalDate newLocalDate = localDate.minusDays(10);
System.out.println("Date After Minus Days: "+newLocalDate);
}
}
Output:
Current Date: 2021-02-18
Date After Minus Days: 2021-02-08
In case, you have a date in string then simply use the parse() method to get its date object and then use the minusDays() method to subtract days from this date. See the below example.
import java.time.LocalDate;
/*
* Code example to minus days from local date in Java
*/
public class JExercise {
public static void main(String[] args) {
// Some Date
String date = "2012-11-15";
LocalDate localDate = LocalDate.parse(date);
System.out.println("Current Date: "+localDate);
// Minus days
LocalDate newLocalDate = localDate.minusDays(10);
System.out.println("Date After Minus Days: "+newLocalDate);
}
}
Output:
Current Date: 2012-11-15
Date After Minus Days: 2012-11-05