Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JodaTime doesn't recognize some Android timezones

I'm using JodaTime in my app and I'm getting reports from the developer console about crashes like this one:

java.lang.RuntimeException: Unable to resume activity {com.myapp.app/Home}: java.lang.IllegalArgumentException: The datetime zone id 'Africa/Abuja' is not recognised
....
Caused by: java.lang.IllegalArgumentException: The datetime zone id 'Africa/Abuja' is not recognised
at org.joda.time.DateTimeZone.forTimeZone(DateTimeZone.java:337)
...

I'm using this line of code to get the Timezone:

DateTimeZone tz = DateTimeZone.forTimeZone(TimeZone.getDefault());

Interestingly, most of the crashes are Africa/****.

TimeZone.getDefault() is part of the Android API, so I assumed that it would return usable timezone IDs.

Is there anything I can do with these timezones to be able to use them properly? Is this a known problem with Android and Joda Time?

like image 651
you786 Avatar asked Mar 19 '14 07:03

you786


2 Answers

Use this Joda version adapted for Android: https://github.com/dlew/joda-time-android

Then, run JodaTimeAndroid.init(this); in your Application's onCreate (it's in their documentation), and voilà, it just works, all timezones are recognized.

like image 149
espinchi Avatar answered Nov 15 '22 07:11

espinchi


The zone "Africa/Abuja" is not a valid time zone identifier in IANA-tz-database using Olson identifiers (named after inventor Arthur David Olson). Instead the valid zone identifier is in this case "Africa/Lagos".

You should also note that JodaTime follows IANA (look for the youngest version there, too) and that Android has a different home-grown tz-database which might use different time zone identifiers. According to this android-api-link Android supports Olson-tz-identifiers. I am not quite sure why you use "Africa/Abuja" at all resp. why this shall be valid. Is it contained in TimeZone.getAvailableIDs()?

If anything strange in Android configuration causes TimeZone.getDefault() using "Africa/Abuja" then you could try to set the system property "user.timezone" to "Africa/Lagos". JodaTime will first look at this property.

like image 21
Meno Hochschild Avatar answered Nov 15 '22 08:11

Meno Hochschild