【问题标题】:Timezone changed Automatically when create new Date object in Java在 Java 中创建新的 Date 对象时自动更改时区
【发布时间】:2016-02-11 05:04:11
【问题描述】:

我的时间戳基于 IST(印度标准时间/格林威治标准时间 + 5:30 小时),我的应用程序根据设备时间戳显示上次活动时间。 这是当前时间 - 活动时间戳。当设备时区是 IST 时很好。

例如,

Activity timestamp - 11/Feb/2016 09:00:00 AM
Current timestamp (IST)  - 11/Feb/2016 10:00:00 AM
My Calculation = Current timestamp - Activity timestamp
So Application shows 1 hr ago

但设备时区同时更改为 PHT 等其他时区(菲律宾时间/格林威治标准时间 + 8 小时)

Activity timestamp - 11/Feb/2016 09:00:00 AM
Current timestamp(PHT)  - 11/Feb/2016 12:30:00 AM (plus 2:30Hrs compare with IST)
My Calculation = Current timestamp - Activity timestamp
So Application shows 3 hrs 30 mis ago

我的问题是,如何始终使用 java 获取 IST 时间?无论时区如何,我都需要 IST 时间。

我尝试了下面的代码,当我将值更改为 IST 但时区自动更改为设备时区。

源码请参考以下网址http://goo.gl/dnvQF5

 SimpleDateFormat sd = new SimpleDateFormat(
        "yyyy.MM.dd G 'at' HH:mm:ss z");
    Date date = new Date();
    // TODO: Avoid using the abbreviations when fetching time zones.
    // Use the full Olson zone ID instead.
    sd.setTimeZone(TimeZone.getTimeZone("GMT"));
    String gmtDate = sd.format(date);
    System.out.println("GMT --> " + gmtDate);
    String istDate = gmtDate.replace("GMT", "IST");
    System.out.println("After Replace ->  " + istDate);
    sd.setTimeZone(TimeZone.getTimeZone("IST"));
    try {
        Date istConvertedDate = sd.parse(gmtDate);
        System.out.println("After Convert --> " + istConvertedDate);
    } catch (ParseException ex) {
        ex.printStackTrace();
    }

我得到了像

这样的输出
GMT --> 2016.02.11 AD at 05:20:07 GMT                                                                                                                                              
After Replace ->  2016.02.11 AD at 05:20:07 IST                                                                                                                                    
After Convert --> Thu Feb 11 00:20:07 EST 2016 

请帮我解决这个问题。

【问题讨论】:

  • 使用名为 JODA TIME 的库

标签: java android datetime timezone


【解决方案1】:

java.util.Date 类只是自 UNIX 纪元 (1970-01-01T00:00:00Z) 以来经过的毫秒数的精简包装。这种类型的对象不携带任何格式或时区信息。因此,在使用SimpleDateFormat 解析带有时区标识符或名称的文本后,每个此类对象都完全丢失了时区信息

您观察到并感到困惑的是,此类的方法toString() 使用基于系统时区的特定表示。

另一件事:如果您应用字符串操作将“GMT”替换为“IST”(一个矛盾的时区名称 - 以色列?印度?爱尔兰?),那么您可以有效地更改时刻/即时,同时保持本地时间表示。你真的想要这个吗?

如果您想保留最初解析的时区信息,那么您可以在库Threeten-ABP 中使用ZonedDateTime 或在库Joda-Time-Android 中使用DateTime 或在我的库Time4A 中使用ZonalDateTime

【讨论】:

    【解决方案2】:

    试试

     public static void main(String[] args) {
        SimpleDateFormat sd = new SimpleDateFormat(
            "yyyy.MM.dd G 'at' HH:mm:ss z");
        Date date = new Date();
        // TODO: Avoid using the abbreviations when fetching time zones.
        // Use the full Olson zone ID instead.
        sd.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(sd.format(date));
        String gmtDate = sd.format(date);
        System.out.println(gmtDate);
    
        //Here you create a new date object
        Date istDate= new Date();
        sd.setTimeZone(TimeZone.getTimeZone("IST"));
        String istDate=sd.format(istDate);
        System.out.println(istDate)
    
       }
    

    这样,第一个打印时间是 GMT,第二个打印时间是 ISD。

    【讨论】:

    • 我可以将 IST 日期作为字符串获取。但我需要带有 IST 的日期对象。所以只有我能做减法。
    • @Dhinakar java.util.Date 没有任何特定时区 - 请参阅我的答案。
    【解决方案3】:

    你可以使用 java.util.Calendar

    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("IST"));
    

    此日历包含 IST 时区的当前时间。

    你可以设置时间戳:

    calendar.setTimeInMillis(timeStamp);
    

    并获取 java.util.Date 或时间戳

    Date date = calendar.getTime();
    Long timeStamp = calendar.getTimeInMillis();
    

    【讨论】:

    • 我也试过了。但仍然是同样的问题。问题是当我尝试将时间转换为 IST 时,日期对象自动更改为设备时区。 :(
    【解决方案4】:

    替换

    sd.setTimeZone(TimeZone.getTimeZone("IST"));
    

    TimeZone.setDefault(TimeZone.getTimeZone("IST"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多