使用Java中的国家/地区代码获取区域的时区 [英] Get timezone of area with country code in Java
问题描述
我必须传递带有时区信息的消息(jms),例如(America/Los_Angeles),但我只有国家名称和代码.如果可能,请使用Java代码获取时区信息.我在某处读到了这个:
I have to pass a message (jms) with timezone info like (America/Los_Angeles) but I have only country name and code. If it possible get timezone info with Java code. Somewhere I read this:
System.out.println(TimeZone.getTimeZone("US"));
但其给出的输出为
sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
我希望看到"America/Los_Angeles"列表,...
I am expecting List of "America/Los_Angeles", ...
推荐答案
根据文档,getTimeZone
方法将返回指定的TimeZone
或GMT
区域(如果无法理解给定的ID).没有称为 US 的TimeZone ID,因此它提供了GMT区域.如果您真的想获得美国可用的所有时区列表,建议您使用以下内容.
As per the documentation the getTimeZone
method returns the specified TimeZone
, or the GMT
zone if the given ID cannot be understood. There's no TimeZone ID called US hence it gives the GMT zone. If you really want to get all the list of TimeZones available in US, I would suggest you to use the following.
final List<String> timeZonesInUS = Stream.of(TimeZone.getAvailableIDs())
.filter(zoneId -> zoneId.startsWith("US")).collect(Collectors.toList());
这篇关于使用Java中的国家/地区代码获取区域的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!