ripper234 ripper234 - 5 months ago 13
Java Question

How to subtract two joda datetimes?

I know this isn't "the way it's supposed to work", but still: If you have two DateTime objects, what's a good way to subtract them? Convert them to Date objects?

DateTime start = new DateTime();
System.out.println(start + " - doing some stuff");

// do stuff

DateTime end = new DateTime();
Period diff = // end - start ???
System.out.println(end + " - doing some stuff took diff seconds");

Answer

Period has a constructor that takes two ReadableInstant instances:

Period diff = new Period(start, end);

(ReadableInstant is an interface implemented by DateTime, as well as other classes.)

Comments