public static void main(String[] args) {Date date = new Date();//获取世界的中心时区时间Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();Map<String, String> timeZoneOffset = new HashMap<>();availableZoneIds.stream().forEach(AvailableZoneIds -> {//获取中心时间Long centerTime = convertTimeZone(date, "UTC+0").getTime();//计算某个时间的偏移量Long somewhereTime = convertTimeZone(date, AvailableZoneIds).getTime();Integer offset = (int) ((somewhereTime - centerTime) / (1000 * 60 * 60));String offsetStr = offset>=0? "+"+offset:offset.toString();System.out.println(AvailableZoneIds + "的偏移量是UTC" + offsetStr);timeZoneOffset.put(AvailableZoneIds,"UTC"+offsetStr);});System.out.println(timeZoneOffset.get("US/Pacific"));}public static Date convertTimeZone(Date date, String timeZone) {try {String formatDate = convertLocalDateTimeFormat(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(), timeZone, TimeZoneUtils.format);SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TimeZoneUtils.format);return simpleDateFormat.parse(formatDate);}catch (Exception e){throw new RuntimeException("时区转换异常",e);}}public static String convertLocalDateTimeFormat(LocalDateTime localDateTime,String timeZone,String format){LocalDateTime convertLocalDateTime = convertLocalDateTime(localDateTime, timeZone);DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);return convertLocalDateTime.format(dateTimeFormatter);}
执行效果