Search code examples
javajava-8timezonegoogle-api-java-clientjava-calendar

Get timezone of area with country code in 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"));

But its giving output as

sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

I am expecting List of "America/Los_Angeles", ...


Solution

  • 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());