Search code examples
androidtimezonedst

Android : Why Time Zone with id "EST" returns false for useDaylightTime()


In my Android app why EST time zone always returns false for useDaylightTime(). Its found that EST uses daylight saving but the method is always returning false (Reference : time zone list).

   TimeZone tz = TimeZone.getTimeZone("EST");
    if(tz.useDaylightTime()){
    //Never enters here
    }

Please anyone can help me to figure out what's going wrong? I don't know much about the topic I appreciate any effort to get an Idea about this one.

Some more information regarding the situation i am facing: I know "EDT" is the ID used for time zone representing Daylight saving time zone but in my case, The Time zone Id is received from the server side and I am using that one to know do daylight saving is applicable on that time zone.


Solution

  • Don’t rely on three and four letter time zone abbreviations. They are ambiguous. EST may refer to North American Eastern Standard Time or Australian Eastern Standard Time (don’t know if there are more). Instead use a time zone ID in the region/city format, for example America/New_York or Australia/Sydney. See Wikipedia for list.

    To answer your question: TimeZone interprets EST as a fixed UTC offset of -05:00. So it hasn’t got any daylight saving time. You may say that it’s logical: daylight saving time isn’t standard time. The abbreviation for Eastern Daylight Saving Time would have been EDT instead.

    ZoneId

    Now we’re at it, the TimeZone class is outdated and poorly designed. If you can, look into java.time, the modern Java date and time API, and its ZoneId class instead.

    ZoneId zoneId = ZoneId.of( "America/New_York" ) ;
    ZoneRules rules = zoneId.getRules() ;
    boolean isNowInDst = rules.isDaylightSavings( Instant.now() ) ;