英文:
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<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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论