How to get ZoneId of SAST?

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

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。
因此,您应该使用:

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

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

  1. ZoneId.of("Africa/Johannesburg")

答案2

得分: 0

DisplayZoneAndOffSet.java

  1. package com.mkyong;
  2. import java.time.LocalDateTime;
  3. import java.time.ZoneId;
  4. import java.time.ZoneOffset;
  5. import java.time.ZonedDateTime;
  6. import java.util.HashMap;
  7. import java.util.LinkedHashMap;
  8. import java.util.Map;
  9. public class DisplayZoneAndOffSet {
  10. public static final boolean SORT_BY_REGION = false;
  11. public static void main(String[] argv) {
  12. Map<String, String> sortedMap = new LinkedHashMap<>();
  13. Map<String, String> allZoneIdsAndItsOffSet = getAllZoneIdsAndItsOffSet();
  14. // sort map by key
  15. if (SORT_BY_REGION) {
  16. allZoneIdsAndItsOffSet.entrySet().stream()
  17. .sorted(Map.Entry.comparingByKey())
  18. .forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
  19. } else {
  20. // sort by value, descending order
  21. allZoneIdsAndItsOffSet.entrySet().stream()
  22. .sorted(Map.Entry.<String, String>comparingByValue().reversed())
  23. .forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
  24. }
  25. // print map
  26. sortedMap.forEach((k, v) ->
  27. {
  28. String out = String.format("%35s (UTC%s) %n", k, v);
  29. System.out.printf(out);
  30. });
  31. System.out.println("\nTotal Zone IDs " + sortedMap.size());
  32. }
  33. private static Map<String, String> getAllZoneIdsAndItsOffSet() {
  34. Map<String, String> result = new HashMap<>();
  35. LocalDateTime localDateTime = LocalDateTime.now();
  36. for (String zoneId : ZoneId.getAvailableZoneIds()) {
  37. ZoneId id = ZoneId.of(zoneId);
  38. // LocalDateTime -> ZonedDateTime
  39. ZonedDateTime zonedDateTime = localDateTime.atZone(id);
  40. // ZonedDateTime -> ZoneOffset
  41. ZoneOffset zoneOffset = zonedDateTime.getOffset();
  42. // replace Z to +00:00
  43. String offset = zoneOffset.getId().replaceAll("Z", "+00:00");
  44. result.put(id.toString(), offset);
  45. }
  46. return result;
  47. }
  48. }
英文:
  1. DisplayZoneAndOffSet.java
  2. package com.mkyong;
  3. import java.time.LocalDateTime;
  4. import java.time.ZoneId;
  5. import java.time.ZoneOffset;
  6. import java.time.ZonedDateTime;
  7. import java.util.HashMap;
  8. import java.util.LinkedHashMap;
  9. import java.util.Map;
  10. public class DisplayZoneAndOffSet {
  11. public static final boolean SORT_BY_REGION = false;
  12. public static void main(String[] argv) {
  13. Map&lt;String, String&gt; sortedMap = new LinkedHashMap&lt;&gt;();
  14. Map&lt;String, String&gt; allZoneIdsAndItsOffSet = getAllZoneIdsAndItsOffSet();
  15. //sort map by key
  16. if (SORT_BY_REGION) {
  17. allZoneIdsAndItsOffSet.entrySet().stream()
  18. .sorted(Map.Entry.comparingByKey())
  19. .forEachOrdered(e -&gt; sortedMap.put(e.getKey(), e.getValue()));
  20. } else {
  21. // sort by value, descending order
  22. allZoneIdsAndItsOffSet.entrySet().stream()
  23. .sorted(Map.Entry.&lt;String, String&gt;comparingByValue().reversed())
  24. .forEachOrdered(e -&gt; sortedMap.put(e.getKey(), e.getValue()));
  25. }
  26. // print map
  27. sortedMap.forEach((k, v) -&gt;
  28. {
  29. String out = String.format(&quot;%35s (UTC%s) %n&quot;, k, v);
  30. System.out.printf(out);
  31. });
  32. System.out.println(&quot;\nTotal Zone IDs &quot; + sortedMap.size());
  33. }
  34. private static Map&lt;String, String&gt; getAllZoneIdsAndItsOffSet() {
  35. Map&lt;String, String&gt; result = new HashMap&lt;&gt;();
  36. LocalDateTime localDateTime = LocalDateTime.now();
  37. for (String zoneId : ZoneId.getAvailableZoneIds()) {
  38. ZoneId id = ZoneId.of(zoneId);
  39. // LocalDateTime -&gt; ZonedDateTime
  40. ZonedDateTime zonedDateTime = localDateTime.atZone(id);
  41. // ZonedDateTime -&gt; ZoneOffset
  42. ZoneOffset zoneOffset = zonedDateTime.getOffset();
  43. //replace Z to +00:00
  44. String offset = zoneOffset.getId().replaceAll(&quot;Z&quot;, &quot;+00:00&quot;);
  45. result.put(id.toString(), offset);
  46. }
  47. return result;
  48. }

}

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:

确定