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);
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()); } }); }
source share