I\'ve read around and basically I\'ve figured out that the Calendar object is capable of adding 1 month to a date specified by using something like:
Calendar
What you are asking for is some implicit knowledge that if the starting date is the last day of the month, and you add 1 month, the result should be the last day of the following month. I.e. the property "last-day-of-month" should be sticky.
This is not directly available in Java's Calendar
, but one possible solution is to use Calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
to reset the day after incrementing the month.
Calendar cal = ...;
cal.add(Calendar.MONTH,1);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
You could even subclass GregorianCalendar
and add a method
public Calendar endOfNextMonth() { ... }
to encapsulate the operation.