From Android doc:
Consider a GregorianCalendar originally set to August 31, 1999. Calling add(Calendar.MONTH, 13) sets the calendar to September 30, 2000.
(Suppose you have a calendar set to August 31, 1999, so calling add (Calendar.MONTH, 13); will set that calendar to September 30, 2000.)
Add rule 1 sets the MONTH field to September, since adding 13 months to August gives September of the next year -(Add method, Rule 1: If you add 13 months to August, it will be September of the following year, so the date will be set to September of the following year.)
Add rule 2 sets the DAY_OF_MONTH to 30, the closest possible value.
In other words Calendar calendar = Calendar.getInstance (); // Suppose this is set to 2017/5/31. calendar.add (Calendar.DATE, 1); // Since there is no date of 2017/5/32, it is automatically set to 2017/06/01. In the case of "yesterday" calendar.add (Calendar.DATE, -1); // Minus is subtraction
It can be expressed by intuitive addition and subtraction like this.
Recommended Posts