Koen Demonie
Koen Demonie

Reputation: 539

jodatime interval between 2 datetimes

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

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?

Upvotes: 0

Views: 111

Answers (2)

Meno Hochschild
Meno Hochschild

Reputation: 44061

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

Upvotes: 1

Jacob Briscoe
Jacob Briscoe

Reputation: 252

Its possible but with some additional API usage. Take a look at this page it should help you understand Interval and how to use it appropriately.

Joda Interval

Upvotes: 0

Related Questions