Java org.joda.time | 夏令时 (DST) 和本地时区偏移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19978570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:02:23  来源:igfitidea点击:

org.joda.time | Day Light Saving time (DST) and local time zone offset

javatimetimezonejodatimedst

提问by Kovács Imre

just to verify this: I have this lame and brain dead method to calculate the time zone offset for my current location. I wonder if I need to adjust it when Day Light Saving time comes into question (currently we have Winter Time at my location, CET time zone, so it's hard to verify).

只是为了验证这一点:我有这个蹩脚和脑死亡的方法来计算我当前位置的时区偏移量。我想知道当夏令时出现问题时是否需要调整它(目前我们在我的位置有冬季时间,CET 时区,因此很难验证)。

// The local time zone's offset
private  int getLocalOffset() {
    DateTimeZone defaultZone = DateTimeZone.getDefault();
    return defaultZone.getOffset(null) / 1000 / 60 / 60;
}

Thanks for any hint.

感谢您的任何提示。

采纳答案by LordOfThePigs

Normally, Joda time will take care of DST by itself, so you don't have to worry about it. However, I notice that you are passing nullto getOffset(). Given that the time zone offset depends on the date, you really should be passing the date/time at which you are calculating the offset, or you're going to get wrong results.

通常情况下,Joda 时间会自行处理 DST,因此您不必担心。但是,我注意到您正在传递null给 getOffset()。鉴于时区偏移量取决于日期,您确实应该传递计算偏移量的日期/时间,否则会得到错误的结果。

Also as mentionned in my previous comment: Be aware that some timezones have an offset that isn't a whole number of hours. India for example is at GMT +5:30

同样在我之前的评论中提到:请注意,某些时区的偏移量不是整数小时。例如,印度在格林威治标准时间 +5:30

回答by ishaaq

Yes, that's fine. To verify that it is correct - instead of passing null pass in a DateTimeobject to DateTimeZone.getOffset- set the datetime to sometime in summer when you know DST is in effect - you should see the offset value change.

是的,没关系。要验证它是否正确 - 而不是在DateTime对象中传递 null 传递到DateTimeZone.getOffset- 将日期时间设置为夏季的某个时间,当您知道 DST 生效时 - 您应该看到偏移值发生变化。

回答by Basil Bourque

Time zones and Daylight Saving Time are a nightmare. You certainly shouldn't take on this task yourself. Let Joda-Time do the heavy lifting.

时区和夏令时是一场噩梦。你当然不应该自己承担这个任务。让 Joda-Time 来完成繁重的工作。

See this answerto similar question, Using Joda time to get UTC offset for a given date and timezone. The class DateTimeZoneoffers a getOffset()method.

请参阅类似问题的答案使用 Joda 时间获取给定日期和时区的 UTC 偏移量DateTimeZone类提供了一个getOffset()方法。

Example source code in Joda-Time 2.3 in Java 7…

Java 7 中 Joda-Time 2.3 中的示例源代码......

// ? 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles");

org.joda.time.DateTime now = new org.joda.time.DateTime(californiaTimeZone);
int millisecondOffsetToAddToUtcToGetLocalTime = californiaTimeZone.getOffset( now );

System.out.println( "millisecondOffsetToAddToUtcToGetLocalTime: " + millisecondOffsetToAddToUtcToGetLocalTime );

// Note the casting to doubles to avoid integer truncation. Time zone offsets are NOT always whole hours.
System.out.println( "Offset in decimal hours: " + (double)millisecondOffsetToAddToUtcToGetLocalTime / 1000d / 60d / 60d );

When run at 2013-11-20T01:03:56.464-08:00…

在 2013-11-20T01:03:56.464-08:00 运行时…

millisecondOffsetToAddToUtcToGetLocalTime: -28800000
millisecondOffsetToAddToUtcToGetLocalTime in hours: -8.0

IMPORTANTThat number format -8.0is incorrectfor an offset. Must be either:

重要信息对于偏移量,该数字格式-8.0不正确。必须是:

  • -08:00with the colon and double digits (padded with leading zero).
  • -08with leading zero.
  • -08:00带有冒号和两位数(用前导零填充)。
  • -08带前导零。