sunriser
sunriser

Reputation: 780

Calendar for specific TimeZone does not showing daylight savings in android and java

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.

Upvotes: 1

Views: 1266

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502016

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

Upvotes: 2

Related Questions