Subtract years from date

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

@Test
public void subtract_years_from_date_in_java () {
    
    Calendar superBowlXLV = Calendar.getInstance();
    superBowlXLV.set(2011, 1, 6, 0, 0);
    
    Calendar numberFour = Calendar.getInstance();
    numberFour.setTimeInMillis(superBowlXLV.getTimeInMillis());
    numberFour.add(Calendar.YEAR, -14);     
    
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");

    logger.info(dateFormatter.format(superBowlXLV.getTime()));
    logger.info(dateFormatter.format(numberFour.getTime()));

    assertTrue(numberFour.before(superBowlXLV));
}

Output

02/06/2011 00:00:13 CST
02/06/1997 00:00:13 CST

Java 8 Date and Time API

Java 8 LocalDateTime.minusYears will return a copy of the LocalDateTime with the specified number of years subtracted.

@Test
public void subtract_years_from_date_in_java8() {

    LocalDateTime newYearsDay = LocalDateTime.of(2011, Month.FEBRUARY, 6,
            0, 0);
    LocalDateTime numberFour = newYearsDay.minusYears(14);

    java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
            .ofPattern("MM/dd/yyyy HH:mm:ss S");

    logger.info(newYearsDay.format(formatter));
    logger.info(numberFour.format(formatter));

    assertTrue(numberFour.isBefore(newYearsDay));
}

Output

02/06/2011 00:00:00 0
02/06/1997 00:00:00 0

Joda Time

Joda DateTime.minusYears will return a copy the DateTime minus the specified number of years.

@Test
public void subtract_years_from_date_in_java_joda () {

    DateTime superBowlXLV = new DateTime(2011, 2, 6, 0, 0, 0, 0);
    DateTime numberFour = superBowlXLV.minusYears(14);

    DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss z");
    
    logger.info(superBowlXLV.toString(fmt));
    logger.info(numberFour.toString(fmt));

    assertTrue(numberFour.isBefore(superBowlXLV));
}

Output

02/06/2011 00:00:00 CST
02/06/1997 00:00:00 CST

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.

@Test
public void subtract_years_from_date_in_java_apachecommons () {
    
    Calendar superBowlXLV = Calendar.getInstance();
    superBowlXLV.set(2011, 1, 6, 0, 0);
    
    Date numberFour = DateUtils.addYears(superBowlXLV.getTime(), -14);
    
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");

    logger.info(dateFormatter.format(superBowlXLV.getTime()));
    logger.info(dateFormatter.format(numberFour));

    assertTrue(numberFour.before(superBowlXLV.getTime()));      
}

Output

02/06/2011 00:00:20 CST
02/06/1997 00:00:20 CST