jodatime how to know if daylight savings is on

谁都会走 提交于 2019-11-29 09:45:00

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);

UPDATE

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!