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
Output
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.
Output
Joda Time
Joda DateTime.minusMillis will return a copy the DateTime minus the specified number of milliseconds.
Output
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.