Joda:如何在两个日期之间获得数月和数日

时间:2022-08-03 21:31:17

I think I'm missing some subtlety here because everything I'm reading says this should work. I need to get the months and days between two dates in Android. That is to say, how many whole months plus any additional days. I need this as numeric output, not a printed string. Here's what I'm doing, using Joda:

我想我在这里遗漏了一些微妙之处,因为我正在读的一切都说这应该有效。我需要在Android中获得两个日期之间的月份和日期。也就是说,整整几个月加上任何额外的天数。我需要这个作为数字输出,而不是打印的字符串。这是我正在做的事情,使用Joda:

void method(DateTime begin, DateTime end) {
    Period period = Period period = new Period(begin, end);
    final int months = period.getMonths();
    final int additionalDays = period.getDays();

The problem is additionalDays is always zero. For example, July 1 2015 to November 29th 2015 should result in 4 months and 29 days, but I get 4 months and 0 days.

问题是additionalDays总是为零。例如,2015年7月1日到2015年11月29日应该会产生4个月和29天,但我会得到4个月零0天。

2 个解决方案

#1


3  

From the documentation Period.getDays() will return 0 if it is unsupported. I am not sure why that would be unsupported, but I'd like to offer a different approach: use the Days and Months classes and their monthsBetween() and daysBetween() methods. Note that you'll have to subtract the months between, too:

从文档中,如果不支持,则Period.getDays()将返回0。我不确定为什么不支持,但我想提供一种不同的方法:使用Days和Months类及其monthsBetween()和daysBetween()方法。请注意,您还必须减去两者之间的月份:

// Get months
int months = Months.monthsBetween(begin, end).getMonths();
// Subtract this number of months from the end date so we can calculate days
DateTime newEnd = end.minusMonths(months);
// Get days
int days = Days.daysBetween(begin, newEnd).getDays();

If you don't preform this subtraction in the middle, you'll get incorrect results as you'll get all days.

如果你没有在中间预先形成这个减法,你会得到不正确的结果,因为你会得到所有的日子。

#2


2  

The javadoc for the Period constructor you used

您使用的Period构造函数的javadoc

new Period(begin, end);

states

状态

Creates a period from the given interval endpoints using the standard set of fields.

使用标准字段集从给定的间隔端点创建一个句点。

In other words, it is equivalent to

换句话说,它相当于

Period period = new Period(startTime, endTime, PeriodType.standard());

The PeriodType#standard() method returns a PeriodType that supports the weeks field.

PeriodType#standard()方法返回支持周字段的PeriodType。

Your period, July 1 2015 to November 29th 2015, is actually 4 months and 28 days, where the 28 days get transformed into 4 weeks. So your Period object is actually of 4 months and 4 weeks.

您的期间,2015年7月1日至2015年11月29日,实际上是4个月和28天,其中28天变为4周。所以你的Period对象实际上是4个月和4个星期。

If you had tried to create a Period from July 1 2015 to November 30th 2015, you'd have 4 months, 4 weeks, and 1 day.

如果您曾尝试创建2015年7月1日至2015年11月30日期间,您将有4个月,4周和1天。

Instead, create a Period with a PeriodType#yearMonthDay() that only supports years, months, and days fields.

相反,使用PeriodType#yearMonthDay()创建一个仅支持年,月和日字段的Period。

period = new Period(begin, end, PeriodType.yearMonthDay());

then you'd have a Period with 4 months, and 28 days, as it doesn't support weeks.

然后你有一个4个月和28天的期间,因为它不支持几个星期。

#1


3  

From the documentation Period.getDays() will return 0 if it is unsupported. I am not sure why that would be unsupported, but I'd like to offer a different approach: use the Days and Months classes and their monthsBetween() and daysBetween() methods. Note that you'll have to subtract the months between, too:

从文档中,如果不支持,则Period.getDays()将返回0。我不确定为什么不支持,但我想提供一种不同的方法:使用Days和Months类及其monthsBetween()和daysBetween()方法。请注意,您还必须减去两者之间的月份:

// Get months
int months = Months.monthsBetween(begin, end).getMonths();
// Subtract this number of months from the end date so we can calculate days
DateTime newEnd = end.minusMonths(months);
// Get days
int days = Days.daysBetween(begin, newEnd).getDays();

If you don't preform this subtraction in the middle, you'll get incorrect results as you'll get all days.

如果你没有在中间预先形成这个减法,你会得到不正确的结果,因为你会得到所有的日子。

#2


2  

The javadoc for the Period constructor you used

您使用的Period构造函数的javadoc

new Period(begin, end);

states

状态

Creates a period from the given interval endpoints using the standard set of fields.

使用标准字段集从给定的间隔端点创建一个句点。

In other words, it is equivalent to

换句话说,它相当于

Period period = new Period(startTime, endTime, PeriodType.standard());

The PeriodType#standard() method returns a PeriodType that supports the weeks field.

PeriodType#standard()方法返回支持周字段的PeriodType。

Your period, July 1 2015 to November 29th 2015, is actually 4 months and 28 days, where the 28 days get transformed into 4 weeks. So your Period object is actually of 4 months and 4 weeks.

您的期间,2015年7月1日至2015年11月29日,实际上是4个月和28天,其中28天变为4周。所以你的Period对象实际上是4个月和4个星期。

If you had tried to create a Period from July 1 2015 to November 30th 2015, you'd have 4 months, 4 weeks, and 1 day.

如果您曾尝试创建2015年7月1日至2015年11月30日期间,您将有4个月,4周和1天。

Instead, create a Period with a PeriodType#yearMonthDay() that only supports years, months, and days fields.

相反,使用PeriodType#yearMonthDay()创建一个仅支持年,月和日字段的Period。

period = new Period(begin, end, PeriodType.yearMonthDay());

then you'd have a Period with 4 months, and 28 days, as it doesn't support weeks.

然后你有一个4个月和28天的期间,因为它不支持几个星期。