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

查看:29
本文介绍了在 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;

如果你有日历对象,你可以调用

If you have Calendar objects you can call

c.getTimeInMillis()

做同样的事情

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

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