在Java中获取夏时制转换日期时间区域
问题描述:
我想知道在Java中使用最简单的方法来获取夏时制时间将改变的日期列表。在Java中获取夏时制转换日期时间区域
一个相当不礼貌的方法就是迭代一堆数年的时间,并对TimeZone.inDaylightTime()进行测试。这会起作用,我并不担心效率,因为这只需要在每次启动应用程序时运行,但我不知道是否有更简单的方法。
如果你想知道为什么我这样做,这是因为我有一个JavaScript应用程序需要处理包含UTC时间戳的第三方数据。我想要一种可靠的方式在客户端从GMT转换为EST。请参阅Javascript -- Unix Time to Specific Time Zone我已经写了一些JavaScript来完成它,但我想从服务器获取精确的转换日期。
答
Joda Time(与以往一样)由于采用了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();
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;
}
}
}
输出:
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 ...
与Java 8,你可以使用ZoneRules
其nextTransition
和previousTransition
方法相同的信息。
看到这个问题:http://*.com/questions/581581/find-dst-transition-timestamp-with-java-util-timezone – 2009-09-19 21:04:04