乔达时间 - 两个日期之间的月差
Joda Time - difference in months between two dates
我需要得到两个日期之间的月数差异,我使用的是 Joda Time,问题是这样的:
DateTime date1 = new DateTime().withDate(2015, 2, 1);
DateTime date2 = new DateTime().withDate(2015, 1, 1);
Months m = Months.monthsBetween(date1, date2);
int monthDif = m.getMonths();//this return 0
它returns 0因为两个日期中间没有月份,我需要return相差月份而不是中间的几个月,加1就是当日期相同时会出现问题。
计算方式取决于要使用的业务逻辑。每个月长短不一。一种选择是,在 monthsBetween()
函数中,获取 date1
和 date2
的月初,然后进行比较。
类似于:
DateTime firstOfMonthDate1 = new DateTime(date1.getYear(), date1.getMonthOfYear(), 1, 0, 0);
DateTime firstOfMonthDate2 = new DateTime(date2.getYear(), date2.getMonthOfYear(), 1, 0, 0);
Months m = Months.monthsBetween(firstOfMonthDate1, firstOfMonthDate2)
将第一个日期更改为 2015-02-02,Joda 正确 returns 1 个月:
DateTime date1 = new DateTime().withDate(2015, 2, 2);
DateTime date2 = new DateTime().withDate(2015, 1, 1);
System.out.println(Months.monthsBetween(date2, date1).getMonths());
// Returns 1.
所以我的猜测是,因为你没有提供时间部分,Joda 不能准确2015 年 1 月 1 日的哪个时间点date2
指。 您可能还提到了 23:59:59,在这种情况下,从技术上讲,整整一个月还没有过去。
如果您明确提供零时间部分,它会按您最初的预期工作:
DateTime date1 = new DateTime().withDate(2015, 2, 1).withTime(0, 0, 0, 0);
DateTime date2 = new DateTime().withDate(2015, 1, 1).withTime(0, 0, 0, 0);
System.out.println(Months.monthsBetween(date2, date1).getMonths());
// Returns 1.
因此,我建议您在每个日期明确指定一个00:00:00时间部分。
虽然其他答案是正确的,但它们仍然掩盖了真正的问题。
it returns 0 because there is no month in the middle of the two dates
没有。它 returns 0 因为有 DateTime object. You creating two istances of DateTime
filled with current moment in time (with hours, minutes, seconds and milliseconds) and then modify just date part. There is no reasons to do it if you want to compare just two dates. use LocalDate 的时间部分。
LocalDate date1 = new LocalDate(2015, 2, 1);
LocalDate date2 = new LocalDate(2015, 1, 1);
Months m = Months.monthsBetween(date1, date2);
int monthDif = Math.abs(m.getMonths());//this return 1
还需要注意的是,尽管 Months 文档对此只字未提,但如果第一个日期在第二个日期之后,Month
可以包含负值。所以我们需要使用Math.abs
来真正计算两个日期之间的月数。
docs 说:
Creates a Months representing the number of whole months between the two specified partial datetimes.
但事实并非如此。它确实以月为单位计算 difference。不是 月数。
我需要得到两个日期之间的月数差异,我使用的是 Joda Time,问题是这样的:
DateTime date1 = new DateTime().withDate(2015, 2, 1);
DateTime date2 = new DateTime().withDate(2015, 1, 1);
Months m = Months.monthsBetween(date1, date2);
int monthDif = m.getMonths();//this return 0
它returns 0因为两个日期中间没有月份,我需要return相差月份而不是中间的几个月,加1就是当日期相同时会出现问题。
计算方式取决于要使用的业务逻辑。每个月长短不一。一种选择是,在 monthsBetween()
函数中,获取 date1
和 date2
的月初,然后进行比较。
类似于:
DateTime firstOfMonthDate1 = new DateTime(date1.getYear(), date1.getMonthOfYear(), 1, 0, 0);
DateTime firstOfMonthDate2 = new DateTime(date2.getYear(), date2.getMonthOfYear(), 1, 0, 0);
Months m = Months.monthsBetween(firstOfMonthDate1, firstOfMonthDate2)
将第一个日期更改为 2015-02-02,Joda 正确 returns 1 个月:
DateTime date1 = new DateTime().withDate(2015, 2, 2);
DateTime date2 = new DateTime().withDate(2015, 1, 1);
System.out.println(Months.monthsBetween(date2, date1).getMonths());
// Returns 1.
所以我的猜测是,因为你没有提供时间部分,Joda 不能准确2015 年 1 月 1 日的哪个时间点date2
指。 您可能还提到了 23:59:59,在这种情况下,从技术上讲,整整一个月还没有过去。
如果您明确提供零时间部分,它会按您最初的预期工作:
DateTime date1 = new DateTime().withDate(2015, 2, 1).withTime(0, 0, 0, 0);
DateTime date2 = new DateTime().withDate(2015, 1, 1).withTime(0, 0, 0, 0);
System.out.println(Months.monthsBetween(date2, date1).getMonths());
// Returns 1.
因此,我建议您在每个日期明确指定一个00:00:00时间部分。
虽然其他答案是正确的,但它们仍然掩盖了真正的问题。
it returns 0 because there is no month in the middle of the two dates
没有。它 returns 0 因为有 DateTime object. You creating two istances of DateTime
filled with current moment in time (with hours, minutes, seconds and milliseconds) and then modify just date part. There is no reasons to do it if you want to compare just two dates. use LocalDate 的时间部分。
LocalDate date1 = new LocalDate(2015, 2, 1);
LocalDate date2 = new LocalDate(2015, 1, 1);
Months m = Months.monthsBetween(date1, date2);
int monthDif = Math.abs(m.getMonths());//this return 1
还需要注意的是,尽管 Months 文档对此只字未提,但如果第一个日期在第二个日期之后,Month
可以包含负值。所以我们需要使用Math.abs
来真正计算两个日期之间的月数。
docs 说:
Creates a Months representing the number of whole months between the two specified partial datetimes.
但事实并非如此。它确实以月为单位计算 difference。不是 月数。