Add milliseconds to date

Opposite of subtracting milliseconds from a java date, this example will show how to add milliseconds to a date using Calendar.add, java 8 date time api, joda DateTime.plusMillis and apache commons DateUtils.addMilliseconds. In the examples below, we will set a date that represents new years eve, December 31st, then add milliseconds to return a date representing new years day or January 1st.

Straight up Java

@Test
public void add_milliseconds_to_date_in_java () {

    Calendar newYearsEve = Calendar.getInstance();
    newYearsEve.set(2012, 11, 31, 23, 59, 59);

    Calendar newYearsDay = Calendar.getInstance();
    newYearsDay.setTimeInMillis(newYearsEve.getTimeInMillis());
    newYearsDay.add(Calendar.MILLISECOND, 1000);

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

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

    assertTrue(newYearsDay.after(newYearsEve));
}

Output

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

Java 8 Date and Time API

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

@Test
public void add_milliseconds_to_date_in_java8 () {

    LocalDateTime newYearsEve = LocalDateTime.of(2012, Month.DECEMBER, 31, 23, 59, 59);
    LocalDateTime newYearsDay = newYearsEve.plus(1000, ChronoField.MILLI_OF_DAY.getBaseUnit());

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

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

    assertTrue(newYearsDay.isAfter(newYearsEve));
}

Output

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

Joda Time

Joda DateTime.plusMillis will return a copy the DateTime plus the specified number of milliseconds.

@Test
public void add_milliseconds_to_date_in_java_with_joda () {

    DateTime newYearsEve = new DateTime(2012, 12, 31, 23, 59, 59, 0);
    DateTime newYearsDay = newYearsEve.plusMillis(1000);

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

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

    assertTrue(newYearsDay.isAfter(newYearsEve));
}

Output

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

Apache Commons

Apache commons DateUtils.addMilliseconds will adds a number of milliseconds to the date returning a new object.

@Test
public void add_milliseconds_to_date_in_java_with_apachecommons () {

    Calendar newYearsEve = Calendar.getInstance();
    newYearsEve.set(2012, 11, 31, 23, 59, 59);

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

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

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

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

Output

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