为什么更改Android中的区域时DateTimeZone.getDefault()无法更新 [英] Why DateTimeZone.getDefault() does not get updated when Zone in Android is changed

查看:168
本文介绍了为什么更改Android中的区域时DateTimeZone.getDefault()无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Stackoverflow上回顾了许多与TimeZones有关的问题,但找不到与我苦苦挣扎的问题有关的问题:

I reviewed many questions related to TimeZones on Stackoverflow, but I could not find the one to the problem I am struggling with:


  • 为什么Joda的 DateTimeZone.getDefault()在TZ更改时不返回更新的时区(恢复应用程序后)? TimeZone.getDefault()似乎工作正常

  • 我应该使用 DateTimeZone.forTimeZone(TimeZone.getDefault())获取最新的Joda的 DateTimeZone 对象?

  • 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's DateTimeZone object?

以下是复制方法:


  1. 开始同时显示 DateTimeZone.getDefault() TimeZone.getDefault()的应用程序:

  1. Start app that prints both DateTimeZone.getDefault() and TimeZone.getDefault():




09-15 16:46:59.512 14961-14961 / com.example.android.whatever D / TimeZone:
DateTimeZone.getDefault()=欧洲/伦敦; TimeZone.getDefault()= libcore.util.ZoneInfo [id = Europe / London ,...]




  1. 进入设置->将时区更改为PDT。

  2. 返回打印内容的应用程序(例如在onResume()中):




09- 15 08:49:24.727 14961-14961 / com.example.android.whatever
D / TimeZone: DateTimeZone.getDefault()=欧洲/伦敦;
TimeZone.getDefault()libcore.util.ZoneInfo [id = America / Los_Angeles >,...]




  1. 在此阶段,我可以旋转应用程序。 DateTimeZone.getDefault()将被卡住。

  2. 仅在应用程序onRestart之后-值将是正确的。

  1. At this stage I can be rotating the App. The DateTimeZone.getDefault() will be stuck.
  2. Only after application onRestart - the value will be correct.

为什么?

推荐答案

Joda-时间会缓存默认时区。

Joda-Time caches the default timezone.

如果运行此代码(在我的JVM中,默认时区为 America / Sao_Paulo ):

If you run this code (in my JVM, the default timezone is 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默认值=美国/圣保罗

Joda默认值=美国/ Sao_Paulo

JVM默认值=欧洲/伦敦

Joda默认值= America / Sao_Paulo

true

JVM default=America/Sao_Paulo
Joda Default=America/Sao_Paulo
JVM default=Europe/London
Joda Default=America/Sao_Paulo
true

还要注意 t1 == t2 返回 true ,这意味着它们是完全相同的实例。

Also note that t1 == t2 returns true, which means they are exactly the same instance.

设置Joda的默认时区更改JVM默认值后,也必须在 DateTimeZone 中进行设置:

To set Joda's default timezone after changing the JVM default, you must set it in DateTimeZone too:

// change Joda's 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默认值=欧洲/伦敦

false

Joda Default=Europe/London
false

重新启动所有内容后,缓存消失,Joda-Time在首次调用时会获得新的默认值。

After restarting everything, the cache disappears and Joda-Time gets the new default when first called.

这篇关于为什么更改Android中的区域时DateTimeZone.getDefault()无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆