将 EDT 时区转换为 GMT 在 Java 中无法正常工作 [英] Converting EDT time zone to GMT not working fine in java

查看:29
本文介绍了将 EDT 时区转换为 GMT 在 Java 中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来解析此日期.它必须将新日期显示为 "2012-06-20 03:09:38" 因为 EDT 是 -4GMT 而我当前的位置是 GMT+5.但它没有显示它现在显示它是

private static void convertEDT_TO_GMT() {尝试 {字符串 s = "2012-06-20 18:09:38";SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");df.setTimeZone(TimeZone.getTimeZone(EDT"));日期时间戳 = null;时间戳 = df.parse(s);df.setTimeZone(TimeZone.getTimeZone("GMT+05:00"));System.out.println("Old = " + s);字符串解析 = df.format(timestamp);System.out.println("New = " + parsed);} 捕获(异常 e){e.printStackTrace();}}

显示

<块引用>

旧 = 2012-06-20 18:09:38

新 = 2012-06-20 23:09:38

解决方案

时区EDT"不存在.执行`TimeZone.getTimeZone("EDT") 的 System.out.println() 表明它正在回退到 GMT,因为 Java 不知道 'EDT' 作为时区.

"EDT" 更改为 "GMT-04:00" 给出了正确的结果:

尝试{字符串 s = "2012-06-20 18:09:38";SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//df.setTimeZone(TimeZone.getTimeZone("EDT"));df.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));日期时间戳 = null;时间戳 = df.parse(s);df.setTimeZone(TimeZone.getTimeZone("GMT+05:00"));System.out.println("旧 = " + s);字符串解析 = df.format(timestamp);System.out.println("New = " + 解析);} 捕获(异常 e){e.printStackTrace();}

结果:

旧 = 2012-06-20 18:09:38新 = 2012-06-21 03:09:38

根据这篇文章:><块引用>

东部夏令时不是完整"时区的名称 - 它是半"时区,实际上总是比 UTC 晚 4 小时.

所以使用 "GMT-04:00" 可能是正确的解决方案.

I am using this code to parse this date. It must show new date as "2012-06-20 03:09:38" as EDT is -4GMT and my current location is GMT+5. But its not showing this it now showing as it is

private static void convertEDT_TO_GMT() {
    try {
        String s = "2012-06-20 18:09:38";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("EDT"));
        Date timestamp = null;

        timestamp = df.parse(s);
        df.setTimeZone(TimeZone.getTimeZone("GMT+05:00"));
        System.out.println("Old = " + s);
        String parsed = df.format(timestamp);
        System.out.println("New = " + parsed);

    } catch (Exception e) {
        e.printStackTrace();
    }
    }

It show

Old = 2012-06-20 18:09:38

New = 2012-06-20 23:09:38

解决方案

The time zone 'EDT' does not exist. Doing a System.out.println() of `TimeZone.getTimeZone("EDT") shows that it is falling back to GMT because Java does not know 'EDT' as a time zone.

Changing from "EDT" to "GMT-04:00" gives the correct result:

try {
    String s = "2012-06-20 18:09:38";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //df.setTimeZone(TimeZone.getTimeZone("EDT"));
    df.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));
    Date timestamp = null;

    timestamp = df.parse(s);
    df.setTimeZone(TimeZone.getTimeZone("GMT+05:00"));
    System.out.println("Old = " + s);
    String parsed = df.format(timestamp);
    System.out.println("New = " + parsed);

    } catch (Exception e) {
    e.printStackTrace();
}

Result:

Old = 2012-06-20 18:09:38
New = 2012-06-21 03:09:38

According to this post:

Eastern Daylight Time isn't the name of a "full" time zone - it's "half" a time zone, effectively, always 4 hours behind UTC.

So using "GMT-04:00" might be the right solution.

这篇关于将 EDT 时区转换为 GMT 在 Java 中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆