如何减去两个 joda 日期时间?

2022-09-01 01:54:32

我知道这不是“它应该工作的方式”,但仍然:如果你有两个DateTime对象,那么减去它们的好方法是什么?将它们转换为日期对象?

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");

答案 1

Period 有一个构造函数,它采用两个 ReadableInstant 实例

Period diff = new Period(start, end);

(ReadableInstant是由 以及其他类实现的接口。DateTime


答案 2

从你的例子中,你似乎想要秒数的差异,所以这应该有所帮助

Seconds diff = Seconds.secondsBetween(start, end);