java – 当Android中的Zone发生更改时,为什么DateTimeZone.getDefault()不会更新
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 当Android中的Zone发生更改时,为什么DateTimeZone.getDefault()不会更新,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2199字,纯文字阅读大概需要4分钟。
内容图文
![java – 当Android中的Zone发生更改时,为什么DateTimeZone.getDefault()不会更新](/upload/InfoBanner/zyjiaocheng/820/ae0ac741bd9b47abaea96630574f7da5.jpg)
我在Stackoverflow上回顾了许多与TimeZones相关的问题,但我找不到我正在努力解决的问题:
>为什么Joda的DateTimeZone.getDefault()在TZ更改时(恢复应用程序后?)返回更新的时区. TimeZone.getDefault()似乎工作正常.
>我应该使用DateTimeZone.forTimeZone(TimeZone.getDefault())来获取最新的Joda的DateTimeZone对象吗?
以下是如何复制:
>启动打印DateTimeZone.getDefault()和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”,…]
>转到设置 – >将时区更改为PDT.
>返回打印东西的应用程序(例如在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”,…]
>在这个阶段我可以旋转App. DateTimeZone.getDefault()将被卡住.
>只有在应用onRestart之后 – 该值才是正确的.
为什么会这样?
解决方法:
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=America/Sao_Paulo
Joda Default=America/Sao_Paulo
JVM default=Europe/London
Joda Default=America/Sao_Paulo
true
另请注意,t1 == t2返回true,这意味着它们是完全相同的实例.
要在更改JVM默认值后设置Joda的默认时区,您还必须在DateTimeZone中设置它:
// 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 Default=Europe/London
false
重新启动所有内容后,缓存消失,Joda-Time在首次调用时获取新的默认值.
内容总结
以上是互联网集市为您收集整理的java – 当Android中的Zone发生更改时,为什么DateTimeZone.getDefault()不会更新全部内容,希望文章能够帮你解决java – 当Android中的Zone发生更改时,为什么DateTimeZone.getDefault()不会更新所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。