我如何获得夏令时开始和结束的日期? [英] How do I get the date of the start and end of daylight savings?

查看:1326
本文介绍了我如何获得夏令时开始和结束的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得在Android的夏令时过渡日期。即日期是夏令就用于特定时区结束。

I am trying to get the transition date for daylight savings in Android. That is the date that daylight savings start and ends for a specific timezone.

我该怎么办呢?

推荐答案

有没有办法直接做到这一点,但你可以使用TimeZone.isDaylightTime(日期),如:

there's no way to do that directly, but you could use TimeZone.isDaylightTime(Date), as in:

时区TZ = TimeZone.getTimeZone(美国/丹佛);
日期日期=新的GregorianCalendar(2010,10,15).getTime();
如果(tz.isDaylightTime(日期)){
// DST生效code
   }
其他{
// DST不​​起作用code
   }

TimeZone tz = TimeZone.getTimeZone ("America/Denver") ; Date date = new GregorianCalendar (2010, 10, 15).getTime () ; if (tz.isDaylightTime (date)) { // dst in effect code } else { // dst not in effect code }

当然,要获得的开始日期(如果那是真的你所需要的),你需要将所有的天迭代在某一年,并检查每一个...这将是pretty效率低下。此外,如上面的回答指出,据我所知,夏令时信息被编译成java.util中的图书馆等,如果给定的直辖市改变它什么时候DST开始/结束,你会得到不正确的结果的法律。

Of course, to get the start date (if that is really what you need), you need to iterate over all days in a given year and check each one...which would be pretty inefficient. Also, as noted in the answer above, as far as I know, the daylight savings time info is compiled into java.util library and so if a given municipality changes its laws about when dst starts/ends you'll get incorrect results.

此外,它出现在Android上的java.util.TimeZone中执行被打破(至少在2.2)。例如

Also, it appears that the java.util.TimeZone implementation in Android is broken (at least in 2.2). For instance

时区丹佛= TimeZone.getTimeZone(美国/丹佛);
时区凤凰= TimeZone.getTimeZone(美国/凤凰);
 == TRUE; //它不应该

TimeZone denver = TimeZone.getTimeZone ("America/Denver") ; TimeZone phoenix = TimeZone.getTimeZone ("America/Phoenix") ; == true ; // which it shouldn't

在Eclipse中的emulate下运行,denver.hasSameRules(凤凰城)返回true,它应该返回false:都是GMT-7:00,但凤凰不使用DST。我没有访问任何Android设备来测试这是否仅仅是一个模拟器与否的问题。其实,我开始寻找这个归档文件的答案,这丹佛==凤问题,碰到您的帖子所以想我会回答。

Running under the emulate in Eclipse, denver.hasSameRules (phoenix) returns true, and it should return false: both are GMT-7:00 but Phoenix doesn't use dst. I don't have access to any Android devices to test whether this is just a problem with the emulator or not. I actually started searching this archive for an answer to this denver == phoenix problem and came across your post so thought I'd answer.

这篇关于我如何获得夏令时开始和结束的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆