0

I currently have two Calendar instances that are set to specific dates and times, they are:

Fri 14th March 2015 - 09:00:00 GMT
Mon 17th March 2015 - 10:30:15 GMT

What i'd like is a way to get the difference in days, hours, minutes and seconds, in a Day:HH:MM:SS format. Note the dates can be in different timezones. So in this example, the answer should be:

3:1:30:15

Any idea how I can achieve this?

MisterIbbs
  • 247
  • 1
  • 7
  • 20
  • 1
    Maybe [this](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) or (http://stackoverflow.com/questions/17940200/how-to-find-the-duration-of-difference-between-two-dates-in-java) could help you ;) – Zorian Mar 17 '15 at 12:06
  • I would use Joda Time, and its Interval class: http://joda-time.sourceforge.net/key_interval.html – facundofarias Mar 17 '15 at 12:07
  • How do you want to handle daylight saving time changes? Is this a diff between "local" dates and times (where 2am to 2am is always deemed "1 day") or a difference between points in time (where 2am to 2am could be 25 hours or 23 hours, as well as 24 hours)? – Jon Skeet Mar 17 '15 at 12:08
  • The dates could be in different timezones. The first date is always Europe/London, but the second one could be any timezone. It is a difference between points in time, so essentially a year is also involved in the difference. – MisterIbbs Mar 17 '15 at 12:11
  • You could first normalize both global timestamps to the same timezone (for ex. Europe/London) and then calculate the duration between the associated local timestamp representations. Are you open for other libraries? I ask because this is very hard in old JDK-classes like `java.util.Calendar`. – Meno Hochschild Mar 17 '15 at 14:24
  • Convert both dates into java.time.ZonedDateTime. Then use the static method to calculate the duration: `Duration d = Duration.between(dt1, dt2);` and format the duration accordingly. – Klitos Kyriacou Mar 17 '15 at 14:47

0 Answers0