Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda Time get Duration between two Instants

Tags:

java

jodatime

I have two Instant objects and would like to get a Duration indicating how much time is between them, however Instant.minus only accepts either a ReadableDuration or a long for the amount of time to change by, and doesn't seem to have any method for subtracting two instants to get the difference between them.

Other answers that I found seem to suggest converting both instants to milliseconds and doing the math myself, but I would prefer something that retains type safety and doesn't require re-implementing end - start logic.

How can I get a Duration from two Instants?

like image 662
zstewart Avatar asked Jul 18 '16 20:07

zstewart


1 Answers

Duration has a constructor which takes two ReadableInstant objects for the start and end of the Duration.

http://joda-time.sourceforge.net/apidocs/org/joda/time/Duration.html#Duration(org.joda.time.ReadableInstant,%20org.joda.time.ReadableInstant)

like image 110
zstewart Avatar answered Nov 02 '22 08:11

zstewart