jodatime如何知道是否夏令时是 [英] jodatime how to know if daylight savings is on
问题描述
我有需要时区的API。对于如。如果我是在加州,我需要通过-7到它时,夏令时是在(美国加州,PDT是GMT - 7)-8到它时,夏令时是关闭的。但我无法弄清楚知道是否在当前日期的方式,实行夏令时开启或关闭。
I have an API that needs the timezone. For eg. if I am in california, I need to pass -7 to it when daylight savings is on (California , PDT is GMT - 7) and -8 to it when daylight saving is off. But I am not able to figure out a way of knowing whether on the current date, daylight saving is on or off.
Date date1 = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
double[] coords = db.getCoords(id1);
double latitude = coords[0];
double longitude = coords[1];
double timezone = -7; /* For Pacific daylight time (GMT - 7)*/
ArrayList<String> Times = Class.foo(cal, latitude,
longitude, timezone);
我已经安装了JodaTime,甚至还有我能不能找到一种方法。如果本地Java或jodatime请建议,要么有这样做的方式。
I have installed JodaTime and even there I cannot find a way. Please suggest if native java or jodatime, either have a way of doing this.
推荐答案
当你创建一个的DateTime
与JodaTime,你并不需要传递一个偏移量。相反,通过的时区。这将需要确定正确的偏移量,包括考虑对DST的照顾。
When you create a DateTime
with JodaTime, you don't need to pass an offset. Instead, pass the time zone. It will take care of determining the correct offset, including consideration for DST.
// First get a DateTimeZone using the zone name
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles");
// Then get the current time in that zone.
DateTime dt = new DateTime(zone);
// Or if you prefer to be more explicit, this syntax is equivalent.
DateTime dt = DateTime.now(zone);
更新
我仍然不知道到底是什么你问,但也许你正在寻找的其中之一:
I'm still not sure exactly what you are asking, but perhaps you are looking for one of these:
// To get the current Pacific Time offset
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles");
int currentOffsetMilliseconds = zone.getOffset(Instant.now());
int currentOffsetHours = currentOffsetMilliseconds / (60 * 60 * 1000);
// To just determine if it is currently DST in Pacific Time or not.
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles");
boolean isStandardOffset = zone.isStandardOffset(Instant.now());
boolean isDaylightOfset = !isStandardOffset;
这篇关于jodatime如何知道是否夏令时是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!