java - 如果电话时区已更改,则 SimpleDateFormat setTimeZone 不起作用

标签 java android timezone jodatime

我在使用 SimpleDateFormat 和时区时遇到了一个奇怪的问题。

基本上,我有这个代码 fragment :

    String input              = "2017-12-21 16:15:00";
    String inputTZ            = "America/Los_Angeles";
    String phoneTZ            = TimeZone.getDefault().getID();
    SimpleDateFormat fmtInput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    fmtInput.setTimeZone(TimeZone.getTimeZone(inputTZ));

    SimpleDateFormat fmtOutputEventTZ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    fmtOutputEventTZ.setTimeZone(TimeZone.getTimeZone(inputTZ));

    SimpleDateFormat fmtOutputPhoneTZ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    fmtOutputPhoneTZ.setTimeZone(TimeZone.getTimeZone(phoneTZ));


    try {
        LocalDateTime dt = new LocalDateTime(fmtInput.parse(input));

        Log.d("DEBUG>>>>", "INPUT=" + input);
        Log.d("DEBUG>>>>", "TZ_EVENT=" + inputTZ);
        Log.d("DEBUG>>>>", "TZ_PHONE=" + phoneTZ);
        Log.d("DEBUG>>>>", "DT=" + dt);
        Log.d("DEBUG>>>>", "OUTPUT_EVENT=" + fmtOutputEventTZ.format(dt.toDate()));
        Log.d("DEBUG>>>>", "OUTPUT_PHONE=" + fmtOutputPhoneTZ.format(dt.toDate()));
    }
    catch (Exception e) {
        e.printStackTrace();
    }

代码逻辑如下:

  • 我有一个洛杉矶时区 (GMT-8) 的输入日期
  • 我解析这个指定时区 (LA) 的日期
  • 然后我使用原始时区 (LA) 和当前手机时区格式化日期

我在将手机时区设置为芝加哥 (GMT-6) 的情况下运行代码并获得以下输出。

D/DEBUG>>>>: INPUT=2017-12-21 16:15:00
D/DEBUG>>>>: TZ_EVENT=America/Los_Angeles
D/DEBUG>>>>: TZ_PHONE=America/Chicago
D/DEBUG>>>>: DT=2017-12-21T18:15:00.000
D/DEBUG>>>>: OUTPUT_EVENT=2017-12-21 16:15:00
D/DEBUG>>>>: OUTPUT_PHONE=2017-12-21 18:15:00

基本上,结果是预期的。

但是,如果我在不重启应用程序的情况下将手机时区更改为纽约时区 (GMT-5),则会得到以下输出

D/DEBUG>>>>: INPUT=2017-12-21 16:15:00
D/DEBUG>>>>: TZ_EVENT=America/Los_Angeles
D/DEBUG>>>>: TZ_PHONE=America/New_York
D/DEBUG>>>>: DT=2017-12-21T18:15:00.000
D/DEBUG>>>>: OUTPUT_EVENT=2017-12-21 15:15:00
D/DEBUG>>>>: OUTPUT_PHONE=2017-12-21 18:15:00

请注意 TimeZone.getDefault().getID() 返回的时区是正确的,但是在解析时,SimpleDateFormat 将其转换为电话时区仍然是芝加哥,因此返回错误的时间!

但是,如果我终止应用程序并重新启动它,相同的代码可以正常工作:

D/DEBUG>>>>: INPUT=2017-12-21 16:15:00
D/DEBUG>>>>: TZ_EVENT=America/Los_Angeles
D/DEBUG>>>>: TZ_PHONE=America/New_York
D/DEBUG>>>>: DT=2017-12-21T19:15:00.000
D/DEBUG>>>>: OUTPUT_EVENT=2017-12-21 16:15:00
D/DEBUG>>>>: OUTPUT_PHONE=2017-12-21 19:15:00

根据我的理解,SimpleDateFormat.parse 解析日期并将其存储为自 1970 年以来的秒数。使用 setTimeZone 应该使用时区来适本地移动时间。

但是,SimpleDateFormat 似乎没有反射(reflect)手机时区的变化,而 TimeZone.getDefault().getID() 清楚地反射(reflect)了变化。

有什么办法可以解决这个问题吗?

最佳答案

找到解决方案!

我在一开始就调用了 DateTimeZone.setDefault(DateTimeZone.forID(TimeZone.getDefault().getID()))

我猜 DateTimeZone.setDefault 在应用程序启动时被调用,但之后不会被 joda-time 本身重置,即使用户的时区可能已经改变。

关于java - 如果电话时区已更改,则 SimpleDateFormat setTimeZone 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47948641/

相关文章:

java - 使用java流比较两个字符串列表

android - 是否可以在Android中使用编写c代码访问rild套接字

带有状态到时区偏移的 php 数组

PHP 默认时区

java - 如何使用继承覆盖 equals() 方法?

java - 在执行期间,Java 程序如何知道它使用了多少内存?

java - Proguard 混淆 + getJarEntry

android - 在安卓中使用 AliasActivity

android - 由 : java. lang.IllegalStateException 引起:

php - 同步 MySQL 和 PHP 时间