如何在过去的特定时间使用UTC创建Date对象? [英] How to create a Date object, using UTC, at a specific time in the past?
问题描述
是否可以创建一个 java.util.Date
对象,该对象保证为UTC并且在过去的特定时间,例如一小时前或一天以前,这是一个好主意吗?
Is it possible to create a java.util.Date
object that is guaranteed to be UTC and at a specific time in the past, like an hour ago or a day ago, and is this a good idea?
我查看了Stack Overflow问题
获取4小时前的日期及其答案。但我希望避免添加更多依赖项,例如 jodaTime
,并且接受的答案使用 System.currentTimeMillis()
这不是本地时区,不是吗?
I have reviewed the Stack Overflow question
get date as of 4 hours ago and its answers. But I want to avoid having to add more dependencies, like jodaTime
, and the accepted answer uses System.currentTimeMillis()
which would be the local timezone, would it not?
推荐答案
正如评论中生动地讨论的那样,建议使用 java.time
包。简单的解决方案:
As discussed vividly in the comments, the recommendation is to use the java.time
package. The easy solution:
Instant fourHoursAgo = Instant.now().minus(Duration.ofHours(4));
System.out.println(fourHoursAgo);
这只是打印
2018-01-31T14:57:44.667255Z
由于现在是UTC时间 18:58
,输出就是你要求的。 Instant
本身偏移中性。它的 toString
以UTC格式提供时间,但在生成 Instant
时没有提及UTC,所以它是否给你你想要什么,我不确定。我稍后会在UTC中给你一个显式的结果。
Since UTC time is now 18:58
, the output is what you asked for. The Instant
itself is offset neutral. Its toString
delivers time in UTC, but there was no mention of UTC in producing the Instant
, so whether it gives you what you want, I am not sure. I will give you a result that is explicitly in UTC later.
但首先,如果你需要一个 java .util.Date
,通常对于您无法更改的旧API,转换很简单:
But first, if you do need a java.util.Date
, typically for a legacy API that you cannot change, the conversion is easy:
Date oldfashionedDate = Date.from(fourHoursAgo);
System.out.println(oldfashionedDate);
在我的电脑上欧洲/哥本哈根
时间打印区域:
On my computer in Europe/Copenhagen
time zone this printed:
Wed Jan 31 15:57:44 CET 2018
同样,这与运行代码段前四小时的时间一致。同样, Date
中没有UTC偏移量。只有它的 toString
方法才能获取我的JVM的时区设置并用它来生成字符串,这不会影响 Date
。请参阅Stack Overflow问题,如何设置时区一个java.util.Date?及其答案。
Again, this agrees with the time four hours before running the snippet. And again, a Date
doesn’t have a UTC offset in it. Only its toString
method grabs my JVM’s time zone setting and uses it for generating the string, this does not affect the Date
. See the Stack Overflow question, How to set time zone of a java.util.Date?, and its answers.
正如所承诺的那样,如果你不仅需要代表时间而且还要代表偏移量,请使用 OffsetDateTime
:
As promised, if you do need to represent not only the time but also the offset, use an OffsetDateTime
:
OffsetDateTime fourHoursAgoInUtc = OffsetDateTime.now(ZoneOffset.UTC).minusHours(4);
System.out.println(fourHoursAgoInUtc);
此印刷
2018-01-31T14:57:44.724147Z
Z
在结尾处意味着从UTC或祖鲁时区(这不是真正的时区)偏移零。转换为日期
并不比以前复杂得多,但同样,您将失去转换中的偏移信息:
Z
at the end means offset zero from UTC or "Zulu time zone" (which isn’t a true time zone). The conversion to a Date
is not much more complicated than before, but again, you will lose the offset information in the conversion:
Date oldfashionedDate = Date.from(fourHoursAgoInUtc.toInstant());
System.out.println(oldfashionedDate);
此印刷:
Wed Jan 31 15:57:44 CET 2018
这篇关于如何在过去的特定时间使用UTC创建Date对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!