知识卡片
加月应先求目标再裁剪日期
把年月映为连续月序号,加偏移再分解成年月,可让跨年过程透明;最后以原日和目标月末的较小值作为结果日。加年也需在闰日等位置应用同类约束。该例的整数运算和工厂都有支持范围,不能直接推广到负序号、任意大偏移或溢出情况;裁剪也意味着分步加月未必等于一次合并偏移。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 6 处
- 第16章 重构SerialDate 16.2 让它做对(续) · 查看原书依据
int resultMonthAsOrdinal = thisMonthAsOrdinal + months;
- 附录B org.jfree.date.SerialDate 附录B org.jfree.date.SerialDate(续) · 查看原书依据
base.getDayOfMonth(), SerialDate.lastDayOfMonth(mm, yy)
- 附录B org.jfree.date.SerialDate 附录B org.jfree.date.SerialDate(续) · 查看原书依据
SerialDate.addMonths(2, this.nov9Y2001)
- 附录B org.jfree.date.SerialDate 附录B org.jfree.date.SerialDate(续) · 查看原书依据
test1096282()
- 附录B org.jfree.date.SerialDate 附录B org.jfree.date.SerialDate(续) · 查看原书依据
addMonths(1, d(31, JANUARY, 1900))
- 附录B org.jfree.date.SerialDate 附录B org.jfree.date.SerialDate(续) · 查看原书依据
int resultMonthAndYearAsOrdinal = thisMonthAndYearAsOrdinal + months;
参考来源
- 位置:《代码整洁之道》全书 · 第16章 重构SerialDate,spine025单元005;来源项目book-sources,extracted/代码整洁之道/_epub-src/OEBPS/Text/part0023.xhtml。
- 结论依据:原文完整addMonths/plusYears及min裁剪说明算法,联系第9章SerialDate测试的非合成性,不声称运行结果或无限范围。未在本机运行示例或改变环境。
- 原始内容:tMonth().index - 1; int resultMonthAsOrdinal = thisMonthAsOrdinal + months; int resultYear = resultMonthAsOrdinal / 12; Month resultMonth = Month.make(resultMon