Java中如何通过国家代码获取对应时区信息?
Absolutely! The issue you're running into is that TimeZone.getTimeZone("US") expects a time zone ID (like America/Los_Angeles) or a time zone alias (like EST), not a country code. That's why it falls back to GMT by default. But yes, you can definitely get a list of time zones for a given country using Java code—here are a few reliable approaches:
Method 1: Use Java 8+ java.time API (Recommended)
Starting with Java 8, the modern java.time package makes this task cleaner. We can iterate over all available time zones and filter those associated with your target country by checking the time zone's display name against the country's locale.
Here's a working example:
import java.time.ZoneId; import java.util.Locale; import java.util.Set; import java.util.stream.Collectors; public class CountryTimeZones { public static void main(String[] args) { String countryCode = "US"; Locale targetLocale = new Locale("", countryCode); // Filter time zones linked to the target country Set<String> countryTimeZones = ZoneId.getAvailableZoneIds().stream() .filter(zoneId -> { ZoneId zone = ZoneId.of(zoneId); // Check if the short display name includes the country code String shortDisplayName = zone.getDisplayName(false, ZoneId.SHORT, targetLocale); return shortDisplayName.contains(countryCode); }) .collect(Collectors.toSet()); // Print results System.out.println("Time zones for country code " + countryCode + ":"); countryTimeZones.forEach(System.out::println); } }
This will output all US time zones like America/Los_Angeles, America/New_York, and America/Chicago.
Method 2: Legacy Java 7 and Below
If you're working with an older Java version, you can use the legacy java.util.TimeZone class to achieve the same result:
import java.util.ArrayList; import java.util.List; import java.util.TimeZone; import java.util.Locale; public class LegacyCountryTimeZones { public static void main(String[] args) { String countryCode = "US"; Locale targetLocale = new Locale("", countryCode); List<String> countryTimeZones = new ArrayList<>(); // Iterate over all available time zone IDs String[] allTimeZones = TimeZone.getAvailableIDs(); for (String zoneId : allTimeZones) { TimeZone tz = TimeZone.getTimeZone(zoneId); // Check display name for country code match String displayName = tz.getDisplayName(false, TimeZone.SHORT, targetLocale); if (displayName.contains(countryCode)) { countryTimeZones.add(zoneId); } } // Print results System.out.println("Time zones for country code " + countryCode + ":"); for (String zone : countryTimeZones) { System.out.println(zone); } } }
Method 3: Use a Third-Party Library (Most Reliable)
For the most accurate results (since manual filtering can have edge cases), use the ICU4J library, which maintains direct mappings between countries and their time zones.
First, add the Maven dependency to your project:
<dependency> <groupId>com.ibm.icu</groupId> <artifactId>icu4j</artifactId> <version>73.2</version> <!-- Use the latest available version --> </dependency>
Then use this simple code to get the time zones:
import com.ibm.icu.util.TimeZone; import java.util.Set; public class ICUCountryTimeZones { public static void main(String[] args) { String countryCode = "US"; // Directly get all time zone IDs for the country Set<String> timeZones = TimeZone.getAvailableIDs(countryCode); // Print results System.out.println("Time zones for country code " + countryCode + ":"); timeZones.forEach(System.out::println); } }
This method avoids guesswork with display names and gives you the exact list of time zones linked to the country.
To recap: The reason TimeZone.getTimeZone("US") returns GMT is because the method doesn't recognize country codes as valid time zone identifiers—it only accepts actual time zone IDs or aliases. The approaches above will get you the list of time zones you need.
内容的提问来源于stack exchange,提问作者Manvi

