[英]Format a local date at another time zone
我尝试在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)));
打印出来:
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为芝加哥和东京都打印。 为什么?
感谢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
最后一行应该是NY fmt: 8:51:54 PM EST
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)));
产量
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.