I reviewed many questions related to TimeZones on Stackoverflow, but I could not find the one to the problem I am struggling with:
- Why doesn't Joda's
DateTimeZone.getDefault()
return updated timezone on TZ change (after resuming an application?).TimeZone.getDefault()
seems to be working just fine. - Should I use
DateTimeZone.forTimeZone(TimeZone.getDefault())
to get up to date Joda'sDateTimeZone
object?
Here is how to replicate:
- Start app that prints both
DateTimeZone.getDefault()
andTimeZone.getDefault()
:
09-15 16:46:59.512 14961-14961/com.example.android.whatever D/TimeZone: DateTimeZone.getDefault()=Europe/London; TimeZone.getDefault()=libcore.util.ZoneInfo[id="Europe/London",...]
- Go to settings -> change Timezone to PDT.
- Go back to Application that prints stuff (e.g. in onResume()):
09-15 08:49:24.727 14961-14961/com.example.android.whatever D/TimeZone: DateTimeZone.getDefault()=Europe/London; TimeZone.getDefault()libcore.util.ZoneInfo[id="America/Los_Angeles",...]
- At this stage I can be rotating the App. The
DateTimeZone.getDefault()
will be stuck. - Only after application onRestart - the value will be correct.
Why is it so?
Joda-Time caches the default timezone.
If you run this code (in my JVM, the default timezone is
America/Sao_Paulo
):The output will be:
Also note that
t1 == t2
returnstrue
, which means they are exactly the same instance.To set Joda's default timezone after changing the JVM default, you must set it in
DateTimeZone
too:This outputs:
After restarting everything, the cache disappears and Joda-Time gets the new default when first called.
You should not directly use Joda-Time but better use the library of Daniel Lew (JodaTimeAndroid - a thin wrapper around Joda-Time) because