Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Joda time获取给定时区的当前墙时间_Java_Timezone_Jodatime - Fatal编程技术网

Java 使用Joda time获取给定时区的当前墙时间

Java 使用Joda time获取给定时区的当前墙时间,java,timezone,jodatime,Java,Timezone,Jodatime,要求只需获取给定时区的当前墙时间(包括正确的DST调整) 在SO中似乎存在一些问题,但我似乎找不到一个简单的答案(在SO、Joda doco或谷歌搜索中),用一种低摩擦的方式来获取墙时间。它似乎与给定的输入(当前UTC时间和期望的TZ)类似我应该能够从Joda时间库中链接几个方法来实现我想要的,但在上述示例中似乎希望评估+处理应用程序代码中的偏移/转换-如果可能,我希望避免这种情况,并根据可用的静态TZ规则集使用Jodas best effort 出于这个问题的目的,让我们假设我不会使用任何其他

要求只需获取给定时区的当前墙时间(包括正确的DST调整)

在SO中似乎存在一些问题,但我似乎找不到一个简单的答案(在SO、Joda doco或谷歌搜索中),用一种低摩擦的方式来获取墙时间。它似乎与给定的输入(当前UTC时间和期望的TZ)类似我应该能够从Joda时间库中链接几个方法来实现我想要的,但在上述示例中似乎希望评估+处理应用程序代码中的偏移/转换-如果可能,我希望避免这种情况,并根据可用的静态TZ规则集使用Jodas best effort

出于这个问题的目的,让我们假设我不会使用任何其他第三方服务(基于网络或其他二进制文件),只使用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
所以在这里阳光充足,我们是+12,但在DST期间是+13


我的错。尽管如此,还是感谢您的回答,让我明白了我的错误。

您是否查看了
日期时间
构造函数:

DateTime(DateTimeZone zone) 
这将构造一个DateTime,表示指定时区中的当前时间。

如何:

DateTime utc = new DateTime(DateTimeZone.UTC);
DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
DateTime losAngelesDateTime = utc.toDateTime(tz);
最干净的方法 我发现以下是最干净的方法

DateTime currentTime = DateTime.now( DateTimeZone.UTC );
这将以UTC为单位获取当前时间,但此值可以转换为另一个
DateTimeZone
,也可以用不同的
DateTimeZone
替换
DateTimeZone.UTC

使用系统时区 如果要将其设置为系统时区,可以使用以下选项:

DateTime currentTime = DateTime.now( DateTimeZone.getDefault() );

我更喜欢
DateTime.now(DateTimeZone)
,因为我发现它更明显。顺便说一下,要调整时区调用
withZone(myTimeZone)
。正确,但有两个问题。(A) 不需要传递毫秒的当前时间,Joda time会在内部为您这样做。该参数用于在代码被传递毫秒计数时进行转换。(B) “local”是一个糟糕的var名称选择,因为与LocalDate混淆,这意味着没有时区。
.forId
应该是
.forId