Get timezone of area with country code in Java
Asked Answered
R

3

5

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", ...

Redaredact answered 23/5, 2018 at 22:5 Comment(3)
All timezones for a country?Calisaya
yes a list if possibleRedaredact
It’s not built into Java. You may be able to find the information in some database somewhere.Gatian
N
4

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());
Nonce answered 24/5, 2018 at 2:22 Comment(7)
This will give you deprecated time zones like US/Alaska, not current ones like America/Adak or America/Los_Angeles. I’m not saying it’s not good enough, just think twice. It also won’t work for other country codes.Gatian
I totally agree with you. Can you suggest any other better way of doing this?Nonce
Sorry, not really, Ravindra Ranwala. One may of course try parsing List of tz database time zones and use the country codes in the leftmost column of the table.Gatian
Have you tried this approach some other countries besides US? US is a simple example, but what about "AU" or some others that returns empty list? SO, is there any solution for this?Evadnee
Unfortunately, I didn't.Nonce
Any solution for that? I would appreciated your suggestions.Evadnee
I'll check it and let you know.Nonce
J
8

The builtin Java classes don't offer this, but ICU's TimeZone class does, and TimeZone.getAvailableIDs("US") provides the correct answer.

Javanese answered 29/5, 2019 at 17:3 Comment(0)
N
4

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());
Nonce answered 24/5, 2018 at 2:22 Comment(7)
This will give you deprecated time zones like US/Alaska, not current ones like America/Adak or America/Los_Angeles. I’m not saying it’s not good enough, just think twice. It also won’t work for other country codes.Gatian
I totally agree with you. Can you suggest any other better way of doing this?Nonce
Sorry, not really, Ravindra Ranwala. One may of course try parsing List of tz database time zones and use the country codes in the leftmost column of the table.Gatian
Have you tried this approach some other countries besides US? US is a simple example, but what about "AU" or some others that returns empty list? SO, is there any solution for this?Evadnee
Unfortunately, I didn't.Nonce
Any solution for that? I would appreciated your suggestions.Evadnee
I'll check it and let you know.Nonce
M
2

If I'm understanding correctly, it looks like you just want a list of timezones from a given country. This site has a list of all the countries that have their own code:

https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/

Looking at the API for TimeZones shows that there's no way to grab a list of timezones directly through TimeZone.getTimeZone(). So instead, you probably want to loop through them and just see which ones start with the country name and add them to a list, like so:

public static List<String> GetZones(String country) {
    List<String> zones = new ArrayList<>();

    for (String i : TimeZone.getAvailableIDs()) {
        if (i.startsWith(country)) {
            zones.add(i);
        }
    }
    return zones;

}
Margiemargin answered 23/5, 2018 at 23:31 Comment(1)
Timezones do not contain country name. They contain continent and city nameWilson

© 2022 - 2024 — McMap. All rights reserved.