在Java中,如何获得两个日期之间的秒数差异? [英] In Java, how do I get the difference in seconds between 2 dates?

查看:1698
本文介绍了在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()

并执行相同的

这篇关于在Java中,如何获得两个日期之间的秒数差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆