• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP: Daylight Savings in Java

 
Riga Tanatra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
DateFormat.MEDIUM,
locale);
localFormat.setTimeZone(targetTimezone);
String localdate = localFormat.format(date);
return localdate;
}
 
Riga Tanatra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
[id=GMT-06:00,offset=-21600000,dstSavings=3600000,useDaylight=false]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
C:\j2sdk1.4.2\jre\lib\tzmappings
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:
C:\j2sdk1.4.2\jre\lib\zi
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,
Avi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic