|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Java Desktop A Time Zone Patch
A flexible solution
By: Paulo Caroli
Jan. 5, 2005 12:00 AM
The java.util.TimeZone abstract class that represents a time zone is used to produce local time for a particular global time zone. A TimeZone comprises three basic pieces of information: an ID, a time zone offset, and the logic necessary to deal with DST (Daylight Savings Time). While working with the TimeZone class provided with JRE 1.3 and 1.4, however, I discovered that it provides erroneous times for several time zones. This article describes this DST transition date problem, presents TimeZone test scenarios, and introduces TimeZonePatch. This patch is a flexible solution that enables the java.util.TimeZone to produce correct time zone data.
TimeZone Problem The TimeZone JRE class was being used to retrieve the user's local time in the SMS message according to the user's registered time zone. It was discovered, however, that the application was delivering erroneous local times, and further investigation showed that the DST transition dates were not accurate for a number of time zones.
Testing TimeZone The correct DST entry and exit dates can be selected from trusted sources. For this article the dates were selected from the world clock Web site: www.timeanddate.com/worldclock/full.html. To verify the DST transition problem the TestTimeZone test case was executed for two distinct time zones: America/Sao_Paulo and America/Santiago. The time zone data in the DST classes was retrieved from the world clock Web site.The tests results are depicted in Figures 1 and 2. Note that these tests are based on the JDK TimeZone class usage.
TimeZone Test 1:
TimeZone Test 2:
Test 2 for TimeZone shows the inaccuracy in the DST entry date of the TimeZone class with the America/Sao_Paulo time zone.
Patching TimeZone One solution for the 1.4 TimeZone class is to update the time zone files; however, these are binary and its change is dependent on you having access to and intimate knowledge of the JRE. The JRE provider could supply updates to the time zone files, however, it's not straightforward even for them to keep track of all the time zones' correct data. Although there are conventions about the DST transition dates and times, countries have the freedom to decide their own transition dates and times, and this has occurred in several places. For these reasons I developed a patch for the SDK TimeZone, which solves the wrong DST transition dates problem independent of the original TimeZone implementation. TimeZone Test Set-Up
TimeZonePatch The fix works by having the TimeZonePatch class return your desired version of TimeZone. In case there is a time zone correction, this correction will be set in the timezonePatchList, which holds the patched TimeZone's objects. The TimeZonePatchListFactory class builds timezonePatchList.
A Factory of Your Time Zone Data Listing 4 shows TimeZonePatchListFactory, a Factory Method implementation that creates a time zone patch list based on an XML time zone data source. (Listings 4-6 can be downloaded from www.sys-con.com/java/sourcec.cfm.) Listing 5 has timezonePatch.xml, an XML data source read by TimeZonePatchListFactory for patching the America/Sao_Paulo time zone. The presented TimeZonePatchListFactory implementation uses JOX (Java Objects in XML) library for reading the XML file into the TimeZonePatchList JavaBean. When reading the XML document, a mapping between the root node name - TimezonePatchList - and the nested nodes - timezonePatch, dst, startDate, and endDate - is automatically made for the TimeZonePatchList bean, and the nested beans - TimeZonePatch, DST, and Date - respectively. Reader Feedback: Page 1 of 1
Your Feedback
SOA World Latest Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||