gpt4 book ai didi

java - 将毫秒转换为 UTC 时间戳

转载 作者:行者123 更新时间:2023-11-30 05:30:31 24 4
gpt4 key购买 nike

我正在尝试将毫秒转换为时区 UTC 的时间戳,但它无法按预期工作,因为它转换为我的本地日期时间。

我尝试过以下。在调试代码时,我发现执行此命令时: new DateTime(eventDate) 它工作正常,因为它的值是 10:34:18.721 但后来 new Timestamp()将其更改为本地日期时间。

long eventDate = 1566297258721L;
DateTimeZone.setDefault(DateTimeZone.UTC);
Timestamp timestamp = new Timestamp(new DateTime(eventDate).getMillis());

我预计输出为:2019-08-20 10:34:18.721 但实际输出为:2019-08-20 12:34:18.721

最佳答案

您可以使用Java 8及更高版本的java.time包:

ZonedDateTime zonedDateTime = Instant.ofEpochMilli(1566817891743L).atZone(ZoneOffset.UTC);

关于java - 将毫秒转换为 UTC 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57656204/

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