在java.time中,如何计算添加月份的结果?
在JDK 8中的JSR-310 java.time
API中,有哪些规则可用于计算将日期添加到日期的结果。 特别是,当您将1个月添加到1月31日这样的日期时会发生什么?
LocalDate initial = LocalDate.of(2012, 1, 31); // 31st January 2012 LocalDate result = initial.plusMonths(1); // what is the result?
简短回答:
在该示例中,结果将是2012-02-29
的最后一天。
说明:
问题是“如果你增加一个月,你会得到什么日期”,这个问题可以解释。 为避免这种情况, java.time
API有一个明确的规则 。 结果将与输入具有相同的日期,除非这将是无效日期,在这种情况下,结果是该月的最后一天。
因此,1月31日加1个月将导致2月31日,但由于这是无效日期,结果是2月的最后一个有效日期,即2月28日或29日,具体取决于它是否为闰年:
// normal case 2011-01-15 plus 1 month = 2011-02-15 // 15 Jan -> 15 Feb // special rule choosing the last valid day-of-month 2011-01-31 plus 1 month = 2011-02-28 // 31 Jan -> 28 Feb (2011 is normal year) 2012-01-31 plus 1 month = 2012-02-29 // 31 Jan -> 29 Feb (2012 is leap year) // same rule applies for months other than February 2013-03-31 plus 1 month = 2013-04-30 // 31 Mar -> 30 Apr (only 30 days in April)
无论是添加一个月还是多个月,并且始终基于结果月份,同样的规则也适用。 即。 首先添加月份(必要时调整年份),然后才考虑月份日期。 减去时也适用相同的规则。
// multiple months works on the month of the result 2013-10-31 plus 4 months = 2014-02-28 // last day of February 2013-10-31 minus 4 months = 2013-06-30 // last day of June
在日期中添加/减去年份时也适用相同的规则 – 添加年份,然后仅在月份内检查有效期的日期。
// years use the same rule 2012-02-29 plus 1 year = 2013-02-28 // 29th February invalid so adjusted to 28th
如果您的业务逻辑需要一个不同的月添加规则,最好的方法是编写一个TemporalAdjuster
或TemporalAmount
来打包您的特殊逻辑。