5

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

Community
  • 1
  • 1
Arslan Anwar
  • 18,486
  • 18
  • 74
  • 104
  • 1
    Perhaps you want to read this documentation of Java 7, http://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html. It says you should use GMT as a reference. EDT, PST, or CST is deprecated. – The Original Android Jun 20 '12 at 23:19
  • [See this answer. You may want getTimeZone("America/New_York")](http://stackoverflow.com/questions/10545960/how-to-tackle-daylight-savings-using-timezone-in-java) – krubo Oct 09 '12 at 03:54

2 Answers2

9

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.

Community
  • 1
  • 1
prunge
  • 20,579
  • 3
  • 71
  • 75
1
TimeZone.setDefault(TimeZone.getTimeZone("GMT-04:00"));
Date date = new Date();
System.out.println("Current EDT Time is : "+date);
Alex Karshin
  • 11,259
  • 12
  • 45
  • 57