3

I am trying to get the transition date for daylight savings in Android. That is the date that daylight savings start and ends for a specific timezone.

How do I do that?

Emad
  • 4,110
  • 5
  • 30
  • 35

2 Answers2

3

there's no way to do that directly, but you could use TimeZone.isDaylightTime(Date), as in:

TimeZone tz = TimeZone.getTimeZone ("America/Denver") ;
Date date = new GregorianCalendar (2010, 10, 15).getTime () ;
if (tz.isDaylightTime (date)) {
// dst in effect code
   }
else {
// dst not in effect code
   }

Of course, to get the start date (if that is really what you need), you need to iterate over all days in a given year and check each one...which would be pretty inefficient. Also, as noted in the answer above, as far as I know, the daylight savings time info is compiled into java.util library and so if a given municipality changes its laws about when dst starts/ends you'll get incorrect results.

Also, it appears that the java.util.TimeZone implementation in Android is broken (at least in 2.2). For instance

TimeZone denver = TimeZone.getTimeZone ("America/Denver") ;
TimeZone phoenix = TimeZone.getTimeZone ("America/Phoenix") ;

== true ; // which it shouldn't

Running under the emulate in Eclipse, denver.hasSameRules (phoenix) returns true, and it should return false: both are GMT-7:00 but Phoenix doesn't use dst. I don't have access to any Android devices to test whether this is just a problem with the emulator or not. I actually started searching this archive for an answer to this denver == phoenix problem and came across your post so thought I'd answer.

Ad Infinitum
  • 3,560
  • 27
  • 33
Paul Biron
  • 31
  • 2
1

You have just stepped into a VERY messy briar patch. The rules for daylight saving time are complex and set by man so there isn't an algorithm you can use. For an idea see this page: http://home.tiscali.nl/~t876506/TZworld.html. That only shows the current rules. If you want to get the rules programatically or for past dates you will need to query the tz database directly. This is not easy nor trivial and the process would take longer to explain than is appropriate here (also, I would have to figure it out :) ) but the information you need to get started is on this page: http://www.twinsun.com/tz/tz-link.htm.

If you are doing time calculations and need to take the shift into consideration, it will be easier to just use joda-time. Here's a link to it: http://joda-time.sourceforge.net/. It has all the tz database information so you don't have to parse it. You will need to keep it up to date though because the rules change regularly.

As a side note, there may be a way to query the daylight saving start and end dates out of joda-time for a given timezone but I couldn't find it.

Jere.Jones
  • 9,915
  • 5
  • 35
  • 38
  • For anyone who stumbles upon this post. There's another answer on stackoverflow on exactly how to use Joda-Time to get the next Timezone transition: http://stackoverflow.com/questions/1449500/get-daylight-saving-transition-dates-for-time-zones-in-java – Mike Aug 17 '11 at 15:22