How Calendar class’s add and roll methods affect different date parts
In Java, Calendar class’s add() method, with a positive argument, moves the specified date part to future, may move the "bigger" date parts to future, and may move the "smaller" date parts to past.
For example, considering that 2016 is a leap year:
Calendar c = Calendar.getInstance(); c.set(2015, 11, 31, 0, 0, 0); System.out.println(c.getTime()); //Thu Dec 31 00:00:00 CST 2015 c.add(Calendar.MONTH, 2); System.out.println(c.getTime()); //Mon Feb 29 00:00:00 CST 2016
Calendar class’s roll() method, with a positive argument, moves the specified date part to future, does not change the "bigger" date parts, and may move the "smaller" date parts to past.
For example:
Calendar c = Calendar.getInstance(); c.set(2015, 11, 31, 0, 0, 0); System.out.println(c.getTime()); //Thu Dec 31 00:00:00 CST 2015 c.roll(Calendar.MONTH, 2); System.out.println(c.getTime()); //Sat Feb 28 00:00:00 CST 2015












