Subtract minutes from date

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

Straight up Java

@Test
public void subtract_minutes_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.MINUTE, -1);       
    
    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.minusMinutes will return a copy of the LocalDateTime with the specified number of minutes subtracted.

@Test
public void subtract_minutes_from_date_in_java8 () {

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

    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 CST
12/31/2012 23:59:00 CST

Joda Time

Joda DateTime.minusMinutes will return a copy the DateTime minus the specified number of minutes.

@Test
public void subtract_minutes_from_date_in_java_with_joda () {
    
    DateTime newYearsDay = new DateTime(2013, 1, 1, 0, 0, 0, 0);
    DateTime newYearsEve = newYearsDay.minusMinutes(1);

    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.addMinutes will adds a number of minutes, in this case a negative number of minutes, to the date returning a new object.

@Test
public void subtract_minutes_from_date_in_java_apachecommons () {
    
    Calendar newYearsDay = Calendar.getInstance();
    newYearsDay.set(2013, 0, 1, 0, 0, 0);
    
    Date newYearsEve = DateUtils.addMinutes(newYearsDay.getTime(), -1);
    
    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:00 CST