java Joda Time - 两个日期之间的月差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34773160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:19:55  来源:igfitidea点击:

Joda Time - difference in months between two dates

javajodatime

提问by Carlos Lopez

I need to get the difference in months between two dates, I'm using Joda Time, the problem is this:

我需要得到两个日期之间的月差,我使用的是 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 

it returns 0 because there is no month in the middle of the two dates, I need to return the difference in months not a few months in between, and add 1 would be problematic when the dates are the same.

它返回 0 因为两个日期中间没有月份,我需要返回月份的差异而不是中间的几个月,如果日期相同,加 1 会出现问题。

回答by raulk

Changing the first date to 2015-02-02, Joda correctly returns 1 month:

将第一个日期更改为 2015-02-02,Joda 正确返回 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.

So my guess is that because you didn't provide a time portion, Joda cannot be precise about exactly which point in time of 2015-01-01date2refers to. You might have as well referred to 23:59:59, in which case a full month wouldn't have elapsed yet, technically.

所以我的猜测是,因为您没有提供时间部分,Joda 无法准确确定2015-01-01date2指的是哪个时间点您可能还提到了 23:59:59,在这种情况下,从技术上讲,整整一个月还没有过去。

If you provide a zero time portion explicitly, it works as you initially expected:

如果您明确提供零时间部分,它将按您最初的预期工作:

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.

Therefore, I recommend you specify a 00:00:00 time portion in each date explicitly.

因此,我建议你指定每个日期00:00:00时间部分明确

回答by Ruslan Stelmachenko

While other answers are correct they still mask the real problem.

虽然其他答案是正确的,但它们仍然掩盖了真正的问题。

it returns 0 because there is no month in the middle of the two dates

它返回 0 因为两个日期中间没有月份

No. It returns 0 because there is time part of DateTimeobject. You creating two istances of DateTimefilled 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 LocalDateinstead.

否。它返回 0,因为DateTime对象有时间部分。您创建两个DateTime填充当前时刻(小时、分钟、秒和毫秒)的实例,然后只修改日期部分。如果您只想比较两个日期,则没有理由这样做。使用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

Also need to pay attention to the fact that despite the fact that Monthsdocs say nothing about it, Monthcan contain negative value if first date is after second date. So we need to use Math.absto really count the number of months between two dates.

还需要注意的事实是,尽管Months文档对此一无所知,但Month如果第一个日期在第二个日期之后,则可能包含负值。所以我们需要使用Math.abs来真正计算两个日期之间的月数。

The docssay:

文件说:

Creates a Months representing the number of whole months between the two specified partial datetimes.

创建一个 Months,表示两个指定的部分日期时间之间的整月数。

But it isn't true. It really calculates the differencein months. Not the number of months.

但事实并非如此。它确实计算了几个月的差异。不是月数

回答by CJBS

The way this is calculated depends on the business logic that is to be used. Each month varies in length. One option would be to, in the monthsBetween()function, get the start of the month for both date1and date2, and compare that.

计算方式取决于要使用的业务逻辑。每个月长短不一。一个选择是,在monthsBetween()功能,得到了一个月的开始两个date1date2,和比较。

Something like:

就像是:

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)