在java.time中,添加一个月的结果如何计算? [英] In java.time, how is the result of adding a month calculated?

查看:132
本文介绍了在java.time中,添加一个月的结果如何计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JDK 8中的JSR-310 java.time API中,计算将日期添加到日期的结果的规则是什么?特别是,当您在1月31日之前添加1个月时会发生什么?

In the JSR-310 java.time API in JDK 8, what are the rules for calculating the result of adding a month to a date. In particular, what happens when you add 1 month to a date like January 31st?

LocalDate initial = LocalDate.of(2012, 1, 31);  // 31st January 2012
LocalDate result = initial.plusMonths(1);
// what is the result?


推荐答案

简答:

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

In the example, the result will be the last day of February, 2012-02-29.

说明:

问题是如果你添加一个月你会得到什么日期,那么可以开放解释。为了避免这种情况, java.time API有一个清除规则。结果将与输入的月份相同,除非这是无效的日期,否则结果是该月的最后一天。

The question, "what date do you get if you add a month", is one which could be open to interpretation. To avoid this, the java.time API has a clear rule. The result will have the same day-of-month as the input, unless that would be an invalid date, in which case the result is the last day of the month.

因此,1月31日加上一个月将导致2月31日,但由于这是无效的日期,结果是2月28日或29日的最后一个有效日期,取决于是闰年: / p>

Thus, the 31st January plus one month would result in the 31st February, but since that is an invalid date, the result is the last valid date in February, which is 28th or 29th February depending on whether it is a leap year:

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

同样的规则适用于是增加一个月还是几个月总是基于所产生的月份。即该月首先添加(如果需要调整年份),那么只有当天的考虑。相同的规则也适用于减去。

The same rule applies whether adding one month or many months and is always based on the resulting month. ie. the month is added first (adjusting the year if necessary), and only then is the day-of-week considered. The same rule also applies when subtracting.

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

同样的规则也适用于在/从日期添加/减去年数 - 添加年份后,只有当天 - 月检查有效期。

The same rules also applies when adding/subtracting years to/from a date - the years are added, and only then is the day-of-month checked for validity within the month.

// years use the same rule
2012-02-29 plus 1 year = 2013-02-28  // 29th February invalid so adjusted to 28th

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

If your business logic needs a different rule for month addition, the best approach is to write a TemporalAdjuster or TemporalAmount that packages up your special logic.

这篇关于在java.time中,添加一个月的结果如何计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆