gpt4 book ai didi

java - 从 Instant 和 ZoneId 形成 ZonedDateTime 时的预期行为是什么?

转载 作者:行者123 更新时间:2023-11-29 08:44:10 25 4
gpt4 key购买 nike

给定一个由时间戳(例如“2016-06-07 08-01-55”)和特定 ZoneId(“欧洲/柏林”)组成的 Instant,此代码的预期结果是什么?

ZonedDateTime.ofInstant(timestamp.toInstant, zoneId)

会不会

'2016-06-07 08-01-55 +02:00' (time doesn't change, but ZoneId is changed)

'2016-06-07 10-01-55 +02:00' (time & ZoneId are changed)

我问这个问题是因为我在不同的环境中看到了这两种行为。

最佳答案

假设您正确设置了每个参数,输出是确定性的。使用您的数据:

LocalDateTime datetime = LocalDateTime.of(2016, 6, 7, 8, 1, 55);
ZonedDateTime zdt = datetime.atZone(ZoneId.of("Europe/Berlin"));
Instant instant = zdt.toInstant();

Timestamp ts = Timestamp.from(instant); //The timestamp you describe in your question

ZonedDateTime result = ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.of("Europe/Berlin"));

System.out.println(result); //WILL ALWAYS PRINTS: 2016-06-07T08:01:55+02:00[Europe/Berlin]

关于java - 从 Instant 和 ZoneId 形成 ZonedDateTime 时的预期行为是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37675983/

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