In this article, we will learn how to subtract Year or Month or DayOfMonth fields from LocalDate
Subtracting Day/Week/Month/Year from LocalDate:
- It is very simple to subtract Day or Week or Month or Year fields from LocalDate using below methods,
- minusDays() – Returns a copy of invoking
LocalDate
with the specified number of days subtracted - minusWeeks() – Returns a copy of invoking
LocalDate
with the specified number of weeks subtracted - minusMonths() – Returns a copy of invoking
LocalDate
with the specified number of months subtracted - minusYears() – Returns a copy of invoking
LocalDate
with the specified number of years subtracted
- minusDays() – Returns a copy of invoking
- In the below illustration, we are going to do below operations with current LocalDate,
- Subtract 5 Days from LocalDate using minusDays() method
- Subtract 2 Weeks from LocalDate using minusWeeks() method
- Subtract 3 Months from LocalDate using minusMonths() method
- Subtract 1 Year from LocalDate using minusYears() method
- Finally, print LocalDate to the console
SubtractFromLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
public class SubtractFromLocalDate {
public static void main(String[] args) {
// 1. get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- " + localDate);
// 1.1 subtract 5 days from current system date
LocalDate subtract_5_Days = localDate.minusDays(5);
System.out.println("\n1. After subtracting 5 Days from Current System Date is :- "
+ subtract_5_Days);
// 1.2 subtract 2 weeks from current system date
LocalDate subtract_2_Weeks = localDate.minusWeeks(2);
System.out.println("2. After subtracting 2 Weeks from Current System Date is :- "
+ subtract_2_Weeks);
// 1.3 subtract 3 months from current system date
LocalDate subtract_3_Months = localDate.minusMonths(3);
System.out.println("3. After subtracting 3 Months from Current System Date is :- "
+ subtract_3_Months);
// 1.4 subtract 1 year from current system date
LocalDate subtract_1_Year = localDate.minusYears(1);
System.out.print("4. After subtracting 1 Year from Current System Date is :- "
+ subtract_1_Year);
}
}
Output:
Current System Date is :- 2022-07-30
1. After subtracting 5 Days from Current System Date is :- 2022-07-25
2. After subtracting 2 Weeks from Current System Date is :- 2022-07-16
3. After subtracting 3 Months from Current System Date is :- 2022-04-30
4. After subtracting 1 Year from Current System Date is :- 2021-07-30
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!