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

Ton*_*ony 4 java format date

我尝试在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)));
Run Code Online (Sandbox Code Playgroud)

打印出来:

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
Run Code Online (Sandbox Code Playgroud)

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

感谢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)));
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

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

And*_*eas 10

LocalDateTime.atZone()表示:取当地时间并将其视为此时区的当地时间.

这意味着芝加哥当地时间下午6:19是美国东部时间下午6:19,东京时间6:19 PM"当地时间"是时区JST时间下午6:19 .

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

此类不存储或表示时区.相反,它是用于生日的日期的描述,结合在挂钟上看到的当地时间.如果没有附加信息(如偏移或时区),它不能代表时间线上的瞬间.

如果您想获取当前实时并查看它在芝加哥和东京的情况,那么您需要使用通用时间(Instant)或知道它所代表的时区的时间(ZonedDateTime).

运用 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)));
Run Code Online (Sandbox Code Playgroud)

产量

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
Run Code Online (Sandbox Code Playgroud)

运用 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)));
Run Code Online (Sandbox Code Playgroud)

产量

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
Run Code Online (Sandbox Code Playgroud)