Kov*_*mre 5 java time timezone jodatime dst
只是为了验证这一点:我有这种跛脚和脑死亡的方法来计算我当前位置的时区偏移量.我想知道当Day Light Saving时间问题时我是否需要调整它(目前我的位置有冬季时间,CET时区,因此很难验证).
// The local time zone's offset
private int getLocalOffset() {
DateTimeZone defaultZone = DateTimeZone.getDefault();
return defaultZone.getOffset(null) / 1000 / 60 / 60;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何提示.
时区和夏令时是一场噩梦.你当然不应该自己承担这项任务.让Joda-Time做繁重的工作.
查看类似问题的答案,使用Joda时间获取给定日期和时区的UTC偏移量.DateTimeZone类提供了getOffset()方法.
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 );
Run Code Online (Sandbox Code Playgroud)
在2013-11-20T01:03:56.464-08:00运行时...
millisecondOffsetToAddToUtcToGetLocalTime: -28800000
millisecondOffsetToAddToUtcToGetLocalTime in hours: -8.0
Run Code Online (Sandbox Code Playgroud)
重要事项该数字格式对于偏移量-8.0不正确.必须是:
-08:00 用冒号和两位数(用前导零填充).-08 领先零.通常,Joda 时间会自行处理 DST,因此您不必担心。但是,我注意到您正在传递null给 getOffset()。鉴于时区偏移量取决于日期,您确实应该传递计算偏移量的日期/时间,否则您将得到错误的结果。
另外,正如我之前的评论中提到的:请注意,某些时区的偏移量不是整数小时。例如,印度的时间为 GMT +5:30
| 归档时间: |
|
| 查看次数: |
9915 次 |
| 最近记录: |