简体   繁体   中英

Java Get All Time Zones For Specific Hour

I want to be able to get and use a list of all time zones that fall within a specific hour at any given time. So for example, if I pass a calendar object with an hour of 7 am, the method should return all the zones in the world in which it is currently 7 am.

Right now I am doing this by traversing all available time zones, check each one against a calendar object, and appending the zone to an array if it falls within the hour.

private List<String> getAllTimeZones(Calendar date) {
    List<String> ret = new ArrayList<String>();
    String[] timezones = TimeZone.getAvailableIDs();
    for(String timezone : timezones) {
        Calendar currentTime = Calendar.getInstance(TimeZone.getTimeZone(timezone));
        Integer zoneHour = currentTime.get(Calendar.HOUR_OF_DAY);
        Integer dateHour = date.get(Calendar.HOUR_OF_DAY);
        if(zoneHour.equals(dateHour)) {
            ret.add(timezone);
        }
    }
    return ret;
}

I can use this method like this (in this example I am passing in an hour of 7 am). Right now it is 9 am eastern standard time, so the results should be all time zones in which it is currently 7 am.

Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, 7);
List<String> zones = getAllTimeZones(date);
for(String zone : zones) {
    System.out.println(zone);
}

This returns this list currently (of course if it were a different time it would be a different list):

America/Boise
   America/Cambridge_Bay
   America/Chihuahua
   America/Dawson_Creek
   America/Denver
   America/Edmonton
   America/Hermosillo
   America/Inuvik
   America/Mazatlan
   America/Ojinaga
   America/Phoenix
   America/Shiprock
   America/Yellowknife
   Canada/Mountain
   Etc/GMT+7
   MST
   MST7MDT
   Mexico/BajaSur
   Navajo
   PNT
   SystemV/MST7
   SystemV/MST7MDT
   US/Arizona
   US/Mountain

Is this a reliable way to get all time zones that are currently a specific time (in this example case 7am)? Because I am comparing against current times, does this take daylight savings into consideration? And last but not least, is there a more efficient way of doing this?

UPDATE: As someone pointed out in an answer below (but is refusing to clarify) is it necessary to calculate the offset since what I am doing is basically setting the time zone a calendar object and getting the object in realtime? The data pulled from these methods is used instantly and does not need to be saved so if the DST changes a few months later it does not matter, as long as its giving me the correct time at the moment I check it.

Yes. It should work. I've used this same approach on other platforms. As long as you're asking for the current time in a time zone, you don't have to worry about daylight savings time, etc.

Having said that, there's the micro-second window at the top of the hour and for leap-seconds when you have to remember that you get the hour at the instant of the call and not necessarily seconds later when the user reads the screen. But I don't think that should be an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM