2

大约在 2 月份,您可以在 Joda-Time 的错误结果中看到一个奇怪的行为,您可以往下看:

DateTime date1 = new DateTime(2013, 4, 28, 0, 0, 0, 0);
DateTime date1PlusNineMonths = new DateTime(2014, 1, 28, 0, 0, 0, 0);
Period period1 = new Period(date1, date1PlusNineMonths);
//Correct result
System.out.print(date1.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date1.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period1.getYears() + "Years/" + period1.getMonths() + "Months/" + period1.getWeeks() + "Weeks/" + period1.getDays() + "Days");

DateTime date2 = new DateTime(2013, 4, 29, 0, 0, 0, 0);
DateTime date2PlusNineMonths = new DateTime(2014, 1, 28, 0, 0, 0, 0);
Period period2 = new Period(date2, date2PlusNineMonths);
//Correct result
System.out.print(date2.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date2.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period2.getYears() + "Years/" + period2.getMonths() + "Months/" + period2.getWeeks() + "Weeks/" + period2.getDays() + "Days");

DateTime date3 = new DateTime(2013, 5, 28, 0, 0, 0, 0);
DateTime date3PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period3 = new Period(date3, date3PlusNineMonths);
//Correct result
System.out.print(date3.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date3.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period3.getYears() + "Years/" + period3.getMonths() + "Months/" + period3.getWeeks() + "Weeks/" + period3.getDays() + "Days");

DateTime date4 = new DateTime(2013, 5, 29, 0, 0, 0, 0);
DateTime date4PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period4 = new Period(date4, date4PlusNineMonths);
//Incorrect result
System.out.print(date4.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date4.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period4.getYears() + "Years/" + period4.getMonths() + "Months/" + period4.getWeeks() + "Weeks/" + period4.getDays() + "Days");

DateTime date5 = new DateTime(2013, 5, 30, 0, 0, 0, 0);
DateTime date5PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period5 = new Period(date5, date5PlusNineMonths);
//Incorrect result
System.out.print(date5.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date5.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period5.getYears() + "Years/" + period5.getMonths() + "Months/" + period5.getWeeks() + "Weeks/" + period5.getDays() + "Days");

DateTime date6 = new DateTime(2013, 5, 31, 0, 0, 0, 0);
DateTime date6PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period6 = new Period(date6, date6PlusNineMonths);
//Incorrect result
System.out.print(date6.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date6.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period6.getYears() + "Years/" + period6.getMonths() + "Months/" + period6.getWeeks() + "Weeks/" + period6.getDays() + "Days");

DateTime date7 = new DateTime(2013, 6, 1, 0, 0, 0, 0);
DateTime date7PlusNineMonths = new DateTime(2014, 3, 1, 0, 0, 0, 0);
//Correct result
Period period7 = new Period(date7, date7PlusNineMonths);
System.out.print(date7.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date7.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period7.getYears() + "Years/" + period7.getMonths() + "Months/" + period7.getWeeks() + "Weeks/" + period7.getDays() + "Days");

通常在测试中(周期4、周期5、周期6)我应该有天变化而不是每次都显示9个月和0天,这是否正常,也许我在使用joda框架时出错了,当我做一个时输出变得正确.addMonth(9)01/06/2013

输出:

28/04/2013.plusMonths(9) = 28/01/2014 //correct result

0Years/9Months/0Weeks/0Days //correct calculation

29/04/2013.plusMonths(9) = 29/01/2014 //correct result

0Years/8Months/4Weeks/2Days //correct calculation

28/05/2013.plusMonths(9) = 28/02/2014 //correct result

0Years/9Months/0Weeks/0Days //correct calculation

29/05/2013.plusMonths(9) = 28/02/2014 //incorrect result should be 29/02/2014

0Years/9Months/0Weeks/0Days //correct calculation

30/05/2013.plusMonths(9) = 28/02/2014 //incorrect result should be 01/03/2014

0Years/9Months/0Weeks/0Days //incorrect calculation, should be 0Years/8Months/4Weeks/2Days

31/05/2013.plusMonths(9) = 28/02/2014 //incorrect result should be 01/03/2014

0Years/9Months/0Weeks/0Days //incorrect calculation, should be 0Years/8Months/4Weeks/1Days

01/06/2013.plusMonths(9) = 01/03/2014 //correct result

0Years/9Months/0Weeks/0Days //correct calculation
4

3 回答 3

2

为什么你认为这个结果是不正确的?
不是天数。是抽象单位

所以月份的最后一天加上月份是下个月的最后一天,而不是下个月之后的第一天

为什么你这么想

30/05/2013.plusMonths(9) = 28/02/2014 //不正确的结果应该是 01/03/2014

为什么不02/03/2014呢?

你的假设是错误的。Joda time 在您的示例中效果很好

于 2013-06-12T18:27:44.617 回答
0

我犯了一个错误,因为我累了大声笑我不知道为什么我确定 2014 年是闰年,但无论如何我实际上更专注于计算月份和天数如果你在这里看一下:

        DateTime date4 = new DateTime(2013, 5, 29, 0, 0, 0, 0);
        DateTime date4PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
        Period period4 = new Period(date4, date4PlusNineMonths);
        //Incorrect result
        System.out.print(date4.toString("dd/MM/YYYY")+".plusMonths(9) = "+ date4.plusMonths(9).toString("dd/MM/YYYY"));
        System.out.print(period4.getYears() +"Years/"+ period4.getMonths() +"Months/"+ period4.getWeeks() +"Weeks/"+ period4.getDays() +"Days");

输出 :

0Years/9Months/0Weeks/0Days

这是一个不正确的计算应该是 0Years/8Months/4Weeks/2Days 不是吗...?

如果它是正确的,那么为什么当我增加两天(30 和 31)时它总是给我相同的计算(0Years/9Months/0Weeks/0Days)我们都同意天在减少或者我在做梦......?

现在也许我在使用 joda 框架时出错了,因为在我开发的应用程序中,我每天都会检查一段时间,以便在两个日期(今天和下一个日期)之间有精确的月份间隔,如果我有 9 个月和 1 天,那也是迟到了,我的应用程序需要 9 个月和 0 天 =/

如果我错了,我怎么能做到这一点......?

于 2013-06-14T07:55:43.397 回答
0

抱歉,刚才绝对没有适合您的解决方案。我想 joda 建筑师 S. Colebourne 由于向后兼容性问题,甚至必须坚持他的周期计算方式。无论如何,他已将joda-time/github 上的相应问题关闭为 not-a-bug/won't-fix。如果你点击那里给出的链接,他在更新 joda 的 javadoc 中说他的周期计算方式是正确的,因为

[2013-05-29] + P9M = [2014-02-28]

在我看来,这个论点是相当薄弱的,因为它也是真的

[2013-05-29] + P8M30D = [2014-02-28]

因此,仅基于加法运算没有明确指示哪个周期是正确的。为了明确指定期间计算的行为,如果有类似以下规范的内容,这将是一个更好的解决方案:如果结束日期的月份日期小于开始日期的月份日期,则差异不应计为完整月。简单易懂。我也不相信周应该是周期计算的主要部分,因为 LocalDate 的状态仅基于年、月和日。因此,与其说 P8M4W2D(与 ISO-8601 不兼容!),不如使用 P8M30D 解决方案。

于 2013-06-16T20:15:11.550 回答