How to get the TimeZone ID for the specified ISO country code?

I have a requirement when I need to convert a time zone from UTC to a specific time zone and vice versa, given daylight saving. I use the java.util.TimeZone class for this. Now the problem is that there are several hundred identifiers for the time zone that cannot be displayed to the user.

Now that we are working, we decided to first look at the list of countries and indicate the time zones for the selected country. I can not get TimeZone for the ISO country code .

Here is the code I'm currently using to convert timezones,

 Timestamp convertedTime = null; try{ System.out.println("timezone: "+timeZone +", timestamp: "+timeStamp); Locale locale = Locale.ENGLISH; TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault(); System.out.println("Source timezone: "+destTimeZone); DateFormat formatter = DateFormat.getDateTimeInstance( DateFormat.DEFAULT, DateFormat.DEFAULT, locale); formatter.setTimeZone(destTimeZone); Date date = new Date(timeStamp.getTime()); System.out.println(formatter.format(date)); convertedTime = new Timestamp(date.getTime()); /*long sixMonths = 150L * 24 * 3600 * 1000; Date inSixMonths = new Date(timeStamp.getTime() + sixMonths); System.out.println("After 6 months: "+formatter.format(inSixMonths)); 

I need to find out the time zone identifier that will be used in the above code for this ISO country code.


Update: I tried a lot of things, and the code below leads me to a list of time zones with 148 entries (which are still large). Can anyone help me cut it. Or suggest another way to either shorten the list of time zones or get time intervals for a country,

the code:

 public class TimeZones { private static final String TIMEZONE_ID_PREFIXES = "^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*"; private List<TimeZone> timeZones = null; public List<TimeZone> getTimeZones() { if (timeZones == null) { initTimeZones(); } return timeZones; } private void initTimeZones() { timeZones = new ArrayList<TimeZone>(); final String[] timeZoneIds = TimeZone.getAvailableIDs(); for (final String id : timeZoneIds) { if (id.matches(TIMEZONE_ID_PREFIXES)) { timeZones.add(TimeZone.getTimeZone(id)); } } Collections.sort(timeZones, new Comparator<TimeZone>() { public int compare(final TimeZone a, final TimeZone b) { return a.getID().compareTo(b.getID()); } }); } 
+4
source share
3 answers

He was able to make everything work. I created my own database table with all the time zones that appear on Windows and their corresponding TimeZone identifiers. The conversion is performed using the java.util.TimeZone class.

Thanks to Namal and Frank for your submissions.

0
source

I think the ICU4J package will help you.

+3
source

You can shorten your list with hasSameRules () ... this should reduce the selection to 50:

iterate through -> equal timezone files -> select the most recognizable

The list of countries should have about 200 entries with a number of uninteresting ones, such as Gibraltar or St. Martin ... I do not like this idea

0
source

All Articles