Java 库,将两个 Date 对象相加/相减,精度达到毫秒

发布于 2024-11-10 03:13:57 字数 288 浏览 7 评论 0原文

我正在查看 java Date 库 1. java.util.Date, 2. 日期4J 3. Joda-time

看看我是否可以对两个日期对象执行时间减法,精确到毫秒。

我收到 2011-05-29T22:50:12.692 作为字符串,并通过使用 SimpleDateFormat 解析它将其转换为 Date 对象。

另一个 Date 对象也将收到一个 String。我想从两个 Date 对象中减去。

有什么想法吗?

基本上我想获得两个 Date 对象之间的间隔,精确到毫秒。

I was looking thru java Date libraries
1. java.util.Date,
2. Date4J
3. Joda-time

to find out whether I can perform time subtraction to two Date Objects, to the precision of milliseconds.

I receive 2011-05-29T22:50:12.692 as a String, and convert it into a Date object by parsing it with SimpleDateFormat.

The other Date object will also be received a String. and I want to subtract from two Date objects.

Any ideas?

Basically i want to get an interval between the two Date objects, to the precision of milliseconds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

拍不死你 2024-11-17 03:13:57

尝试使用 Date.getTime()

long timeBetweenInMillis = d2.getTime() - d1.getTime();

编辑

这假设d2按时间顺序晚于d1

Try using Date.getTime()

long timeBetweenInMillis = d2.getTime() - d1.getTime();

EDIT

This assumes that d2 is chronologically after d1.

撩动你心 2024-11-17 03:13:57

在 java.util.Date 中有一个名为 Date.getTime() 的方法,您可以使用它来获取以英里为单位的时间。

in java.util.Date there is a method called Date.getTime() that you can use to get the time in milies.

凤舞天涯 2024-11-17 03:13:57

你的意思是这样吗?

Date d1 = 
Date d2 =
long intervalInMillis = d1.getTime() - d2.getTime();

Do you mean like this?

Date d1 = 
Date d2 =
long intervalInMillis = d1.getTime() - d2.getTime();
童话 2024-11-17 03:13:57

Date() 对象的内部表示是纪元过去的毫秒数。只需使用 getTime() 方法获取这些值并对它们进行算术即可。然后,您可以根据该值构造一个新的 Date 对象,然后就完成了。

The internal representation of a Date() object is the number of milliseconds past the epoch. Just get those values with the getTime() method and do arithmetic on them. You can then construct a new Date object based on that value and you are done.

权谋诡计 2024-11-17 03:13:57

只需获取毫秒差异:

long millis = date1.getTime() - date2.getTime();

Just get the millisecond difference:

long millis = date1.getTime() - date2.getTime();
月竹挽风 2024-11-17 03:13:57

您可以利用 XMLGregorianCalendar 而不是 SimpleDateFormat (注意 SimpleDateFormat 不是线程安全的: http://www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-and-thread-safety.html):

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Demo {

    public static void main(String[] args) throws Exception {
        DatatypeFactory df = DatatypeFactory.newInstance();
        XMLGregorianCalendar date2 = df.newXMLGregorianCalendar("2011-05-29T22:50:12.692");
        XMLGregorianCalendar date1 = df.newXMLGregorianCalendar("2011-03-29T22:50:12.692");

        System.out.println(date2.toGregorianCalendar().getTimeInMillis() - date1.toGregorianCalendar().getTimeInMillis());
    }

}

You could leverage XMLGregorianCalendar instead of SimpleDateFormat (Note SimpleDateFormat is not thread safe: http://www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-and-thread-safety.html):

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Demo {

    public static void main(String[] args) throws Exception {
        DatatypeFactory df = DatatypeFactory.newInstance();
        XMLGregorianCalendar date2 = df.newXMLGregorianCalendar("2011-05-29T22:50:12.692");
        XMLGregorianCalendar date1 = df.newXMLGregorianCalendar("2011-03-29T22:50:12.692");

        System.out.println(date2.toGregorianCalendar().getTimeInMillis() - date1.toGregorianCalendar().getTimeInMillis());
    }

}
疑心病 2024-11-17 03:13:57

这就是您想要的:

Date result = new Date(d2.getTime() - d1.getTime());

解释:

d2.getTime() - d1.getTime() 

返回日期之间的毫秒差

new Date(milliseconds);

创建一个新的 Date 对象,设置为自纪元以来的毫秒数。

编辑

如果您只想要正差异,请将其更改为此。这样,输入的顺序并不重要:

Date result = new Date(Math.abs(d2.getTime() - d1.getTime()));

This is what you want:

Date result = new Date(d2.getTime() - d1.getTime());

Explanation:

d2.getTime() - d1.getTime() 

Returns millisecond difference between the dates

new Date(milliseconds);

Creates a new Date object set to number of milliseconds since epoch.

Edit

IF you only want the positive difference, change it to this. This way it doesn't matter what order you put in inputs:

Date result = new Date(Math.abs(d2.getTime() - d1.getTime()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文