4

I would like to know exactly how many months and days(possibly years) some older date is from today. Is there a method to do that?

I know how to get the difference of the months, I know how to get the difference in days. But I am unable to get the months and the days.

Ex:

old = '2013-03-04' now = '2013-04-17'

so the result im looking for is something like 1 month(s) and 13* day(s)

*maybe its 12 im not every sure.

4

2 回答 2

4

这可以通过在 JodaTime 中使用Period来完成。

例如,

LocalDate old = new LocalDate(2013, 3, 4);
LocalDate now = new LocalDate(2013, 4, 17);

Period p = new Period(old, now, PeriodType.yearMonthDay());

要获取月份,请使用p.getMonths(), 获取天数p.getDays()

该示例的结果是 1 个月 13 天。

于 2013-04-17T18:57:48.153 回答
1

是的,请参阅间隔文档

间隔

Joda-Time 中的时间间隔表示从一个瞬间到另一个瞬间的时间间隔。这两个瞬间都是日期时间连续体中完全指定的瞬间,并带有时区。

间隔被实现为半开,也就是说,开始时刻是包含的,而结束时刻是排除的。结束总是大于或等于开始。两个端点都被限制为具有相同的年表和相同的时区。

提供了两个实现,Interval 和 MutableInterval,它们都是 ReadableInterval 的特化。

于 2013-04-17T18:57:26.330 回答