在Java中,如何获得两个日期之间的秒数差异?

问题描述:

Java类库有一个名为DateTime的类。 DateTime有这样的方法:

The Java class library has a class named DateTime. DateTime has this method:

int daysBetween(DateTime other)

它返回此参数与参数之间的天数。
它没有方法

which returns the number of days between this and the parameter. It doesn't have a method

int secondsBetween(DateTime other)

我偶然需要。
是否有类与DateTime类似,但是有这样的方法?

which I happen to need. Is there a class which is similar to DateTime but has such a method?

不熟悉DateTime ..

Not familiar with DateTime...

如果您有两个日期,您可以在其上调用getTime获得毫秒数,得到差分并除以1000.例如

If you have two Dates you can call getTime on them to get millseconds, get the diff and divide by 1000. For example

Date d1 = ...;
Date d2 = ...;
long seconds = (d2.getTime()-d1.getTime())/1000;

如果您有Calendar对象,您可以调用

If you have Calendar objects you can call

c.getTimeInMillis()

并执行相同的