ripper234 ripper234 - 1 year ago 90
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 Source

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.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download