无法使用 Joda 时间将 DateTime 转换为 ReadableInstant
Can't convert DateTime to ReadableInstant using Joda time
我想计算两个日期之间的年数。我写了简单的测试用例来检查。
@Test
public void testGetYears() {
DateTime oldDate = new DateTime(2014,1,1,0,0,0,0);
DateTime newDate = new DateTime(2016,2,1,0,0,0,0);
Days.daysBetween(oldDate, newDate).getDays(); //this line works
Years.yearsBetween(oldDate, newDate).getYears(); //no suitable method found
assertEquals(2, Years.yearsBetween(oldDate, newDate).getYears());
}
错误:
Error:(146, 14) java: no suitable method found for yearsBetween(org.elasticsearch.common.joda.time.DateTime,org.elasticsearch.common.joda.time.DateTime)
method org.joda.time.Years.yearsBetween(org.joda.time.ReadableInstant,org.joda.time.ReadableInstant) is not applicable
(argument mismatch; org.elasticsearch.common.joda.time.DateTime cannot be converted to org.joda.time.ReadableInstant)
method org.joda.time.Years.yearsBetween(org.joda.time.ReadablePartial,org.joda.time.ReadablePartial) is not applicable
(argument mismatch; org.elasticsearch.common.joda.time.DateTime cannot be converted to org.joda.time.ReadablePartial)
我查看了 yearsBetween 定义是否需要与 daysBetween 函数中相同的参数。所以我想知道为什么它在 yearsBetween 中不起作用。
我在这里遗漏了什么吗?
你可以使用 Period:
DateTime oldDate = new DateTime(2014,1,1,0,0,0,0);
DateTime newDate = new DateTime(2016,2,1,0,0,0,0);
Period p = new Period(oldDate, newDate);
int years = p.getYears();
检索两个日期之间的年份。
您尝试使用 elasticsearch (org.elasticsearch.common.joda.time.DateTime
) 提供的 DateTime
和 joda-time 本身 (org.joda.time.Years
) 的 Years
class。
要么到处使用 joda-time classes,要么 elasticsearch 提供 joda-time classes;但不能同时进行!
我想计算两个日期之间的年数。我写了简单的测试用例来检查。
@Test
public void testGetYears() {
DateTime oldDate = new DateTime(2014,1,1,0,0,0,0);
DateTime newDate = new DateTime(2016,2,1,0,0,0,0);
Days.daysBetween(oldDate, newDate).getDays(); //this line works
Years.yearsBetween(oldDate, newDate).getYears(); //no suitable method found
assertEquals(2, Years.yearsBetween(oldDate, newDate).getYears());
}
错误:
Error:(146, 14) java: no suitable method found for yearsBetween(org.elasticsearch.common.joda.time.DateTime,org.elasticsearch.common.joda.time.DateTime)
method org.joda.time.Years.yearsBetween(org.joda.time.ReadableInstant,org.joda.time.ReadableInstant) is not applicable
(argument mismatch; org.elasticsearch.common.joda.time.DateTime cannot be converted to org.joda.time.ReadableInstant)
method org.joda.time.Years.yearsBetween(org.joda.time.ReadablePartial,org.joda.time.ReadablePartial) is not applicable
(argument mismatch; org.elasticsearch.common.joda.time.DateTime cannot be converted to org.joda.time.ReadablePartial)
我查看了 yearsBetween 定义是否需要与 daysBetween 函数中相同的参数。所以我想知道为什么它在 yearsBetween 中不起作用。
我在这里遗漏了什么吗?
你可以使用 Period:
DateTime oldDate = new DateTime(2014,1,1,0,0,0,0);
DateTime newDate = new DateTime(2016,2,1,0,0,0,0);
Period p = new Period(oldDate, newDate);
int years = p.getYears();
检索两个日期之间的年份。
您尝试使用 elasticsearch (org.elasticsearch.common.joda.time.DateTime
) 提供的 DateTime
和 joda-time 本身 (org.joda.time.Years
) 的 Years
class。
要么到处使用 joda-time classes,要么 elasticsearch 提供 joda-time classes;但不能同时进行!