有没有办法仅针对特定类或方法设置时区?

发布于 2025-02-08 04:28:26 字数 1691 浏览 1 评论 0 原文

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

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

发布评论

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

评论(2

末骤雨初歇 2025-02-15 04:28:26

locadate。现在允许使用区域ID。

如果其日期使用Zone id day date date date.toinstant()。 (“”)。

.of ”> https://docs.oracle.com/javase/8/docs/api/java/java/java/date.html#toinstant--

noreferrer

Instant instant = Instant.parse("2022-06-18 00:00");
ZoneId zoneId = ZoneId.of("Asia/Tokyo");
LocalDateTime value LocalDateTime.ofInstant(instant, zoneId);

LocaDate.now allow with zone id.
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#now-java.time.ZoneId-

If its date use zone Id date.toInstant().atZone(ZoneId.of("")).toLocalDateTime()

https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toInstant--

Use Instant and zone id

Instant instant = Instant.parse("2022-06-18 00:00");
ZoneId zoneId = ZoneId.of("Asia/Tokyo");
LocalDateTime value LocalDateTime.ofInstant(instant, zoneId);
夜雨飘雪 2025-02-15 04:28:26

意图和问题的原因

是在DB中输入UTC中的时间。
时间:2022-06-17 15:00

我想将其转换为亚洲时间,以分区 localdateTime
(需要+09:00。)

我想要的时间:2022-06-18 00:00

但是,无论使用哪种方法,UTC标准中的时间都是输出的。

getmytime.atzone(...)。tolocaldateTime ..
输出:2022-06-17 15:00

在寻找一种方式时,我发现LocalDateTime忽略(或删除)“区域”信息。

创建时间时,可以使用区域信息创建它,但是区域信息已经创建后没有含义。

从那时起的解决方案

,我越来越接近答案,然后找到了它。

ZonedDateTime.of(localDateTime,ZoneId.systemDefault())
        .withZoneSameInstant(ZoneId.of("Asia/Seoul"))
        .toLocalDateTime();

获取基本时间的ZonEdateTime。

通过 with ZoneMameInstant 创建一个带有区域信息的即时,并将其创建为 localdateTime

这对我来说非常有用。

Intent and cause of the question

The time in UTC is entered in the DB.
time : 2022-06-17 15:00

I wanted to convert this to Asia time in a Zoned LocalDateTime.
(+09:00 is required.)

The time I want: 2022-06-18 00:00

However, no matter which method was used, the time in UTC standard was output as it is.

getMyTime.atZone(...).toLocalDateTime ..
Output: 2022-06-17 15:00

While looking for a way, I found that LocalDateTime ignores (or deletes) the "zone" information.

When creating time, it can be created with zone information, but zone information has no meaning after it has already been created.

The solution

From then on, I got closer to the answer, and I found it.

ZonedDateTime.of(localDateTime,ZoneId.systemDefault())
        .withZoneSameInstant(ZoneId.of("Asia/Seoul"))
        .toLocalDateTime();

Get the zoneDateTime of the base time.

and the key

Create an instant with zone information through withZoneSameInstant and create it as LocalDateTime.

This works perfectly for me.

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