简体   繁体   English

Java获取特定时间的所有时区

[英]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. 因此,例如,如果我传递一个上午7点钟的日历对象,则该方法应返回当前是7点钟的世界中的所有区域。

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). 我可以像这样使用这种方法(在本例中,我是在上午7点的一个小时内通过)。 Right now it is 9 am eastern standard time, so the results should be all time zones in which it is currently 7 am. 现在是东部标准时间上午9点,因此结果应该是当前现在是上午7点的所有时区。

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)? 这是获取当前为特定时间的所有时区(在本示例中为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. 从这些方法中提取的数据可立即使用,无需保存,因此,如果DST在几个月后发生更改,则没关系,只要它在我检查时能给我正确的时间即可。

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. 话虽如此,当您必须记住您在通话的那一刻就获得了小时,而在用户阅读屏幕时却不必在几秒钟之后,小时的顶部是微秒窗口,而leap秒则是leap秒。 But I don't think that should be an issue. 但我认为这不应该成为问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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