区域的 LocalDateTime 不显示区域的一天中的小时
LocalDateTime at zone doesn’t display the zone’s hour of day
我在 LocalDateTime lastUpdated
中有一个 UTC 日期和时间。我想显示给定区域的时间和日期,但无论在哪个区域,我都会得到同一天的时间。对于以下代码:
System.out.println(lastUpdated);
System.out.println(lastUpdated.atZone(ZoneId.of("Europe/Paris")));
System.out.println(lastUpdated.atZone(ZoneId.of("America/Los_Angeles")));
我得到:
2018-05-26T21:33:46
2018-05-26T21:33:46+02:00[Europe/Paris]
2018-05-26T21:33:46-07:00[America/Los_Angeles]
但我想得到的是:
2018-05-26T21:33:46
2018-05-26T**23**:33:46+02:00[Europe/Paris]
2018-05-26T**14**:33:46-07:00[America/Los_Angeles]
区域信息对我来说是可选的。我只需要适当的时间在区域。有办法实现吗?
您正在使用 LocalDateTime
,这是一个没有时区的日期和时间指示,就像您在与您所在时区的人讨论日期和时间时使用的那样。该日期的 atZone
returns a ZonedDateTime
就像您在那个时区谈论它一样。
因此,为了获得不同的时间,您需要将 ZonedDateTime
转换为 Instant
,这是一个时间点,为整个星球标识。然后 Instant
可以再次转换为 ZonedDateTime
。
LocalDateTime lastUpdated = LocalDateTime.now();
ZonedDateTime zonedDateTime = lastUpdated.atZone(ZoneId.of("Europe/Paris"));
System.out.println(zonedDateTime);
ZonedDateTime other = ZonedDateTime.ofInstant(zonedDateTime.toInstant(), ZoneId.of("America/Los_Angeles"));
System.out.println(other);
输出:
2018-05-27T21:53:53.754+02:00[Europe/Paris]
2018-05-27T12:53:53.754-07:00[America/Los_Angeles]
我的印象是你混淆了 "no timezone information" 和 "UTC time zone... Because when I don't say what's the time zone it's UTC... Right?"
错了。如果您不说时区是什么,那么您就没有关于时区的任何具体信息。
所以你的 2018-05-26T21:33:46 不是日期和时间,UTC,是这个日期的日期和时间的概念,不知道时区的概念存在。当你去问某人今天是几号和几点钟时,你会得到什么。不,他们不会认为您可能还想考虑它在您当前所在的时区内。这个人根本不会认为存在时区这样的东西,但他会能说出现在是什么日期和时间。
因此,要将 UTC 日期时间转换为其他时区的日期时间,您可以这样做:
ZonedDateTime time = lastUpdated.atZone(ZoneId.of("UTC"));
System.out.println(time);
System.out.println(time.withZoneSameInstant(ZoneId.of("Europe/Paris")));
System.out.println(time.withZoneSameInstant(ZoneId.of("America/Los_Angeles")));
我在 LocalDateTime lastUpdated
中有一个 UTC 日期和时间。我想显示给定区域的时间和日期,但无论在哪个区域,我都会得到同一天的时间。对于以下代码:
System.out.println(lastUpdated);
System.out.println(lastUpdated.atZone(ZoneId.of("Europe/Paris")));
System.out.println(lastUpdated.atZone(ZoneId.of("America/Los_Angeles")));
我得到:
2018-05-26T21:33:46
2018-05-26T21:33:46+02:00[Europe/Paris]
2018-05-26T21:33:46-07:00[America/Los_Angeles]
但我想得到的是:
2018-05-26T21:33:46
2018-05-26T**23**:33:46+02:00[Europe/Paris]
2018-05-26T**14**:33:46-07:00[America/Los_Angeles]
区域信息对我来说是可选的。我只需要适当的时间在区域。有办法实现吗?
您正在使用 LocalDateTime
,这是一个没有时区的日期和时间指示,就像您在与您所在时区的人讨论日期和时间时使用的那样。该日期的 atZone
returns a ZonedDateTime
就像您在那个时区谈论它一样。
因此,为了获得不同的时间,您需要将 ZonedDateTime
转换为 Instant
,这是一个时间点,为整个星球标识。然后 Instant
可以再次转换为 ZonedDateTime
。
LocalDateTime lastUpdated = LocalDateTime.now();
ZonedDateTime zonedDateTime = lastUpdated.atZone(ZoneId.of("Europe/Paris"));
System.out.println(zonedDateTime);
ZonedDateTime other = ZonedDateTime.ofInstant(zonedDateTime.toInstant(), ZoneId.of("America/Los_Angeles"));
System.out.println(other);
输出:
2018-05-27T21:53:53.754+02:00[Europe/Paris]
2018-05-27T12:53:53.754-07:00[America/Los_Angeles]
我的印象是你混淆了 "no timezone information" 和 "UTC time zone... Because when I don't say what's the time zone it's UTC... Right?"
错了。如果您不说时区是什么,那么您就没有关于时区的任何具体信息。
所以你的 2018-05-26T21:33:46 不是日期和时间,UTC,是这个日期的日期和时间的概念,不知道时区的概念存在。当你去问某人今天是几号和几点钟时,你会得到什么。不,他们不会认为您可能还想考虑它在您当前所在的时区内。这个人根本不会认为存在时区这样的东西,但他会能说出现在是什么日期和时间。
因此,要将 UTC 日期时间转换为其他时区的日期时间,您可以这样做:
ZonedDateTime time = lastUpdated.atZone(ZoneId.of("UTC"));
System.out.println(time);
System.out.println(time.withZoneSameInstant(ZoneId.of("Europe/Paris")));
System.out.println(time.withZoneSameInstant(ZoneId.of("America/Los_Angeles")));