Why DateTimeZone.getDefault () is not updated when changing Zone in Android

I looked at many issues related to TimeZones in Stackoverflow, but I could not find the problem with the problem I'm struggling with:

  • Why DateTimeZone.getDefault()does Joda not return the updated time zone when the TZ changes (after resuming the application?). seems to work just fine . TimeZone.getDefault()
  • Should I use DateTimeZone.forTimeZone(TimeZone.getDefault())to update a Joda object DateTimeZone?

Here's how to replicate:

  • Launch an application that prints both DateTimeZone.getDefault(), and TimeZone.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" , ...]

  1. Go to settings โ†’ change time zone to PDT.
  2. Return to the application that prints the material (for example, 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" ...]

  1. At this point, I can rotate the application. DateTimeZone.getDefault()will depend on.
  2. Only after the onRestart application - will the value be correct.

Why is this so?

+4
2

Joda-Time .

( JVM - America/Sao_Paulo):

System.out.println("JVM default=" + TimeZone.getDefault().getID()); // America/Sao_Paulo
DateTimeZone t1 = DateTimeZone.getDefault();
System.out.println("Joda Default=" + t1); // America/Sao_Paulo

// setting the default timezone to London
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
System.out.println("JVM default=" + TimeZone.getDefault().getID()); // Europe/London
DateTimeZone t2 = DateTimeZone.getDefault();
System.out.println("Joda Default=" + t2); // America/Sao_Paulo
System.out.println(t1 == t2);  // true

:

JVM default = /Sao _Paulo
Joda = /Sao _Paulo
JVM default = /
Joda = /Sao _Paulo

, t1 == t2 true, , .

Joda JVM , DateTimeZone:

// change Joda default timezone to be the same as the JVM's
DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault()));
DateTimeZone t3 = DateTimeZone.getDefault();
System.out.println("Joda Default=" + t3); // Europe/London
System.out.println(t1 == t3); // false

:

Joda Default = /

, Joda-Time .

+3

Joda-Time, Daniel Lew (JodaTimeAndroid - a Joda-Time),

  • tz Android
  • .
+3

Source: https://habr.com/ru/post/1685761/


All Articles