gpt4 book ai didi

java - UTC 的 Joda DateTime getMillis() 不正确,而是返回本地毫秒

转载 作者:搜寻专家 更新时间:2023-11-01 03:03:47 25 4
gpt4 key购买 nike

我正在尝试获取 UTC 毫秒以转换 Joda DateTime instanceSQL Timestamp ,但是它正在将其转换为我的本地时间。

这是一个例子:

DateTime d = DateTime.now().withZone(DateTimeZone.UTC);
Timestamp t = new Timestamp(d.getMillis());
System.out.println(t);
t.setHours(d.getHourOfDay());
System.out.println(t);

输出:

2015-04-17 18:11:27.277
2015-04-17 22:11:27.277

为什么会转换为我本地的时区?我以为 DateTime.getMillis() 方法总是返回 UTC 的毫秒数?

最佳答案

当您更改时间时,您不会更改自纪元以来的毫秒数。换句话说,调用 withZone 实际上并没有改变底层的 long millis

真正的问题是 Timestamp.setHours 方法,该方法自 Java 1.1 以来已被弃用。请参阅文档:

@Deprecated

public void setHours(int hours)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.HOUR_OF_DAY, int hours). Sets the hour of this Date object to the specified value. This Date object is modified so that it represents a point in time within the specified hour of the day, with the year, month, date, minute, and second the same as before, as interpreted in the local time zone.

关于java - UTC 的 Joda DateTime getMillis() 不正确,而是返回本地毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710303/

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