Calendar.roll()
Changes a specific unit and leaves 'larger' (in terms of time-month is 'larger' than day) units unchanged. The API example is that given a date of August 31, 1999, rolling by (Calendar.MONTH, 8) yields April 30, 1999. That is, the DAY was changed to meet April's maximum, but the 'larger' unit, YEAR, was unchanged.
Calendar.add()
Will cause the next 'larger' unit to change, if necessary. That is, given a date of August 31, 1999, add(Calendar.MONTH, 8) yields April 30, 2000. add() also forces a recalculation of milliseconds and all fields.
http://stackoverflow.com/questions/2504141/calendar-add-vs-roll-when-do-we-use-it
Changes a specific unit and leaves 'larger' (in terms of time-month is 'larger' than day) units unchanged. The API example is that given a date of August 31, 1999, rolling by (Calendar.MONTH, 8) yields April 30, 1999. That is, the DAY was changed to meet April's maximum, but the 'larger' unit, YEAR, was unchanged.
Calendar.add()
Will cause the next 'larger' unit to change, if necessary. That is, given a date of August 31, 1999, add(Calendar.MONTH, 8) yields April 30, 2000. add() also forces a recalculation of milliseconds and all fields.
http://stackoverflow.com/questions/2504141/calendar-add-vs-roll-when-do-we-use-it
本文深入解析了Java Calendar API中的roll()和add()方法的区别,通过实例展示了如何在日期操作中灵活使用这些方法,确保时间单位间的正确转换。
2070

被折叠的 条评论
为什么被折叠?



