【问题标题】:While formatting GMT time to GMT+01:00 an extra hour is being added在将 GMT 时间格式化为 GMT+01:00 时,正在添加一个额外的小时
【发布时间】:2015-03-28 09:44:01
【问题描述】:

我面临的问题是仅在 GMT+01:00 格式化后将时间添加到时间中。

这是我的做法:

private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                       

    try {
        // Change Start date according to the Locale time
        formattor.setTimeZone(TimeZone.getTimeZone("GMT"));
        date1 = formattor.parse("2015-03-26 11:17:41");
        formattor.setTimeZone(TimeZone.getDefault());

        String start = formattor.format(date1);

        // Change End date according to the Locale time
        formattor.setTimeZone(TimeZone.getTimeZone("GMT"));
        date1 = formattor.parse("2015-03-26 13:17:00");
        formattor.setTimeZone(TimeZone.getDefault());

        String end = formattor.format(date1);

        System.out.println(start + " - " + end);
    } catch (ParseException e) {
        e.printStackTrace();
    }

输出:

2015-03-26 12:17:41 - 2015-03-26 14:17:00

我在使用 Joda-Time 时遇到了同样的问题。

我已经用GMT+01:00, West Africa Standard Time测试过了

【问题讨论】:

  • 这是意料之中的事:如果是 GMT 时间 11:17:41,那么 GMT+1 就是 12:17:41。这就是 GMT+1 的意思。你希望这会输出什么?为什么?
  • 你为什么认为这是错误的?格林威治标准时间 11:17 与格林威治标准时间 +01:00 的 12:17 相同。你为什么不期待这个?
  • 看起来不错。在输出中包含 TZ 偏移,这将有助于“可视化”问题。输出时间应该是 GMT 输入的 +1。
  • 对不起,我想我需要更新问题。
  • 或删除它。这里没有问题。

标签: java locale jodatime simpledateformat


【解决方案1】:

这很明显,因为您的 getDefault () 会将时区设置为 GMT+01:00,因此您会看到相同的结果。如果这不是您所期望的,请重新定义问题。

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2012-07-04
    • 2015-09-03
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多