获取Java中的时区的夏令时转换日期 [英] Get Daylight Saving Transition Dates For Time Zones in Java

查看:270
本文介绍了获取Java中的时区的夏令时转换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道在Java中最简单的方法,以获取未来夏令时间将更改的日期列表。这样做的方法是简单地迭代一年多的时间,测试它们与TimeZone.inDaylightTime()。这将会起作用,我并不担心效率,因为这只需要运行每次我的应用程序启动,但我不知道是否有一个更简单的方法。



如果你想知道为什么我这样做,这是因为我有一个需要处理包含UTC时间戳记的第三方数据的JavaScript应用程序。我想要一个可靠的方式从客户端的GMT转换为EST。我写过的 Javascript - Unix时间到特定时区一些javascript将会做,但我想从服务器获取精确的转换日期。

解决方案

Joda时间(一如以往)由于 DateTimeZone.nextTransition 方法。例如:

  import org.joda.time。*; 
import org.joda.time.format。*;

public class Test
{
public static void main(String [] args)
{
DateTimeZone zone = DateTimeZone.forID(Europe / London );
DateTimeFormatter format = DateTimeFormat.mediumDateTime();

long current = System.currentTimeMillis(); (int i = 0; i <100; i ++)
{
long next = zone.nextTransition(current);

if(current == next)
{
break;
}
System.out.println(format.print(next)+Into DST?
+!zone.isStandardOffset(next));
current = next;
}
}
}

输出:

 
2009年10月25日01:00:00进入DST?假
2010年3月28日02:00:00进入DST? true
2010年10月31日01:00:00进入DST?假
2011年3月27日02:00:00进入DST? true
2011年10月30日01:00:00进入DST?假
2012年3月25日02:00:00进入DST? true
2012年10月28日01:00:00进入DST? false
2013年3月31日02:00:00进入DST? true
2013年10月27日01:00:00进入DST?假
2014年3月30日02:00:00进入DST? true
2014年10月26日01:00:00进入DST?假
2015年3月29日02:00:00进入DST? true
2015年10月25日01:00:00进入DST? false
...

使用Java 8,您可以使用 ZoneRules 及其 nextTransition previousTransition 方法。


I'd like to know the simplest way, in Java, to get a list of dates in the future where daylight savings time will change.

One rather inellegant way to do this would be to simply iterate over a bunch of years' worth of days, testing them against TimeZone.inDaylightTime(). This will work, and I'm not worried about efficiency since this will only need to run every time my app starts, but I wonder if there's a simpler way.

If you're wondering why I'm doing this, it's because I have a javascript app which needs to handle third-party data containing UTC timestamps. I want a reliable way to translate from GMT to EST on the client side. See Javascript -- Unix Time to Specific Time Zone I've written some javascript which will do it, but I want to get precise transition dates from the server.

解决方案

Joda Time (as ever) makes this really easy due to the DateTimeZone.nextTransition method. For example:

import org.joda.time.*;
import org.joda.time.format.*;

public class Test
{    
    public static void main(String[] args)
    {
        DateTimeZone zone = DateTimeZone.forID("Europe/London");        
        DateTimeFormatter format = DateTimeFormat.mediumDateTime();

        long current = System.currentTimeMillis();
        for (int i=0; i < 100; i++)
        {
            long next = zone.nextTransition(current);
            if (current == next)
            {
                break;
            }
            System.out.println (format.print(next) + " Into DST? " 
                                + !zone.isStandardOffset(next));
            current = next;
        }
    }
}

Output:

25-Oct-2009 01:00:00 Into DST? false
28-Mar-2010 02:00:00 Into DST? true
31-Oct-2010 01:00:00 Into DST? false
27-Mar-2011 02:00:00 Into DST? true
30-Oct-2011 01:00:00 Into DST? false
25-Mar-2012 02:00:00 Into DST? true
28-Oct-2012 01:00:00 Into DST? false
31-Mar-2013 02:00:00 Into DST? true
27-Oct-2013 01:00:00 Into DST? false
30-Mar-2014 02:00:00 Into DST? true
26-Oct-2014 01:00:00 Into DST? false
29-Mar-2015 02:00:00 Into DST? true
25-Oct-2015 01:00:00 Into DST? false
...

With Java 8, you can get the same information using ZoneRules with its nextTransition and previousTransition methods.

这篇关于获取Java中的时区的夏令时转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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