Java Language Calendar and its Subclasses Increasing / Decreasing calendar fields

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

add() and roll() can be used to increase/decrease Calendar fields.

Calendar calendar = new GregorianCalendar(2016, Calendar.MARCH, 31); // 31 March 2016

The add() method affects all fields, and behaves effectively if one were to add or subtract actual dates from the calendar

calendar.add(Calendar.MONTH, -6);

The above operation removes six months from the calendar, taking us back to 30 September 2015.

To change a particular field without affecting the other fields, use roll().

calendar.roll(Calendar.MONTH, -6);

The above operation removes six months from the current month, so the month is identified as September. No other fields have been adjusted; the year has not changed with this operation.



Got any Java Language Question?