2个日期时间之间的jodatime间隔
jodatime interval between 2 datetimes
我想找到日期时间 A 和另一个日期时间之间的 "interval"。
例如
- A) 21/09/2015 12:00:00
- B) 25/09/2015 12:00:00
间隔 = 4 天 0h 0m 0s
我找到了这个 post:
How to find difference between two Joda-Time DateTimes in minutes
但我想知道 JodaTime 的 Interval
不应该这样做吗?
如果可以,怎么做?
这是可能的,但需要一些额外的 API 用法。看看这个页面,它应该可以帮助您了解 Interval 以及如何正确使用它。
您不是在寻找间隔(在时间轴上有锚点),而是在寻找不受时间轴上特定时间限制的持续时间。基于任何时间单位的持续时间在 Joda-Time 中称为 Period
。
DateTimeFormatter f = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
LocalDateTime ldtA = LocalDateTime.parse("21/09/2015 12:00:00", f);
LocalDateTime ldtB = LocalDateTime.parse("25/09/2015 12:00:00", f);
Period diff = new Period(ldtA, ldtB, PeriodType.dayTime());
System.out.println(PeriodFormat.wordBased(Locale.ENGLISH).print(diff)); // 4 days
我想找到日期时间 A 和另一个日期时间之间的 "interval"。 例如
- A) 21/09/2015 12:00:00
- B) 25/09/2015 12:00:00
间隔 = 4 天 0h 0m 0s
我找到了这个 post: How to find difference between two Joda-Time DateTimes in minutes
但我想知道 JodaTime 的 Interval
不应该这样做吗?
如果可以,怎么做?
这是可能的,但需要一些额外的 API 用法。看看这个页面,它应该可以帮助您了解 Interval 以及如何正确使用它。
您不是在寻找间隔(在时间轴上有锚点),而是在寻找不受时间轴上特定时间限制的持续时间。基于任何时间单位的持续时间在 Joda-Time 中称为 Period
。
DateTimeFormatter f = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
LocalDateTime ldtA = LocalDateTime.parse("21/09/2015 12:00:00", f);
LocalDateTime ldtB = LocalDateTime.parse("25/09/2015 12:00:00", f);
Period diff = new Period(ldtA, ldtB, PeriodType.dayTime());
System.out.println(PeriodFormat.wordBased(Locale.ENGLISH).print(diff)); // 4 days