I am having the following code
Calendar calendar = Calendar.getInstance();
calendar.set(2011, 7, 29); //case 1
// calendar.set(2011, 7, 30); //case 2
// calendar.set(2011, 7, 31); //case 3
System.out.println("===After seting the date== "+calendar.getTime());
System.out.println("=================================================================");
calendar.add(Calendar.MONTH, 6);
System.out.println("===result after adding 6 months== "+calendar.getTime());
and for the case 2 and case 3 also i am getting the same results. it should overflow to next month and show the new date. but its not happening.
It’s not clear whether you’re using "should" to mean "this is what I want it to do" or to mean "this is what I believe it’s documented to do". The documentation actually backs up the behaviour given:
The second part of the example is exactly the situation you’re facing: the DAY_OF_MONTH is expected to be invariant, but has to change in order to stay within the right month, so it’s adjusted to the closest possible value (29 in your case).
So it looks like the behaviour is consistent to me – in what way do you believe it’s inconsistent?