教程中心 / Java教程 / 时区和ZonedDateTime

时区和ZonedDateTime

12分钟 日期时间

ZoneId

 ZoneId zone = ZoneId.of("Asia/Shanghai");
ZoneId.systemDefault();
Set zones = ZoneId.getAvailableZoneIds(); 

ZonedDateTime

 ZonedDateTime zdt = ZonedDateTime.now();
ZonedDateTime zdt2 = ZonedDateTime.of(2026, 6, 15, 10, 30, 0, 0, zone);

ZonedDateTime converted = zdt.withZoneSameInstant(ZoneId.of("America/New_York")); 

Instant

 Instant instant = Instant.now();
instant.atZone(zone);

long epochSecond = instant.getEpochSecond();
Instant fromEpoch = Instant.ofEpochSecond(epochSecond); 

小结

  • ZoneId表示时区ID
  • ZonedDateTime带时区的日期时间
  • Instant表示时间戳(UTC)
  • withZoneSameInstant转换时区
☕ Java 在线代码编辑器
📝 运行结果