To subtract years from date, you can use the minusYears() method of the LocalDateTime class of Java 8.
This method returns a copy of date-time after subtracting the specified years from the date.
For example, if you subtract 2 years from the date "2022-03-14T17:28:13.048999208"
/*
* Code example to subtract years from 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 years from the date
LocalDateTime newDate = date.minusYears(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2020-03-14T17:28:13.048999208
public LocalDateTime minusYears(long years)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of localdatetime object after subtracting the specified number of years, 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
This method is useful if you are working with datetime object in java. If you have only date object then use this method of LocalDate class.
Let' see some more examples.
You can use the minusYears() method to minus years from the current date-time as well.
In this article, we used the now() method to get the current date-time and then used the minusYears() to get the desired result. See the below Java code.
/*
* Code example to subtract years from 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 years from the date
LocalDateTime newDate = date.minusYears(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T18:43:25.767649708
New Date : 2020-03-15T18:43:25.767649708
The minusYears() method accepts the negative value as an argument as well. So, you can use this method to add years to the date-time by passing the negative argument.
See the below Java code.
Here, we passed -5 years to subtract but rather than subtraction it adds the years and returns a date 5 years ahead.
/*
* Code example to subtract years from local datetime 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 5 years from the date
LocalDateTime newDate = date.minusYears(-5); // negative value
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-15T18:44:32.351862082
New Date : 2027-03-15T18:44:32.351862082
Use this code to get results in a single line of code. If you are a beginner, skip this code.
LocalDateTime.now().minusYears(5);
This code will return a localdatetime object after subtracting 5 years from the current date-time. You just replace the value with your input and get the result instantly.