Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting EDT time zone to GMT not working fine in java

Tags:

java

timezone

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

like image 451
Arslan Anwar Avatar asked Jun 20 '12 22:06

Arslan Anwar


People also ask

How do you convert EDT to GMT?

Eastern Daylight Time is 4 hours behind Greenwich Mean Time.

How do I change the TimeZone without changing the time in Java?

The best way to to it is probably to create a new Calendar object, set the Timezone on it, and then set all of the fields individually, so year, month, day, hour, minute, second, getting the values from the Date object. Then you won't be using any deprecated methods.

How do you change time zones in Java?

To convert any time to the specific timezone (for example: UTC -> local timezone and vise versa) with any time pattern you can use java. time library. This method will take time patterns (original and required format) and timezone (original time zone and required timezone) will give String as output.

How does Java determine TimeZone?

By default, the JVM reads time zone information from the operating system. This information gets passed to the TimeZone class, which stores the time zone and calculates the daylight saving time. We can call the method getDefault, which will return the time zone where the program is running.


2 Answers

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.

like image 179
prunge Avatar answered Oct 30 '22 13:10

prunge


TimeZone.setDefault(TimeZone.getTimeZone("GMT-04:00"));
Date date = new Date();
System.out.println("Current EDT Time is : "+date);
like image 29
Rahul Palakurthi Avatar answered Oct 30 '22 14:10

Rahul Palakurthi