Opposite of adding years to a java date, this example demonstrates how to subtract a number of years from a date using java Calendar.add, java 8 date time api, joda DateTime.minusYears and DateUtils.addYears. In each of the examples below, we will set a date that represents super bowl XLV between Pittsburgh Steelers and Green Bay Packers. Next we will subtract 14 years which represents the number of years between Green Bay packers superbowl wins. Super Bowl XXXI was played between New England Patriots and Green Bay Packers.
Straight up Java
Output
Java 8 Date and Time API
Java 8 LocalDateTime.minusYears will return a copy of the LocalDateTime with the specified number of years subtracted.
Output
Joda Time
Joda DateTime.minusYears will return a copy the DateTime minus the specified number of years.
Output
Apache Commons
Apache commons DateUtils.addYears will adds a number of years, in this case a negative number of years, to the date returning a new object.