80,360
社区成员
发帖
与我相关
我的任务
分享
/**
* After adjustments such as add(MONTH), add(YEAR), we don't want the
* month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar
* 3, we want it to go to Feb 28. Adjustments which might run into this
* problem call this method to retain the proper month.
*/
private final void pinDayOfMonth() {
int year = internalGet(YEAR);
int monthLen;
if (year > gregorianCutoverYear || year < gregorianCutoverYearJulian) {
monthLen = monthLength(internalGet(MONTH));
} else {
GregorianCalendar gc = getNormalizedCalendar();
monthLen = gc.getActualMaximum(DAY_OF_MONTH);
}
int dom = internalGet(DAY_OF_MONTH);
if (dom > monthLen) {
set(DAY_OF_MONTH, monthLen);
}
}
这是GregorianCalendar源码里的描述,当1月31,加一个月时,是希望跳到的就是2月28。其次,代码逻辑里面有这样的判断,monthLen 是一个月的最大天数,但 dom > monthLen 时,当月的天数就被设为最大天数了,避免出现2月31这样的情况。
这样理解了吧