【问题标题】:Printing GMT and PST timestamp shows a difference of only 7 hours打印 GMT 和 PST 时间戳显示仅相差 7 小时
【发布时间】:2014-12-23 08:48:40
【问题描述】:

我的理解是 PST 与 GMT / UTC 相差 8 小时。但是,当我打印出来时,我发现只有 7 小时的差异。你能解释一下我在这里做错了什么吗?

    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date date = sdf1.parse("2014-05-01 13:31:03.7");

    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmssS");
    df.setTimeZone(TimeZone.getTimeZone("PST"));
    System.out.println(df.format(date));
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(df.format(date));
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(df.format(date));

打印:

20140501_1331037
20140501_2031037
20140501_2031037

【问题讨论】:

  • 避免使用 3 或 4 个字母的时区代码。这些代码既不是标准化的也不是唯一的。使用proper time zone names。这些名称大多是大陆、斜线和城市或地区的组合。对于美国西海岸,America/Los_Angeles。对于加拿大东部,America/Montreal.
  • 如果您使用 Joda-Time 或 java.time 而不是 java.util.Date/.Calendar,则处理时区会容易得多。
  • 为了记录,我正在转换我的代码以使用上面建议的 Joda Time。
  • 您不会后悔学习 Joda-Time 的投资。我添加了an answer 以帮助您解决此问题的问题。

标签: java timezone simpledateformat


【解决方案1】:

我假设您在夏季/夏令时执行 PST,以节省 GMT+7 的时间。试试隆冬。

来自http://wwp.greenwichmeantime.co.uk/time-zone/usa/pacific-time/

太平洋时间何时更改为夏令时?

在大多数州 在美国和加拿大的大多数省份,夏令时 (DST) 被观察到。在 DST 期间,PT 或 PDT 比格林威治平均值晚 7 小时 时间 (GMT-7)。

在夏季月份之后,太平洋时间向后移动 1 小时到美国 太平洋标准时间 (PST) 或 (GMT-8)。

美国各州采用日光的时间表 节省时间是:

3 月的第二个星期日凌晨 2 点到

第一个星期日凌晨 2 点 11 月。

【讨论】:

    【解决方案2】:

    您在这里没有做错任何事情。如果将时区添加到输出格式中:

        SimpleDateFormat df = new SimpleDateFormat("HH mm ssS Z z");
    

    您可以看到输出实际上是 PDT(夏令时)而不是 PST(常规)

    10 31 037 -0700 PDT
    17 31 037 +0000 GMT
    17 31 037 +0000 UTC
    12 31 037 -0500 EST
    17 31 037 +0000 GMT
    

    五月是夏令时。

    【讨论】:

      【解决方案3】:

      这是使用 Joda-Time 2.6 的解决方案。

      String inputRaw = "2014-05-01 13:31:03.7";
      String input = inputRaw.replace( " ", "T" ); // Adjust input to comply with the ISO 8601 standard.
      DateTimeZone zone = DateTimeZone.UTC;
      DateTime dateTime = new DateTime( input, zone );  // The input lacks an offset. So specify the time zone by which to interpret the parsed input string. The resulting DateTime is then adjusted to the JVM’s current default time zone. So, *two* time zones were used in this line of code, one explicit, the other implicit.
      DateTime dateTimeUtc = dateTime.withZone( DateTimeZone.UTC );  // Adjust to UTC.
      DateTime dateTimeLosAngeles = dateTime.withZone( DateTimeZone.forID( "America/Los_Angeles" ) );  // Adjust to US west coast time zone. DST is automatically applied as needed.
      DateTime dateTimeKolkata = dateTime.withZone( DateTimeZone.forID( "Asia/Kolkata" ) );  // Adjust to India time, five and a half hours ahead of UTC.
      

      所有这些 DateTime 对象都代表宇宙历史时间轴中的同一时刻。每个都有不同的时间(可能还有不同的日期),以适应特定地区人们可能看到的时钟上的“挂钟”。

      【讨论】:

        猜你喜欢
        • 2014-10-19
        • 1970-01-01
        • 2011-03-04
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多