例子说明:
假设:f= 2001-1-30
f.add(Calendar.Month, 13) = 2002.2.28
f.set(Calendar.Month,2) = 2002.3.2
f.roll(Calendar.Month, 13) = 2001.2.28
Add:修改后如果符合实际,会调整,但不会改变调整的值,如例子中的MONTH。
Set: 会改变如把2月改为3月。
roll:于Add类似,不同在于不会改变最大的日期单位,如还是2001 不会为2002。
下面为代码演示:
set
1 Calendar calendar = Calendar.getInstance();
2 calendar.set(2001, Calendar.JANUARY, 29);
3 calendar.set(Calendar.MONTH, 13);
4 System.out.println(calendar.getTime());
5 结果是 Fri Mar 01 17:25:53 CST 2002
add
1 Calendar calendar = Calendar.getInstance();
2 calendar.set(2001, Calendar.JANUARY, 29);
3 calendar.add(Calendar.MONTH, 13);
4 System.out.println(calendar.getTime());
结果是Thu Feb 28 17:28:07 CST 2002
roll
1 Calendar calendar = Calendar.getInstance();
2 calendar.set(2001, Calendar.JANUARY, 29);
3 calendar.roll(Calendar.MONTH, 13);
4 System.out.println(calendar.getTime());
结果是Wed Feb 28 17:29:12 CST 2001