TimeZone 说明
public static TimeZone getTimeZone(ZoneId zoneId);
这个函数使用的参数 ZoneId 的所有值可以通过以下方式获取
for (String id: TimeZone.getAvailableIDs()) {
System.out.println(id);
}
参看:https://bytenota.com/get-list-of-all-timezone-ids-in-java/
时区转换
时间可读字符串切换时区
如果只是显示需要,最简单的方法就是通过 SimpleDateFormat
代码示例:
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat formatWithTimezone = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatWithTimezone.setTimeZone(TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai")));
System.out.println(formatWithTimezone.format(date));
formatWithTimezone = new SimpleDateFormat("yyyy-MM-dd H