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

标签 java date format

我尝试格式化两个位置的当前时间:芝加哥和东京

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)));

打印输出:

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 PM 为芝加哥和东京打印。为什么?

感谢 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)));

输出:

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

最后一行应该是 N.Y. fmt:美国东部时间下午 8:51:54

最佳答案

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

这意味着芝加哥的“本地时间”下午 6:19 是美国东部时间...下午 6:19,而东京的“本地时间”下午 6:19 是...美国东部时间下午 6:19时区 JST。

当您调用 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.

如果您想获取当前的实时时间并查看芝加哥和东京的时间,那么您需要使用世界时(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)));

输出

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

使用 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)));

输出

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

关于java - 在另一个时区格式化本地日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128190/

相关文章:

php - 为什么PC和笔记本电脑的代码结果不同

java - 并发发送带附件的 HtmlEmail 和 "Too many open files"错误

java - Java 上的正则表达式日期格式验证

yyyy-mm-dd hh :mm:ss aa help 的 JavaScript 转换

postgresql - 什么是默认的 PostgreSQL 日志格式

c# - 准时双排版

sql - 按未格式化日期 sqlite 排序

java - 使用 ThreadCount TestNG 限制并行测试的数量

java - java代码中的警告问题

java - 我在spring和freemarker集成中遇到的问题