Assuming that you speak about class GregorianCalendar
- Instead of calendar1.add(Calendar.MONTH, 1)
try to also call following method as work-around:
static GregorianCalendar moveToEndOfMonth(GregorianCalendar gcal) {
gcal.add(Calendar.MONTH, 1); // moving to some day of next month
gcal.set(Calendar.DAY_OF_MONTH, 1); // moving to first day of current month
gcal.add(Calendar.DATE, -1); // moving to last day of previous month
}
So your final code should look like:
calendar1.add(Calendar.MONTH, 1);
moveToEndOfMonth(calendar1);
Why? The analysis of @DavidCAdams is right, see his answer.