java.time in Scala - Getting UTC offset from Time Zone

2.4k views Asked by At

I am trying to get the UTC offset from a timezone (Asia/Aden) using java.time.

See errors below:

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
1

There are 1 answers

0
AudioBubble On
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