【问题标题】:In java.time, how is the result of adding a month calculated?java.time中,加一个月的结果是怎么计算的?
【发布时间】:2014-03-09 00:30:54
【问题描述】:

在 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?

【问题讨论】:

    标签: java datetime java-8 java-time


    【解决方案1】:

    简答:

    在示例中,结果将是二月的最后一天,2012-02-29

    解释:

    “如果加上一个月,你会得到什么日期”这个问题可以解释。为避免这种情况,java.time API 有一个clear rule。结果将与输入具有相同的月份日期,除非该日期无效,在这种情况下,结果是该月的最后一天。

    因此,1 月 31 日加一个月将导致 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)
    

    相同的规则适用于添加一个月或多个月份,并且始终基于生成的月份。 IE。首先添加月份(必要时调整年份),然后才考虑月份中的日期。同样的规则也适用于减法。

    // 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
    

    如果您的业务逻辑需要不同的月份添加规则,最好的方法是编写一个 TemporalAdjusterTemporalAmount 来打包您的特殊逻辑。

    【讨论】:

      猜你喜欢
      • 2017-04-11
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多