2

There are a plethora of SO questions that deal with calculating the difference between two dates or two times in Java. Many answers point out the problems with the core Java Date classes, and suggest using Jodatime, but AFAIK, there are none that suggest how to do this using jsr310.

So, are there any methods to calculate the difference between two dates or two times in jsr310?


For reference, the following questions handle this issue:

In core Java:

In Jodatime:

Community
  • 1
  • 1
amaidment
  • 6,942
  • 5
  • 52
  • 88

2 Answers2

6

Answering wrt to the final JDK1.8 version.

There are two amount classes - Duration and Period. Duration is time-based (nanos) and Period is date-based (year/month/day).

The amount of time between two temporal objects of the same type can be easily calculated in one of two ways (I prefer the first):

long days = ChronoUnit.DAYS.between(start, end);
long days = start.until(end, ChronoUnit.DAYS);

The Duration between two temporal objects of the same type can be calculated as follows:

Duration d = Duration.between(start, end);

The Period between two LocalDate objects can be calculated as follows:

Period p = Period.between(start, end);
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
  • Thanks - can I ask you to clarify on `Period` - when you say that `Period` is 'date-based', does this mean that it will no longer have the concept of hours, minutes, etc.? The Javadoc for 1.8 b75 still shows static constructors that take h,m,s etc. Also, does the `between(TemporalAccessor, TemporalAccessor)` method take into account the time differences as well as the date differences? (To clarify, I'm still using 0.6.3) – amaidment Apr 19 '13 at 15:02
  • There was lots of change in Period/Duration. Period is now only year/month/day. All the between methods are either based on local time or instants and work accordingly. – JodaStephen Apr 20 '13 at 08:16
0

How to do this depends on what date or datetime classes are being used.

If you are operating in real time (i.e. with time zones etc. so the datetime can be pinpointed to an exact Instant on the time-line), you can use the Duration class (javadoc) - e.g.

Duration diff = Duration.between(InstantProvider startInc, InstantProvider endInc);

where an InstantProvider is able

Alternatively, if you are operating in hypothetical time (i.e. without time zones etc.), you can use the Period class (javadoc) to get the difference between two instances of LocalDate - e.g.

// diff in days, months and years:
Period diff = Period.between(LocalDate start, LocalDate end);

// diff in days:
Period diff = Period.daysBetween(LocalDate start, LocalDate end);

However, I'm not aware of any convenient methods for getting the difference between two LocalDateTimes or LocalTimes. But... this could be done using Period as follows:

// diff between LocalDateTimes:
public static Period between(LocalDateTime start, LocalDateTime end){
  return Period.of(
    end.getYear()-start.getYear(), 
    end.getMonthOfYear().getValue()-start.getMonthOfYear().getValue(), 
    end.getDayOfMonth()-start.getDayOfMonth(),
    end.getHourOfDay()-start.getHourOfDay(),
    end.getMinuteOfHour()-start.getMinuteOfHour(),
    end.getSecondOfMinute()-start.getSecondOfMinute(),
    end.getNanoOfSecond()-start.getNanoOfSecond()
  );
}

// diff between LocalTimes:
public static Period between(LocalTime start, LocalTime end){
  return Period.of(0, 0, 0,
    end.getHourOfDay()-start.getHourOfDay(),
    end.getMinuteOfHour()-start.getMinuteOfHour(),
    end.getSecondOfMinute()-start.getSecondOfMinute(),
    end.getNanoOfSecond()-start.getNanoOfSecond()
  );
}
amaidment
  • 6,942
  • 5
  • 52
  • 88
  • Just to hopefully help someone else that was similarly confused while using Joda time with `LocalDateTime` objects: there is a constructor similar to the `Period.of` method used, except that it has a `weeks` parameter (third, just after the months) and the `PeriodType` should be set to `PeriodType.yearMonthDayTime()`. Unfortunately, it became ineffective when trying to create a `Duration` because months have no standard representation due to a month difference being anything from 28 to 31 days. If that's _your_ goal, then you should just convert to a `Date`, then subtract `getTime()`s. – pickypg Aug 26 '13 at 05:40
  • Continuing my comment about Joda time: `weeks` should be set to `0` in the above calculation, unless you happen to know it. amaidment's approach works, and my comment is only that that was a limitation of what I wanted to do and I wanted to provide the answer for those using Joda now rather than able to wait for JDK8. – pickypg Aug 26 '13 at 05:41
  • 1
    @pickypg - just to be clear, the question/answer was relating to the use of jsr310 (which should end up in Java 8), and not related to the use of jodatime. – amaidment Aug 26 '13 at 11:54
  • I know, but this comes up because of the class names in search results :). That's why I didn't add an extra answer – pickypg Aug 26 '13 at 18:33
  • @pickypg - okay, but the question title states that it relates to 'jsr310', it's tagged as jsr310, so it's not about jodatime. When I originally wrote the question/answer... I even provided links to SO questions that addressed these issues in jodatime. – amaidment Aug 27 '13 at 08:51
  • @downvoter - care to explain? I don't mind criticism, but I prefer it to be constructive... – amaidment Jan 21 '14 at 08:23