我知道这不是“它应该工作的方式”,但仍然是:如果你有两个DateTime对象,那么减去它们的好方法是什么?将它们转换为Date对象?
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");
答案 0 :(得分:54)
期间有a constructor that takes two ReadableInstant
instances:
Period diff = new Period(start, end);
(ReadableInstant
是由DateTime
以及其他类实现的接口。)
答案 1 :(得分:19)
从您的示例中,您似乎想要以秒为单位的差异this should help:
Seconds diff = Seconds.secondsBetween(start, end);
答案 2 :(得分:5)
这有帮助吗? http://joda-time.sourceforge.net/key_period.html 它显示了以下示例
DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);
// period of 1 year and 7 days
Period period = new Period(start, end);
// calc will equal end
DateTime calc = start.plus(period);
// able to calculate whole days between two dates easily
Days days = Days.daysBetween(start, end);
答案 3 :(得分:3)
取决于您想要获得的精确度。您应该检查org.joda.time
包并检查帮助程序类,例如Hours
,Days
等。
答案 4 :(得分:1)
我认为您可以使用this构造函数创建Period
,该构造函数需要两个DateTime
个对象。