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

Answer from arnt on Stack Overflow
🌐
Objects
helpdesk.objects.com.au › home › java
Can I find all available timezones for a country? | web development helpdesk
January 8, 2023 - The ICU4J package provides a method for returning the available timezones for a given country code. The following code will return a Map containing all available TimeZone's for each country, keyed on country code. [sourcecode lang="java"] public static Map getAvailableTimeZones() { Map ...
🌐
DZone
dzone.com › coding › java › how to get a country’s time zone in java
How to Get a Country’s Time Zone in Java
February 17, 2021 - Once the process is complete, the returned response will indicate the time zone and current time of the country in question, as well as the full country name and ISO codes. In conclusion, we hope this can be a helpful tool as you navigate your international business network. ... Opinions expressed by DZone contributors are their own. Why High-Availability Java Systems Fail Quietly Before They Fail Loudly
🌐
Codereye
codereye.com › 2009 › 07 › getting-all-time-zones-of-country.html
Getting all time zones of a Country in Java - Coder Eye
July 18, 2009 - String[] timeZones = com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode); This example will return all the available time zones in Israel. Note that the input to this function is country code.
🌐
SAP Community
answers.sap.com › questions › 7368553 › get-time-zone-based-on-country-code.html
Solved: Get time Zone based on Country code - SAP Community
May 26, 2010 - > I am looking for getting the time and date of a particular country based on country code in XI. How should that work? Have you thought about Russia? USA? Check java class Calendar and TimeZone: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html ·
🌐
Coderanch
coderanch.com › t › 583127 › java › Timezone-City-State-Province-Country
To know Timezone given City,State/Province,Country (Java in General forum at Coderanch)
Hi Maulin, If you look at the getTimeZone() method of the Calendar class clearly you can see there is a way to get a TimeZone from a given Calendar. The trick is to get an appropriate calendar instance using the getInstance() method of Calendar, passing in a Locale. Unfortunately that'll not ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 14135355 › get-timezone-by-country-region-in-java
Get TimeZone by Country/Region in Java - Stack Overflow
You cannot get a timezone from the http request directly as mentioned here: Is there another way to get a user's time zone from a HttpServletRequest object in Spring MVC? But you can determine it by ip address. Mentioned here: How to programmatically find geo location of incoming http request? You can also use getTimeZoneOffset(). Mentioned here: User needs to display transaction in there local time as they login from different country.
Top answer
1 of 1
5

For a solution covering all thinkable city names this will require a database containing cities and their corresponding time zones. It will be further complicated by the fact that cities with the same name exist, so you may have ambiguous input. For example, by Paris I suppose you intended the capital and largest city of France, Europe, but towns called Paris exist in other places too. I don’t know if a suitable database exists, you may search.

I can get you close, though, with what is built into Java. Time zones have IDs in the form region/city, for example Australia/Sydney and Asia/Dubai. The city used in naming the time zone is the largest populated area of the time zone, so even in the case where a country or state is only one time zone, the city needs not be the capital. But if the city coincides, we can find the zone.

    Set<String> zids = ZoneId.getAvailableZoneIds();

    String[] cityNames = { "Abu Dhabi", "Dubai", "Sydney", "Dhaka", "Paris", "Indianapolis", "São Tomé" };
    for (String cityName : cityNames) {
        String tzCityName = Normalizer.normalize(cityName, Normalizer.Form.NFKD)
                .replaceAll("[^\\p{ASCII}-_ ]", "")
                .replace(' ', '_');
        List<String> possibleTimeZones = zids.stream()
                .filter(zid -> zid.endsWith("/" + tzCityName))
                .collect(Collectors.toList());
        System.out.format("%-12s %s%n", cityName, possibleTimeZones);
    }

The output from this snippet is:

Abu Dhabi    []
Dubai        [Asia/Dubai]
Sydney       [Australia/Sydney]
Dhaka        [Asia/Dhaka]
Paris        [Europe/Paris]
Indianapolis [America/Indianapolis, America/Indiana/Indianapolis]
São Tomé     [Africa/Sao_Tome]

You will notice, though, that it didn’t find any time zone for Abu Dhabi because although the capital of the United Arab Emirates, it is not the largest city; Dubai is. You will notice too that two time zones were found for Indianapolis. The former is just an alias for the latter, though.

The city names used in the time zone database are the English names (when they exist) stripped of any accents. When a name is in two or three words, they are separated by underscores rather than spaces. So São Tomé becomes Sao_Tome. Therefore in the code I am performing this conversion. The way to strip off the accents was taken from another Stack Overflow answer, link below.

Links

  • List of tz database time zones on Wikipedia
  • Answer by Erick Robertson to Is there a way to get rid of accents and convert a whole string to regular letters?
🌐
Coderanch
coderanch.com › t › 469915 › java › Solution-displaying-time-inputting-country
Solution for displaying time by inputting country code (Beginning Java forum at Coderanch)
November 6, 2009 - Hi, All you need is: - current time - list of all available TimeZones ( String[] TimeZone.getAvailableIDs() ) - call in a loop df.setTimeZone(timezone[idx]) and print it out to the console This should work. Regarding the Locale. This is just a visual presentation of the time and nothing else (no GMT+/- or DST). Regards, Rok ... thanks. but i need to input the standard iso country code and then display the time accordingly.
🌐
Timezone Converter
timezoneconverter.com › cgi-bin › zonehelp.tzc
Time Zones by Country
Time Zone Tools Time Zone Converter Time Zone Information Event Planning World Time What's My Time Zone? Time Zones by Country Forex Market Hours
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › TimeZone.html
TimeZone (Java Platform SE 8 )
1 week ago - However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them. ... Sole constructor. (For invocation by subclass constructors, typically implicit.) public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) Gets the time zone offset, for current date, modified in case of daylight savings. This is the offset to add to UTC to get local time. This method returns a historically correct offset if an underlying TimeZone implementation subclass supports historical Daylight Saving Time schedule and GMT offset changes.
🌐
Codereye
codereye.com › 2009 › 05 › getting-time-zone-list-in-java.html
Getting Time Zone list in Java - Coder Eye
I also wrote another post of how to get languages list in Java. Getting Time zones in Java is quite similar, but there are some differences. Like Locale function: Locale.getISOCountries() and Locale.getISOLanguages(), TimeZone class also has a method for getting all time zone IDs: ...
🌐
SAP Community
community.sap.com › t5 › technology-q-a › get-time-zone-based-on-country-code-in-sap-po-mapping › qaq-p › 12222121
Solved: Get time Zone based on Country code in SAP PO mapp... - SAP Community
October 27, 2020 - Help others by sharing your knowledge. Answer · Request clarification before answering. Comment ... You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in. ... import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; static String getTimeBasedOnZone(String countryCode) { String timeZone=null; LocalDateTime dt = LocalDateTime.now(); ZoneId zone = ZoneId.of(countryCode); DateTimeFormatter offsetFormatter = DateTimeFormatter.ofPattern("xxx"); ZonedDateTime zdt = dt.atZone(zone); ZoneOffset offset = zdt.getOffset(); timeZone = String.format("%s %s", zone, offsetFormatter.format(offset)); return timeZone; }