1

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.

Community
  • 1
  • 1
sunriser
  • 770
  • 3
  • 12
  • IMO the answer you refered to is fairly clear - what you try to achieve is just not possible. – home May 27 '12 at 08:11
  • 2
    "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 May 27 '12 at 08:17
  • 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](https://en.wikipedia.org/wiki/Coordinated_Universal_Time) generally, and work with [proper time zone names](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) in `continent/region` format. – Basil Bourque Jul 14 '16 at 23:00

1 Answers1

2

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194