Opposite of adding seconds to a java date, this example shows how to subtract seconds from a date using java's Calendar.add, java 8 date time api, joda’s DateTime.minusSeconds and apache commons DateUtils.addSeconds. In each of the examples below, we will set a date that represents new years day or January 1st. Then we will subtract a specified number of seconds returning a date that reperesents new years eve or December 31st.
Straight up Java
Output
Java 8 Date and Time API
Java 8 LocalDateTime.minusSeconds will return a copy of the LocalDateTime with the specified number of seconds subtracted.
Output
Joda Time
Joda DateTime.minusSeconds will return a copy the DateTime minus the specified number of seconds.
Output
Apache Commons
Apache commons DateUtils.addSeconds will adds a number of seconds, in this case a negative number of seconds, to the date returning a new object.