1
votes

I can't get accurate time for a specific time zone such as "EST" which is GMT-5 in general,but right now it follows daylight savings which is GMT-4. But in java and android both shows the 1 hour difference (It takes only GMT-5,not GMT-4). But in dot net it shows correctly. My problem is same as How to tackle daylight savings using Timezone in java But I can use only short form of TimeZone such as ("EST","IST"). Please can you give some suggestion to get the accurate time based on a specific short form time zone(like "EST"...)

Thank you.

1
IMO the answer you refered to is fairly clear - what you try to achieve is just not possible.home
"EST" does not follow daylight saving time. "Eastern time" does - but that's EST which becomes EDT. The "S" of EST is "standard", i.e. "no daylight saving".Jon Skeet
These 3-4 letters abbreviations are not real time zones, not standardized, and not even unique(!). Trying to work with these will lead to pain and suffering; avoid them. Work with values in UTC generally, and work with proper time zone names in continent/region format.Basil Bourque

1 Answers

2
votes

There's no perfect answer to this, as you've basically got incomplete information. However, here's some code to find all the supported time zones which use the abbreviation "EST" (in non-daylight time, in the US locale):

import java.util.*; 

public class Test {
  public static void main(String[] args) {
    for (TimeZone zone : getMatchingTimeZones("EST")) {
      System.out.println(zone.getID());
    }
  }

  public static List<TimeZone> getMatchingTimeZones(String target) {
    List<TimeZone> list = new ArrayList<TimeZone>();
    for (String id : TimeZone.getAvailableIDs()) {
      TimeZone zone = TimeZone.getTimeZone(id);
      String actual = zone.getDisplayName(false, TimeZone.SHORT, Locale.US);
      if (actual.equals(target)) {
        list.add(zone);
      }
    }
    return list;
  }
}

How you get from that list to the actual time zone you want is up to you. (Also I don't know whether this will work on Android. I haven't tried it.)