1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 获取ZoneId 收录的时区和偏移量

获取ZoneId 收录的时区和偏移量

时间:2021-12-15 22:57:07

相关推荐

获取ZoneId 收录的时区和偏移量

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);}

执行效果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。