gpt4 book ai didi

Java:将 Date.getTime 与不同的日期进行比较

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:53 25 4
gpt4 key购买 nike

有日期date1以格式 YY-MM-dd hh:mm:ss.SSS 给出我想比较一下

date1.getTime()

通过执行操作检索到一个

new Date().getTime()

SimpleDateFormat sf = new SimpleDateFormat("YY-MM-dd hh:mm:ss.SSS");
Date date1 = sf.parse(date1AsString);
...
compare date1.getTime() with new Date().getTime();

如何将这两个日期带到一个共同的“时区”来进行比较?如何使 date1 与 new Date() 具有相同的“时间长度”?我想要相同的时区...谢谢

最佳答案

来自javadoc 日期

The class Date represents a specific instant in time, with millisecond precision.

时间实例与我们对时区增强的时间定义无关。 现在对你我来说都是一样的,无论我们(可能)位于不同的时区。

添加时区概念的是 DateFormat

The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.

当您调用Date#getTime()时,你回来

the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

您可以使用它来比较 Date 对象,因为它们具有相同的根。同样,compareTo将会返回

the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

关于Java:将 Date.getTime 与不同的日期进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27509817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com