我用过Joda-Time joda-time-2.3.jar 用于 android 应用程序的日计算。我的代码是……
Period Nextperiod = new Period(ddate, nextdt,PeriodType.yearMonthDay());
在这个ddate和nextdt是DateTime 这两个日期都是输入类型,格式为 SimpleDateFormat ,对于日差,我使用了 Nextperiod.getDays() 现在测试用例是,
In case 2 it should be 14 and When I insert 29 may or 30 may the days are 12 same result. I don't know whats wrong with this date. I tested some more date and result are as per my expectation.Let me know my mistake.Case 1 Right ddate=2014-06-01T00:00:00.000+05:30 nextdt =2015-04-11T00:00:00.000+05:30 Day: 10 Month: 10 year: 0<p></p>
Case 2 Wrong ddate= 2014-05-28T00:00:00.000+05:30 nextdt=2015-03-12T00:00:00.000+05:30 Day: 12 Month: 9 year: 0
Also tried,
Period Nextperiod = new Period(new LocalDate(Ddate), new LocalDate(Ddate),PeriodType.yearMonthDay());
提前致谢。
最佳答案
如果您尝试逐步将 9 个月零 12 天添加到您的开始日期 (ddate),您就会明白为什么案例 2 示例是正确的。
2014-05-28 加上 9 个月的结果是 2015-02-28。这是 2015 年 2 月的最后一天,因此加上 12 天得到 2015-03-12。
这也适用于开始日期为 5 月 29 日或 5 月 30 日的测试用例:这些日期与结束日期之间的时间间隔为 9 个月零 12 天。
关于java - 某些情况下的 Joda-Time Period 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24076600/