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
Output
Java 8 Date and Time API
Java 8 LocalDateTime.minusHours will return a copy of the LocalDateTime with the specified number of hours subtracted.
Output
Joda Time
Joda DateTime.minusHours will return a copy the DateTime minus the specified number of hours.
Output
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.