The best thing I've come up with when I need this functionality is to use Calendar and iterations throughout the year, in the specified time zone, and ask, every hour of every day is begging or ending the DST.
You have to do it this way because in the Sun JVM, the TimeZone implementation (sun.util.calendar.ZoneInfo) contains time zone transitions in some kind of βcompiledβ form.
The code looks something like this:
public class Dst { Date start; Date end; public static Dst calculate(TimeZone tz, int year) { final Calendar c = Calendar.getInstance(tz); c.setLenient(false); c.set(year, Calendar.JANUARY, 1, 1, 0, 0); c.set(Calendar.MILLISECOND, 0); if (tz.getDSTSavings() == 0) { return null; } Dst dst = new Dst(); boolean flag = false; do { Date date = c.getTime(); boolean daylight = tz.inDaylightTime(date); if (daylight && !flag) { flag = true; dst.start = date; } else if (!daylight && flag) { flag = false; dst.end = date; } c.add(Calendar.HOUR_OF_DAY, 1); } while (c.get(Calendar.YEAR) == year); return dst; } }
Of course, it makes sense to cache / remember the result of these calculations, etc.
Hope this helps.
javashlook Feb 24 '09 at 13:53 2009-02-24 13:53
source share