日期时间格式化程序显示 (GMT-8:00) 而不是 PST
Date date = new Date(2024,11,23);
ZoneId zone = ZoneId.of("US/Pacific");
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss XXX zzz");
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.of(zone.toString()));
System.out.println(zonedDateTime.format(formatter1));
我期待的是:时区缩写中的 PST 或 PDT。但将于 (GMT-08:00)
这是因为使用了
zzz
而不是 v
,您应该将 Locale
应用于 DateTimeFormatter
。
以下代码打印
2024/11/23 12:00:00 -08:00 PT
:
// use a local date instead of a date
LocalDate date = LocalDate.of(2024,11,23);
// use a more specific zone id
ZoneId zone = ZoneId.of("America/Los_Angeles");
// use v instead of zzz, uuuu instead of yyyy and apply a Locale
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("uuuu/MM/dd hh:mm:ss XXX v", Locale.ENGLISH);
// then take the beginning of the day in the zone
ZonedDateTime zonedDateTime = date.atStartOfDay(zone);
// and print the resul
System.out.println(zonedDateTime.format(formatter1));