gpt4 book ai didi

java - Joda Time : Strange behavior for leap year in Period calculation

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:32 26 4
gpt4 key购买 nike

以下 2 个案例(从 2016 年 2 月 28 日/29 日到 2017 年 3 月 1 日)的 Period 结果彼此相同。

你能帮忙解释一下这种奇怪的行为吗?

案例一:2016年2月28日至2017年3月1日

Calendar start1 = Calendar.getInstance();
start1.set(2016, Calendar.FEBRUARY, 28, 0, 0, 0);
Calendar end1 = Calendar.getInstance();
end1.set(2017, Calendar.MARCH, 1, 0, 0, 0);
Interval i1 = new Interval(new DateTime(start1.getTime()), new DateTime(end1.getTime()))
System.out.println(i1.toPeriod());

结果:P1Y1D


案例2:2016年2月29日至2017年3月1日

Calendar start2 = Calendar.getInstance();
start2.set(2016, Calendar.FEBRUARY, 29, 0, 0, 0);
Calendar end2 = Calendar.getInstance();
end2.set(2017, Calendar.MARCH, 1, 0, 0, 0);
Interval i2 = new Interval(new DateTime(start2.getTime()), new DateTime(end2.getTime()))
System.out.println(i2.toPeriod());

结果:P1Y1D

最佳答案

考虑

public static void main(String[] args) throws Exception
{
LocalDate start1 = LocalDate.of(2016, Month.FEBRUARY, 28);
LocalDate start2 = LocalDate.of(2016, Month.FEBRUARY, 29);
LocalDate end = LocalDate.of(2017, Month.MARCH, 1);
Period year = Period.ofYears(1);

System.out.println(start1);
System.out.println(start2);
System.out.println(end);
System.out.println(start1.plus(year));
System.out.println(start2.plus(year));
System.out.println(start1.until(end));
System.out.println(start2.until(end));
}

输出

2016-02-28
2016-02-29
2017-03-01
2017-02-28
2017-02-28
P1Y1D
P1Y1D

这可能不是您想要的工作方式,但看起来实现是一致的。

2 月 29 日加 1 年到底是什么意思?它同样可以作为“二月的最后一天”或开始日期加上 365 天。后一个定义会导致闰年 2 月 29 日之前的几天出现问题:

February 1 2016 + 1 year == January 31 2017
February 1 2017 + 1 year == February 1 2018

这会让人们感到困惑。

这是我们日历工作方式的一个怪癖,现有的行为似乎最小化(但不是消除)行为“令人惊讶”的日期。

关于java - Joda Time : Strange behavior for leap year in Period calculation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39629657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com