【问题标题】:java.time in Scala - Getting UTC offset from Time ZoneScala 中的 java.time - 从时区获取 UTC 偏移量
【发布时间】:2015-06-12 19:24:02
【问题描述】:

我正在尝试使用 java.time 从时区(亚洲/亚丁)获取 UTC 偏移量。

查看以下错误:

scala> val a = ZoneId.of("Asia/Aden")
a: java.time.ZoneId = Asia/Aden

scala> ZoneOffset.of(a)
<console>:13: error: type mismatch;
 found   : java.time.ZoneId
 required: String
              ZoneOffset.of(a)
                            ^

scala> val as = a.getId
as: String = Asia/Aden

scala> ZoneOffset.of(as)
java.time.DateTimeException: Invalid ID for ZoneOffset, non numeric characters found: Asia/Aden
  at java.time.ZoneOffset.parseNumber(ZoneOffset.java:269)
  at java.time.ZoneOffset.of(ZoneOffset.java:236)
  ... 33 elided

【问题讨论】:

  • java.time.ZoneId.normalized()?它应该返回一个ZoneOffset,如果它是一个固定区域,你可以模式匹配/asInstanceOf
  • 我相信ZoneOffset.of 需要+0200 字符串或类似的东西,而不是ZoneId id。

标签: scala utc timezone-offset


【解决方案1】:
scala> val b = ZonedDateTime.now(ZoneId.of("Asia/Aden")).getOffset
b: java.time.ZoneOffset = +03:00

Wikipedia:

CC  Coordinates     TZ  Comments    UTC offset  UTC DST offset
YE  +1245+04512     Asia/Aden       +03:00      +03:00

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2013-10-15
    • 1970-01-01
    • 2011-07-29
    • 2018-10-20
    • 2014-01-09
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多