Get timezone of area with country code in Java
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());
How to get Country Code from timezone ID in java?
I think the best alternative is to get these IANA's files:
- https://github.com/eggert/tz/blob/main/zone.tab
- https://github.com/eggert/tz/blob/main/zone1970.tab
Those files have entries in the format below:
#codes coordinates TZ comments
AD +4230+00131 Europe/Andorra
AE,OM +2518+05518 Asia/Dubai
AF +3431+06912 Asia/Kabul
If I'm not wrong, zone1970.tab
is newer and covers cases where a timezone is used by more than one country, but you can check both and see which one solves your problem.
And there's also the iso3166.tab file that maps countries codes to their respective names.
I'm not sure if Java has an API that directly reads those files, but looking at their formats, it seems to be straighforward to read them and find the info you want.
**Quick note**: there's no need to call `Calendar.getInstance().getTimeZone().toZoneId()`.If you want your JVM's default timezone as a ZoneId
object, just call ZoneId.systemDefault()
. If you want the zone's ID as a String
(such as Europe/Berlin
or America/New_York
), just call ZoneId.systemDefault().getId()
.
Need timezone based on the ISO country code and city in Java
Rough way I found to achieve is -
private static String getSourceLocalTimeZone(String countryCode, String city, String sourceLocalTimeZone) {
String[] timeZones = com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode);
for (String timeZone : timeZones) {
String cityFromTimeZone = null;
String[] value = timeZone.split("/");
if (value != null && value.length > 0) {
cityFromTimeZone = value[value.length - 1].replace("_", " ");
}
if (city!=null && city.matches("(.*)" + cityFromTimeZone + "(.*)")) {
sourceLocalTimeZone = timeZone;
break;
}
}
if (sourceLocalTimeZone == null || (sourceLocalTimeZone.isEmpty())) {
if(timeZones.length>0)
sourceLocalTimeZone = timeZones[0];
}
return sourceLocalTimeZone;
}
Dependency -
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>4.6</version>
</dependency>
How to get TimeZone id for specified ISO country code?
Was able to get things working. I have created own database table with all time-zones as appearing in windows OS and their corresponding TimeZone IDs. Conversion is done using java.util.TimeZone class.
Thanks Namal and Frank for your inputs.
Convert country-state code to time zone in Java
This isn't possible - in any language. Many US states have more than one time zone, depending on which part of the state you are referring to:
From Wikipedia:
You can clearly see on this map, many states where the time zone boundary does not follow the state boundary.
Related Topics
How to Handle Errors in Spring-Webflux
How to Mock Resultset and Populate It Using Mockito in Java
How to Check If an Integer Is in a Given Range
Typecasting an Object from Parent Class to Child
How to Encrypt a String/Stream With Bouncycastle Pgp Without Starting With a File
How to Pass Json Object as a Pathvariable to Spring Controller
Javafx Column in Tableview Auto Fit Size
Prevent Empty Values on String Split
How to Pass Intent in Adapter Class
How to Extract Ip Address in Spring MVC Controller Get Call
How to Get Image Height and Width Using Java
Passing Data into HTML from Java Via Spring
Geting Http Status 400 - Required Multipartfile Parameter 'File' Is Not Present in Spring
Regex, Remove Whitespace and All Other Characters
How to Force Java Server to Accept Only Tls 1.2 and Reject Tls 1.0 and Tls 1.1 Connections
Best Way to Run Jar Application With Arguments from Env Variables