【发布时间】:2012-02-29 08:49:13
【问题描述】:
要求只是获取给定时区的当前挂钟时间(包括正确的 DST 调整)。
在 SO 中似乎有一些问题围绕着这个问题徘徊,但我似乎无法以低摩擦的方式找到一个直接的答案(在 SO、Joda doco 或谷歌搜索中)。似乎对于给定的输入(当前 UTC 时间和所需的 TZ),我应该能够从 Joda Time 库中链接几个方法来实现我想要的,但在上述示例中似乎希望评估 + 处理偏移量/应用程序代码中的转换——如果可能的话,我想避免这种情况,并根据其可用的静态 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 我们是 +12,但在 DST 期间是 +13。
我的错。尽管如此,还是感谢您的回答,让我看到了我的错误。
【问题讨论】: