The moose likes Java in General and the fly likes HELP: Daylight Savings in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "HELP: Daylight Savings in Java" Watch "HELP: Daylight Savings in Java" New topic

HELP: Daylight Savings in Java

Riga Tanatra

Joined: Jul 07, 2003
Posts: 2
I would appreciate help with timezones and daylight savings time in Java.
Here is the code snippet I am using, and I can't figure out how to do DAYLIGHT SAVINGS TIME. The times are off by an hour. Thanks!
public static String dateToLocalized(
TimeZone targetTimezone,
Locale locale,
Date date) {
DateFormat localFormat =
DateFormat.getDateTimeInstance( DateFormat.MEDIUM,
String localdate = localFormat.format(date);
return localdate;
Riga Tanatra

Joined: Jul 07, 2003
Posts: 2
Further, printing out my timezone object shows that useDaylight is set to false. Can I accomplish
this using DateFormat.format() which I am using currently, or in any other way? Any input would be appreciated.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Well, you could create a new SimpleTimeZone and give it the same properties as the original TimeZone, except give it different daylight savings behavior. But I think this is a bad idea. There are locales in the world which do not use daylight savings (I live in one of them), or which use different start and end dates than you might expect. If you try to override the behavior of one of these TimeZone objects, you will produce the incorrect answer. The place to fix this is not inside the dateToLocalized() method. The problem is that the TimeZone object which is being passed to this method is incorrect - so fix the code that finds or creates this object in the first place, not the code that uses it later.
My guess - a TimeZone is usually obtained from a Locale. If your TimeZone doesn't behave the way you expect, you've probably got the wrong Locale. What Locale and TimeZone are you using, and what locale are you trying to program for? Use TimeZone's getID() and Locale's toString() method to learn the names of the TimeZone and Locale you're using.

"I'm not back." - Bill Harding, Twister
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

Hi Riga,
Nearly every time and date related class in the JDK has -- either directly or indirectly -- a member variable which is an instance of "java.util.TimeZone", and, by default, _all_ of those members are set to the "default" time zone -- unless you explicitly set the member to a different time zone. However, you need to set the time zone for each individual object. You can't set it once and expect all the other classes to use it -- unless you change the default time zone.
I didn't see any details in your post regarding your java version or platform, but since version 1.3, all of the available time zone IDs are located in the "tzmappings" file which is located in the "jre/lib" subdirectory of the JDK installation. On my Windows XP machine (with J2SE SDK 1.4.2) the full path is:
Note that this is a text file, so I can open and read it using "Notepad", for example.
Since JDK 1.4, all the daylight savings rules, raw offsets, etc are stored under the "zi" subdirectory. Again, on my machine the full path is:
Note that these are data files -- I can't read them using "Notepad". In fact, I don't know how to read them. In any case, sometimes they may be incorrect.
So, in my opinion, the best thing to do is to change the default time zone using the setDefault() method, and create an instance of SimpleTimeZone to use for your default.
What I do, is set the default time zone at the start of my application. That way, I know all the other classes will use it.
Good Luck,
I agree. Here's the link:
subject: HELP: Daylight Savings in Java
jQuery in Action, 3rd edition