Search code examples
javatimetimezonedst

Time zone of a country, whether day light savings is in effect


Consider the below code:

if(strTimeZoneCd.equals("A"))
            locTz = TimeZone.getTimeZone("AST");
        else if(strTimeZoneCd.equals("M"))
            locTz = TimeZone.getTimeZone("MST");
        else if(strTimeZoneCd.equals("P"))
            locTz = TimeZone.getTimeZone("PST");
        else if(strTimeZoneCd.equals("H"))
            locTz = TimeZone.getTimeZone("HST");
        else if(strTimeZoneCd.equals("C"))
            locTz = TimeZone.getTimeZone("CST");
        else if(strTimeZoneCd.equals("E"))
            locTz = TimeZone.getTimeZone("EST");
        else if(strTimeZoneCd.equals("G"))
            locTz = TimeZone.getTimeZone("GMT");
        else if(strTimeZoneCd.equals("Y"))
            locTz = TimeZone.getTimeZone("AKST");

Here if I am passing A, it will give me AST. But instead of that I need to determine if I should return AST or ADT.

I need to determine if AST is under daylight saving now. If it is under daylight saving, I can return ADT and if it is not I can return AST. But I am not getting how to determine whether that timezone is under daylight saving or not.

Can someone please help me?


Solution

  • You can use this program to determine whether that timezone is under day light saving or not.

    Also see this link.

    TimeZone.getTimeZone("CST") returns GMT

    import java.util.Date;
    import java.util.TimeZone;
    
    public class test {
    
        public static void main(String[] args) {
            System.out.println(TimeZone.getTimeZone("EST").inDaylightTime( new Date() ));
            System.out.println(TimeZone.getTimeZone( "GMT-9:00").inDaylightTime( new Date() ));
        }
    }