0

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?

Community
  • 1
  • 1
Koen Demonie
  • 539
  • 1
  • 7
  • 24

2 Answers2

1

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
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • What is the diffrence between `LocalDateTime` and plain `DateTime` ? -Why did you use localdt ? – Koen Demonie Oct 02 '15 at 14:20
  • 1
    @KoenDemonie `DateTime` is not plain and contains MORE state than `LocalDateTime` despite of its shorter name. `DateTime` is also related to a timezone while `LocalDateTime` is not. And that is the reason why I have chosen a type without timezone: Your strings don't have any offset or timezone references. – Meno Hochschild Oct 02 '15 at 14:27
0

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

Jacob Briscoe
  • 252
  • 1
  • 8