Subtract milliseconds from date

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

Straight up Java

@Test
public void subtract_milliseconds_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.MILLISECOND, -1000);

    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss S z");

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

    assertTrue(newYearsEve.before(newYearsDay));
}

Output

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

Java 8 Date and Time API

Java 8 LocalDateTime.minus will return a copy of the LocalDateTime with the specified amount subtracted, in this case ChronoField.MILLI_OF_DAY.

@Test
public void subtract_milliseconds_from_date_in_java8() {

    LocalDateTime newYearsDay = LocalDateTime.of(2013, Month.JANUARY, 1, 0,
            0, 0, 0);
    LocalDateTime newYearsEve = newYearsDay.minus(1,
            ChronoField.MILLI_OF_DAY.getBaseUnit());

    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:59 9

Joda Time

Joda DateTime.minusMillis will return a copy the DateTime minus the specified number of milliseconds.

@Test
public void subtract_milliseconds_from_date_in_java_with_joda () {

    DateTime newYearsDay = new DateTime(2013, 1, 1, 0, 0, 0, 0);
    DateTime newYearsEve = newYearsDay.minusMillis(60);

    DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss S z");

    logger.info(newYearsDay.toString(fmt));
    logger.info(newYearsEve.toString(fmt));

    assertTrue(newYearsEve.isBefore(newYearsDay));
}

Output

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

Apache Commons

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

@Test
public void subtract_milliseconds_from_date_in_java_apachecommons () {

    Calendar newYearsDay = Calendar.getInstance();
    newYearsDay.set(2013, 0, 1, 0, 0, 0);

    Date newYearsEve = DateUtils.addMilliseconds(newYearsDay.getTime(), -1000);

    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:59 CST