4

I am getting an exception while trying to get time zone for the location "America/Punta_Arenas". I am using joda LocalDateTime.

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

So the above statement is throwing the following exception

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

Is there any way I can get the time zone for the location America/Punta_Arenas? any help is appreciated.

  • 1
    I cannot reproduce. I use Joda-Time 2.9.9 on Java 1.8.0_131. Your code creates a `DateTimeZone` object that in turn prints as `America/Punta_Arenas`. What version of Joda-Time and what version of Java are you using? Also works on Java 1.7.0_79. – Ole V.V. Jan 31 '19 at 09:56
  • 2
    According to [Joda-Time Available Time Zones](https://www.joda.org/joda-time/timezones.html) America/Punta_Arenas was previously not included. There’s a procedure for [Updating the time zone data](https://www.joda.org/joda-time/tz_update.html). If that were me, I’d first try updating to that latest Joda-Time version. If that doesn’t help, I’d try updating the time zone data following the procedure. – Ole V.V. Jan 31 '19 at 10:04
  • Thank you @OleV.V. I was using an older version 2.8.1, now I updated that to 2.9.4 and issue got resolved. – ArunBabuVijayanath Jan 31 '19 at 12:20

1 Answers1

4

Joda-Time

Joda-Time carries its own copy of the time zone data known as tzdata. Time zone definitions change, so this file may need updating.

You haven't mentioned which version of Joda-Time you are using, first upgrade to latest if possible and that should work:

<!-- 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

The Joda-Time project is now in maintenance-mode. It’s creator, Stephen Colebourne, went on to lead JSR 310 and its implementation, java.time, found in Java 8 and later. This is the official successor to Joda-Time.

In Java's java.time package you will find ZoneId.of.

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

ThreeTen-Backport

Much of java.time functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project, another project led by Stephen Colebourne.

There you will find the org.threeten.bp.ZoneId class.

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

The code will be the same as above with different import:

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

Hope that helps

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
justMe
  • 2,200
  • 16
  • 20