- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 TimeZone.getDefault()
设置 Calendar
类的时区:
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
Log.i("TEST", cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE));
但是,当用户从“设置”更改他们设备的时区时,我的应用程序会显示使用前一个时区的时间,直到他们强制停止(从应用程序信息设置)应用程序并重新启动它。
如何阻止 getDefault()
的缓存?
最佳答案
它并不漂亮,但您可以调用 setDefault(null)
来显式删除缓存值。根据 the documentation ,这只会影响当前进程(即您的应用)。
清空缓存值后,下次调用 getDefault()
时,该值会重新构造:
/**
* Returns the user's preferred time zone. This may have been overridden for
* this process with {@link #setDefault}.
*
* <p>Since the user's time zone changes dynamically, avoid caching this
* value. Instead, use this method to look it up for each use.
*/
public static synchronized TimeZone getDefault() {
if (defaultTimeZone == null) {
TimezoneGetter tzGetter = TimezoneGetter.getInstance();
String zoneName = (tzGetter != null) ? tzGetter.getId() : null;
if (zoneName != null) {
zoneName = zoneName.trim();
}
if (zoneName == null || zoneName.isEmpty()) {
try {
// On the host, we can find the configured timezone here.
zoneName = IoUtils.readFileAsString("/etc/timezone");
} catch (IOException ex) {
// "vogar --mode device" can end up here.
// TODO: give libcore access to Android system properties and read "persist.sys.timezone".
zoneName = "GMT";
}
}
defaultTimeZone = TimeZone.getTimeZone(zoneName);
}
return (TimeZone) defaultTimeZone.clone();
}
您可能应该将其与 ACTION_TIMEZONE_CHANGED
的广播监听器结合使用如果您收到这样的广播,则只清空默认值。
编辑:仔细想想,一个更简洁的解决方案是从广播中提取新设置的时区。来自广播文档:
time-zone - The java.util.TimeZone.getID() value identifying the new time zone.
然后您可以简单地使用此标识符来更新缓存的默认值:
String tzId = ...
TimeZone.setDefault(TimeZone.getTimeZone(tzId));
随后对 getDefault()
的任何调用都将返回正确/更新的时区。
关于android - 如何防止我的应用程序获取 TimeZone.getDefault() 的缓存值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126454/
我需要以 [+/-]hh:mm 格式保存手机的时区 我正在使用 TimeZone 类来处理这个问题,但我能得到的唯一格式如下: PST -05:00 GMT +02:00 我宁愿不对结果进行子串化,是
我正在尝试在 Node js 中使用 moment-timezone 来获取所有可用时区的列表,如下所示 - var moment = require('moment-timezone'); var
我正在使用以下代码将时区 (GMT-3) 转换为设备本地时区。 int hour=17,minute=0,day=12,month=6,year=2014; Calendar cal = new Gr
System.setProperty("user.timezone", "America/Chicago"); TimeZone.setDefault(TimeZone.getTimeZone("美国
我想从字符串初始化DateFormatter().timeZone,但是当我做类似的事情时 let dateFormatter = DateFormatter() dateFormatter.time
我通过存储日期组件字典在我的后端存储用户出生日期。它看起来像这样: { "day": 1, "month": 1, "year": 1970, "timeZone":
我一直在开发一个应用程序,它以下面给出的特定格式从服务器获取日期。 "2015-02-03 00:00:00" 我想使用 NSDateFormatter 以不同的 UI 格式显示它们但是当我更改时区时
我有什么 我有一个来自服务器的日期字符串,它的格式是 w3c 日期格式 2016-02-13T09:53:49.871Z 我的问题 当我将消息发布到服务器时,它显示的是 5 小时前而不是现在 我想要什
我正在将我的项目从 Django 1.8.2 升级到 1.9.7,我收到了这个警告: WARNINGS: my_app.my_model.date_available: (fields.W161) F
在下面的代码中: from datetime import datetime import pytz EDT = pytz.timezone('US/Eastern') d1 = datetime.n
我通过以下方式将服务器(Ubuntu 12.04)的时区更改为UTC: echo 'UTC' > /etc/timezone dpkg-reconfigure --frontend nonintera
我想获取洛杉矶的时区信息,现在10/10/2017是夏令时,但是我通过两种方式获取洛杉矶的时区得到了不同的结果。 public class TimeZoneDemo2 { public stati
我把它放在应用程序 Controller 中: before_filter :set_timezone def set_timezone Time.zone = current_user.time_
是否可以转换java.util.TimeZone字符串 sun.util.calendar.ZoneInfo[id=\"America/Los_Angeles\",offset=-28800000,d
当我写这段代码时: Calendar cal = Calendar.getInstance(); cal.setTimeZone(TimeZone.getTimeZone("EST"));
我正在使用 Java 8, 在我们的代码的前面,我们使用 sdf.setTimeZone(TimeZone.getTimeZone("PDT")); 转换为失败的美国太平洋地区(没有抛出任何错误,但转
我不断收到此警告:timezone of object (UTC) is different than current timezone ().我当前的时区是“EET”,如 Sys.timezone(
我正在我的网站上使用时刻时区转换器进行时区日期转换。默认情况下,我在 IST 中显示日期和时间。我有一个按钮可以转换用户时区中的给定时间。此按钮操作调用 moment js 并进行转换。到这里我没有问
我从服务响应中获得时区偏移量为“-5.00”,我需要在 UI 中将其格式化为 CST。我如何在 JavaScript 中转换它?我搜索了一下,不确定JavaScript中有没有直接的方法。 最佳答案
这个问题在这里已经有了答案: Converting string to datetime object (2 个答案) 关闭 6 年前。 我的主机有 UTC 时区,我正在尝试将 PDT 时间字符串转
我是一名优秀的程序员,十分优秀!