【问题标题】:How to get ZoneId of SAST?如何获取 SAST 的 ZoneId?
【发布时间】:2020-08-03 08:46:51
【问题描述】:

我需要从一个文件中解析一个时区,其中指定的区域如下:UTC、SAST 等。

问题:虽然ZoneId.of("UTC") 效果很好,但我不能对 SAST 做同样的事情。

我收到java.time.zone.ZoneRulesException: Unknown time-zone ID: SAST

问题:如何将“SAST”字符串转换为Java的ZoneId

我知道,每次收到时我都可以将“SAST”替换为“GMT+2”,但如果有更优雅的方式就好了。

【问题讨论】:

  • 使用完整的时区名称以避免冲突ZoneId.of("Africa/Johannesburg") 切勿使用 3-4 个字符的缩写来表示时区。

标签: java java-8 timezone java-time


【解决方案1】:

根据oracle documentation,南非标准时间 (SAST) 是非洲/约翰内斯堡。 所以你应该使用:

ZoneId.of("Africa/Johannesburg")

【讨论】:

    【解决方案2】:
    DisplayZoneAndOffSet.java
    package com.mkyong;
    
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    public class DisplayZoneAndOffSet {
    
        public static final boolean SORT_BY_REGION = false;
    
        public static void main(String[] argv) {
    
            Map<String, String> sortedMap = new LinkedHashMap<>();
    
            Map<String, String> allZoneIdsAndItsOffSet = getAllZoneIdsAndItsOffSet();
    
            //sort map by key
            if (SORT_BY_REGION) {
                allZoneIdsAndItsOffSet.entrySet().stream()
                        .sorted(Map.Entry.comparingByKey())
                        .forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
            } else {
                // sort by value, descending order
                allZoneIdsAndItsOffSet.entrySet().stream()
                        .sorted(Map.Entry.<String, String>comparingByValue().reversed())
                        .forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
            }
    
            // print map
            sortedMap.forEach((k, v) ->
            {
                String out = String.format("%35s (UTC%s) %n", k, v);
                System.out.printf(out);
            });
    
            System.out.println("\nTotal Zone IDs " + sortedMap.size());
    
        }
    
        private static Map<String, String> getAllZoneIdsAndItsOffSet() {
    
            Map<String, String> result = new HashMap<>();
    
            LocalDateTime localDateTime = LocalDateTime.now();
    
            for (String zoneId : ZoneId.getAvailableZoneIds()) {
    
                ZoneId id = ZoneId.of(zoneId);
    
                // LocalDateTime -> ZonedDateTime
                ZonedDateTime zonedDateTime = localDateTime.atZone(id);
    
                // ZonedDateTime -> ZoneOffset
                ZoneOffset zoneOffset = zonedDateTime.getOffset();
    
                //replace Z to +00:00
                String offset = zoneOffset.getId().replaceAll("Z", "+00:00");
    
                result.put(id.toString(), offset);
    
            }
    
            return result;
    
        }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-07
      • 2019-12-07
      • 2021-10-25
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 2019-04-11
      • 2023-03-18
      相关资源
      最近更新 更多