java 在另一个时区格式化本地日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47128190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:30:13  来源:igfitidea点击:

Format a local date at another time zone

javadateformat

提问by Tony

I try to format the current time in 2 locations: Chicago and Tokyo

我尝试在 2 个地点格式化当前时间:芝加哥和东京

LocalDateTime now = LocalDateTime.now();
ZonedDateTime chicago = now.atZone(ZoneId.of("America/New_York"));
System.out.println("chicago: " + chicago);
System.out.println("Chicago formated: " + chicago.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));

ZonedDateTime tokyo = now.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println("Tokyo: " + tokyo);
System.out.println("Tokyo formated: " + tokyo.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));

The print out:

打印出来:

chicago: 2017-11-05T18:19:01.441-05:00[America/New_York]
Chicago formated: 6:19:01 PM EST
Tokyo: 2017-11-05T18:19:01.441+09:00[Asia/Tokyo]
Tokyo formated: 6:19:01 PM JST

6:19:01 PMprinted for both Chicago and Tokyo. Why?

下午 6:19:01为芝加哥和东京打印。为什么?

Thanks Andreas for make the above code work. Following your logic I try to make this work:

感谢 Andreas 使上述代码工作。按照您的逻辑,我尝试使这项工作:

LocalDateTime PCTime = LocalDateTime.now();//Chicago: 7:51:54 PM
ZonedDateTime newYorkTime = PCTime.atZone(ZoneId.of("America/New_York"));
System.out.println("now: " + newYorkTime);
System.out.println("now fmt: " + newYorkTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));
ZonedDateTime newYorkTime0 = newYorkTime.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println("N.Y. fmt: " + newYorkTime0.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));

The output:

输出:

now: 2017-11-05T19:51:54.940-05:00[America/New_York]
now fmt: 7:51:54 PM EST
N.Y. fmt: 7:51:54 PM EST

The last line should be N.Y. fmt: 8:51:54 PM EST

最后一行应该是 N.Y. fmt: 8:51:54 PM EST

回答by Andreas

LocalDateTime.atZone()means: Take this local time and consider it to be the local time in this time zone.

LocalDateTime.atZone()意思是:取这个本地时间并认为它是这个时区的本地时间。

That means that 6:19 PM "local time" in Chicago is ... 6:19 PM in time zone EST, and that 6:19 PM "local time" in Tokyo is ... 6:19 PM in time zone JST.

这意味着芝加哥的“当地时间”下午 6:19 是美国东部标准时间的下午 6:19,东京的“当地时间”下午 6:19 是日本标准时间的下午 6:19 .

When you call LocalDateTime.now(), you get the local time in your current default time zone, but the returned value don't know what time zone that is. As the javadocsays:

当您调用 时LocalDateTime.now(),您将获得当前默认时区的本地时间,但返回的值不知道那是哪个时区。正如javadoc所说:

This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

此类不存储或表示时区。相反,它是对日期的描述,用于生日,结合挂钟上的当地时间。如果没有偏移量或时区等附加信息,它就无法表示时间线上的瞬间。

If you want to take the current realtime and see what it is in Chicago and Tokyo, then you need to use either a universal time (Instant) or a time that know what time zone it represents (ZonedDateTime).

如果您想获取当前的实时时间并查看芝加哥和东京的时间,那么您需要使用世界时 ( Instant) 或知道它代表哪个时区的时间 ( ZonedDateTime)。

Using Instant

使用 Instant

Instant now = Instant.now();
ZonedDateTime chicago = now.atZone(ZoneId.of("America/New_York"));
System.out.println("Chicago: " + chicago);
System.out.println("Chicago formated: " + chicago.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));

ZonedDateTime tokyo = now.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println("Tokyo: " + tokyo);
System.out.println("Tokyo formated: " + tokyo.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));

Output

输出

Chicago: 2017-11-05T20:02:45.444-05:00[America/New_York]
Chicago formated: 8:02:45 PM EST
Tokyo: 2017-11-06T10:02:45.444+09:00[Asia/Tokyo]
Tokyo formated: 10:02:45 AM JST

Using ZonedDateTime

使用 ZonedDateTime

ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime chicago = now.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println("Chicago: " + chicago);
System.out.println("Chicago formated: " + chicago.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));

ZonedDateTime tokyo = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println("Tokyo: " + tokyo);
System.out.println("Tokyo formated: " + tokyo.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)));

Output

输出

Chicago: 2017-11-05T20:04:19.368-05:00[America/New_York]
Chicago formated: 8:04:19 PM EST
Tokyo: 2017-11-06T10:04:19.368+09:00[Asia/Tokyo]
Tokyo formated: 10:04:19 AM JST