Koen Demonie Koen Demonie - 5 months ago 22
Java Question

jodatime interval between 2 datetimes

I would like to find the "interval" between a datetime A, and another latter one.
ex.


  • A) 21/09/2015 12:00:00

  • B) 25/09/2015 12:00:00



interval = 4 days 0h 0m 0s

I found this post:
How to find difference between two Joda-Time DateTimes in minutes

But I am wondering isn't JodaTime's
Interval
supposed to do this ?
If so, how?

Answer

You are not looking for an interval (which has anchors on the timeline) but for a duration which is not bound to a specific time on the timeline. Durations based on any time units are called Period in Joda-Time.

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
Comments