要求是简单地获得给定时区的当前挂墙时间(包括正确的DST调整)。
在这个问题上似乎有几个问题在盘旋,但我似乎无法找到一个直接的答案(在SO,Joda doco或谷歌搜索)以低摩擦的方式获得了时间。对于给定的输入(当前UTC时间和期望的TZ),似乎是这样的,我应该能够链接来自Joda时间库的几个方法来实现我想要的,但是在所述示例中似乎希望评估+处理偏移/在应用程序代码中进行转换 - 我希望尽可能避免这种情况,并根据其可用的静态TZ规则使用Jodas尽力而为。
对于这个问题,我们假设我不会使用任何其他第三方服务(基于网络或其他二进制文件),正如JDK和JodaTime库中提供的那样。
任何指针赞赏。
更新:
这实际上是对我的失意。我有一个基于请求经度计算的UTC偏移量,虽然这很有效,但您仍然需要区域信息才能获得正确的DST调整。
double aucklandLatitude = 174.730423;
int utcOffset = (int) Math.round((aucklandLatitude * DateTimeConstants.HOURS_PER_DAY) / 360);
System.out.println("Offset: " + utcOffset);
DateTimeZone calculatedDateTimeZone = DateTimeZone.forOffsetHours(utcOffset);
System.out.println("Calculated DTZ: " + calculatedDateTimeZone);
System.out.println("Calculated Date: " + new DateTime(calculatedDateTimeZone));
System.out.println();
DateTimeZone aucklandDateTimeZone = DateTimeZone.forID("Pacific/Auckland");
System.out.println("Auckland DTZ: " + aucklandDateTimeZone);
System.out.println("Auckland Date: " + new DateTime(aucklandDateTimeZone));版画
Offset: 12
Calculated DTZ: +12:00
Calculated Date: 2012-02-08T11:20:04.741+12:00
Auckland DTZ: Pacific/Auckland
Auckland Date: 2012-02-08T12:20:04.803+13:00因此,在晴朗的Auckland, NZ这里,我们在DST时间段内+12但+13。
我的错。尽管如此,谢谢你的答案,让我看到了我的错误。