How to get the GMT value for EDT time zone?

it seems that EDT timezone Id is not present in the java.util.Timezone package, if I want to have EDT - (GMT-4:00) and CDT as timezone, how do I do it. I have posted my code below.

public static void main(String arg[]) {
            String timeZone = "EDT";
            String timeZoneInGMTFormat = "";
            TimeZone timezone = TimeZone.getTimeZone(timeZone);
            Date date = new Date();      
            int offset = timezone.getRawOffset();
            timeZoneInGMTFormat = timeZone + " - (GMT"
                    + String.format("%s%02d:%02d", offset >= 0 ? "+" : "", offset / 3600000, (offset 
                    / 60000) % 60) + ")";   
            System.out.print("Time format = " + timeZoneInGMTFormat);
        }

1 answer

  • answered 2021-05-05 12:38 Gilbert Le Blanc

    Here are the results of one of my tests.

    Time format = US/Eastern - (GMT-4:00)
    Time format = US/Central - (GMT-5:00)
    

    I had to use a specific date in the summer to get the daylight-savings offsets.

    Here's the code I tested with.

    import java.util.Calendar;
    import java.util.Date;
    import java.util.TimeZone;
    
    public class Timezone {
        public static void main(String arg[]) {
            String timeZone = "US/Eastern";
            calculateOffset(timeZone);
            timeZone = "US/Central";
            calculateOffset(timeZone);
        }
    
        private static void calculateOffset(String timeZone) {
            TimeZone timezone = TimeZone.getTimeZone(timeZone);
            
            Calendar c1 = Calendar.getInstance();
            c1.set(Calendar.MONTH, 07);
            Date date = c1.getTime();
         
            int offset = timezone.getOffset(date.getTime());
            String timeZoneInGMTFormat = timeZone + " - (GMT"
                    + String.format("%s%02d:%02d", offset >= 0 ? "+" : "", 
                            offset / 3600000, (offset / 60000) % 60) + ")";   
            System.out.println("Time format = " + timeZoneInGMTFormat);
        }
    
    }