How to subtract seconds from date

Opposite of adding seconds to a java date, this example shows how to subtract seconds from a date using java's Calendar.add, java 8 date time api, joda’s DateTime.minusSeconds and apache commons DateUtils.addSeconds. In each of the examples below, we will set a date that represents new years day or January 1st. Then we will subtract a specified number of seconds returning a date that reperesents new years eve or December 31st.

Straight up Java

@Test
public void subtract_seconds_from_date_in_java () {
    
    Calendar newYearsDay = Calendar.getInstance();
    newYearsDay.set(2013, 0, 1, 0, 0, 0);
    
    Calendar newYearsEve = Calendar.getInstance();
    newYearsEve.setTimeInMillis(newYearsDay.getTimeInMillis());
    newYearsEve.add(Calendar.SECOND, -60);      
    
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");

    logger.info(dateFormatter.format(newYearsDay.getTime()));
    logger.info(dateFormatter.format(newYearsEve.getTime()));

    assertTrue(newYearsEve.before(newYearsDay));
}

Output

01/01/2013 00:00:00 CST
12/31/2012 23:59:00 CST

Java 8 Date and Time API

Java 8 LocalDateTime.minusSeconds will return a copy of the LocalDateTime with the specified number of seconds subtracted.

@Test
public void subtract_seconds_from_date_in_java8() {

    LocalDateTime newYearsDay = LocalDateTime.of(2013, Month.JANUARY, 1, 0,
            0);
    LocalDateTime newYearsEve = newYearsDay.minusSeconds(60);

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

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

    assertTrue(newYearsEve.isBefore(newYearsDay));
}

Output

01/01/2013 00:00:00 0
12/31/2012 23:59:00 0

Joda Time

Joda DateTime.minusSeconds will return a copy the DateTime minus the specified number of seconds.

@Test
public void subtract_seconds_from_date_in_java_with_joda () {
    
    DateTime newYearsDay = new DateTime(2013, 1, 1, 0, 0, 0, 0);
    DateTime newYearsEve = newYearsDay.minusSeconds(60);

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

    assertTrue(newYearsEve.isBefore(newYearsDay));
}

Output

01/01/2013 00:00:00 CST
12/31/2012 23:59:00 CST

Apache Commons

Apache commons DateUtils.addSeconds will adds a number of seconds, in this case a negative number of seconds, to the date returning a new object.

@Test
public void subtract_seconds_from_date_in_java_apachecommons () {
    
    Calendar newYearsDay = Calendar.getInstance();
    newYearsDay.set(2013, 0, 1, 0, 0, 0);
    
    Date newYearsEve = DateUtils.addSeconds(newYearsDay.getTime(), -58);
    
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");

    logger.info(dateFormatter.format(newYearsDay.getTime()));
    logger.info(dateFormatter.format(newYearsEve));

    assertTrue(newYearsEve.before(newYearsDay.getTime()));
}

Output

01/01/2013 00:00:00 CST
12/31/2012 23:59:02 CST