java - Joda-Time 在周期计算中的奇怪行为

标签 java jodatime period date-arithmetic

关于二月,你可以在 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");

通常在测试中(period4、period5、period6)我应该有天数变化而不是每次都显示 9month 和 0days,这是正常的吗?也许我在使用 joda 框架时是错误的,并且输出在以下情况下变得正确我在 01/06/2013 上执行了 .addMonth(9)

输出:

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

最佳答案

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

因此,本月的最后一天加上月份是下个月的最后一天,而不是下个月后一个月的第一天

你为什么这么认为

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

为什么不02/03/2014

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

关于java - Joda-Time 在周期计算中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17045217/

相关文章:

scala - joda-time 导入在 Spark shell 中停止工作

ruby-on-rails - ruby on rails - routes.rb - 当文件名中存在多个句点时匹配文件扩展名

python - 具有自定义持续时间的 Pandas Period

c# - 与 System.Windows.Forms.Timer 的间隔不一致

java - 处理 3 作为图像处理库

Java-单例

java - 找出 java.util.Date 和 Joda-Time DateTime 之间的天数差异?

parsing - 使用 clj-time 将 Clojure #inst 即时时间转换为 Joda 时间

java - <s :fileUpload> is returning null value

java - 使用 JProfiler 进行分析时,方法名称会触发基元的自动装箱