Subtract hours from date

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

Straight up Java

@Test
public void subtract_hours_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.HOUR, -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:00:00 CST

Java 8 Date and Time API

Java 8 LocalDateTime.minusHours will return a copy of the LocalDateTime with the specified number of hours subtracted.

@Test
public void subtract_hours_from_date_in_java8 () {
    
    LocalDateTime newYearsDay = LocalDateTime.of(2013, Month.JANUARY, 1, 0, 0);
    LocalDateTime newYearsEve = newYearsDay.minusHours(1);
    
    java.time.format.DateTimeFormatter formatter = 
             java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");

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

    assertTrue(newYearsEve.isBefore(newYearsDay));
}

Output

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

Joda Time

Joda DateTime.minusHours will return a copy the DateTime minus the specified number of hours.

@Test
public void subtract_hours_from_date_in_java_with_joda () {
    
    DateTime newYearsDay = new DateTime(2013, 1, 1, 0, 0, 0, 0);
    DateTime newYearsEve = newYearsDay.minusHours(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:00:00 CST

Apache Commons

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

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