How to get ZoneId of SAST?

huangapple go评论80阅读模式
英文:

How to get ZoneId of SAST?

问题

I need to parse a time zone from a file in which zones are specified like this: UTC, SAST, etc.

Problem: While ZoneId.of("UTC") works well, I cannot do the same for SAST.

I get java.time.zone.ZoneRulesException: Unknown time-zone ID: SAST.

Question: How to convert "SAST" string to Java's ZoneId?

I know, I can replace "SAST" by "GMT+2" every time I receive it, but it would be great if there was a more elegant way.

英文:

I need to parse a time zone from a file in which zones are specified like this: UTC, SAST, etc.

Problem: While ZoneId.of("UTC") works well, I cannot do the same for SAST.

I get java.time.zone.ZoneRulesException: Unknown time-zone ID: SAST.

Question: How to convert "SAST" string to Java's ZoneId?

I know, I can replace "SAST" by "GMT+2" every time I receive it, but it would be great if there was a more elegant way.

答案1

得分: 6

根据Oracle文档,南非标准时间(SAST)是Africa/Johannesburg。
因此,您应该使用:

ZoneId.of("Africa/Johannesburg")
英文:

According to oracle documentation, South Africa Standard Time (SAST) is Africa/Johannesburg.
So you should use :

ZoneId.of("Africa/Johannesburg")

答案2

得分: 0

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;

    }

}
英文:
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&lt;String, String&gt; sortedMap = new LinkedHashMap&lt;&gt;();
Map&lt;String, String&gt; allZoneIdsAndItsOffSet = getAllZoneIdsAndItsOffSet();
//sort map by key
if (SORT_BY_REGION) {
allZoneIdsAndItsOffSet.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEachOrdered(e -&gt; sortedMap.put(e.getKey(), e.getValue()));
} else {
// sort by value, descending order
allZoneIdsAndItsOffSet.entrySet().stream()
.sorted(Map.Entry.&lt;String, String&gt;comparingByValue().reversed())
.forEachOrdered(e -&gt; sortedMap.put(e.getKey(), e.getValue()));
}
// print map
sortedMap.forEach((k, v) -&gt;
{
String out = String.format(&quot;%35s (UTC%s) %n&quot;, k, v);
System.out.printf(out);
});
System.out.println(&quot;\nTotal Zone IDs &quot; + sortedMap.size());
}
private static Map&lt;String, String&gt; getAllZoneIdsAndItsOffSet() {
Map&lt;String, String&gt; result = new HashMap&lt;&gt;();
LocalDateTime localDateTime = LocalDateTime.now();
for (String zoneId : ZoneId.getAvailableZoneIds()) {
ZoneId id = ZoneId.of(zoneId);
// LocalDateTime -&gt; ZonedDateTime
ZonedDateTime zonedDateTime = localDateTime.atZone(id);
// ZonedDateTime -&gt; ZoneOffset
ZoneOffset zoneOffset = zonedDateTime.getOffset();
//replace Z to +00:00
String offset = zoneOffset.getId().replaceAll(&quot;Z&quot;, &quot;+00:00&quot;);
result.put(id.toString(), offset);
}
return result;
}

}

huangapple
  • 本文由 发表于 2020年8月3日 16:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63226340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定