java - 日期时区在查找位置 "America/Punta_Arenas"的 ID 时遇到问题

标签 java date timezone jodatime google-location-services

我在尝试获取位置“America/Punta_Arenas”的时区时遇到异常。我正在使用 joda LocalDateTime。

import org.joda.time.{DateTime, DateTimeZone}
val timezone = DateTimeZone.forID("America/Punta_Arenas")

所以上面的语句抛出了下面的异常

java.lang.IllegalArgumentException: The datetime zone id 'America/Punta_Arenas' is not recognised

有什么方法可以获取位置 America/Punta_Arenas 的时区?任何帮助表示赞赏。

最佳答案

Joda Time

Joda-Time 带有自己的时区数据副本,称为 tzdata .时区定义更改,因此此文件可能需要更新。

您没有提到您使用的是哪个版本的 Joda-Time,如果可能,请先升级到最新版本,这样应该可以:

<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.1</version>
</dependency> 

java.time

Joda-Time 项目现在处于维护模式。它的创建者 Stephen Colebourne 继续领导 JSR 310 及其实现,java.time,在 Java 8 及更高版本中发现。这是 Joda-Time 的官方继承者。

在 Java 的 java.time 包中,您会找到 ZoneId.of

ZoneId zoneId = ZoneId.of("America/Punta_Arenas");

ThreeTen-反向移植

ThreeTen-Backport 项目(另一个由 Stephen Colebourne 领导的项目)中,java.time 的大部分功能已向后移植到 Java 6 和 7。

在那里你会找到 org.threeten.bp.ZoneId 类。

<!-- https://mvnrepository.com/artifact/org.threeten/threetenbp -->
<dependency>
    <groupId>org.threeten</groupId>
    <artifactId>threetenbp</artifactId>
    <version>1.3.8</version>
</dependency>

代码将与上面相同,但导入不同:

import org.threeten.bp.ZoneId;
ZoneId zoneId = ZoneId.of("America/Punta_Arenas");

希望对你有帮助

关于java - 日期时区在查找位置 "America/Punta_Arenas"的 ID 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54457334/

相关文章:

java - java/android中的不同日期

java - 将时分字符串时间转换为 utc,反之亦然

Java Getter Setter 代码风格

java - 终端无法运行apktool

java - 在android中使用服务下载多个文件

php - 如何在 Laravel 中格式化输出 MySQL 日期?

php - MySQL 加入速度慢。任何加速的方法

Java:lambda 局部变量声明问题

SQL 'Round' 将日期更新为一周中的给定日期

timezone - 没有午夜的一天