Click here to close now.

Welcome!

Java Authors: Carmen Gonzalez, Pat Romanski, Elizabeth White, Lori MacVittie, Kyle Samani

Related Topics: Java

Java: Article

A Time Zone Patch

A flexible solution

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
I encountered the problem while working in a Java application that delivers Short Message Service (SMS) to wireless devices. The company's SMS application was being used in several countries and users were registering from several distinct time zones. A key requirement for the application is to have the user's local time as part of the SMS message.

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 following test case was used to identify the time zone DST transition dates problem. It's based on the JUnit framework and is composed by TestTimeZone and DST classes (see Listings 1 and 2, respectively). The source code can be downloaded from www.caroli.org.

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:
America/Santiago time zone


public class DST{
public static final String DATE_FORMAT = "MM/dd/yyyy";
public static final String START_DST = "10/10/2004";
public static final String END_DST = "03/14/2004";
public static final String TIMEZONE_LOCATION = "America/Santiago";
}

TimeZone Test 2:
America/Sao_Paulo time zone


public class DST{
public static final String DATE_FORMAT = "MM/dd/yyyy";
public static final String START_DST = "11/02/2004";
public static final String END_DST = "02/15/2005";
public static final String TIMEZONE_LOCATION = "America/Sao_Paulo";
}

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
The tests were executed with JRE 1.3 and 1.4 and both showed the same problem. Both versions of the TimeZone class read time zone IDs from the the tzmappings file in the jre/lib subdirectory. JRE 1.4 also contains time zone data files for various world regions in the jre/lib/zi subdirectory.

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


public class TestTimeZone extends TestCase {
.
protected void setUp() throws Exception {
// timeZone = TimeZonePatch.getTimeZone(DST.TIMEZONE_LOCATION);
timeZone = TimeZone.getTimeZone(DST.TIMEZONE_LOCATION);
super.setUp();
}
.
}

TimeZonePatch
The TimeZonePatch class has been created for patching time zones according to an elected time zone data source. Listing 3 shows the TimeZonePatch code.

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
TimeZonePatchListFactory can get the time zone data from different sources. For example, the time zone data source can be an XML file, text file, database, Web service, or any other data source appropriate for your application.

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.

More Stories By Paulo Caroli

Paulo Caroli, www.caroli.org, is the J2EE architect at blah!, a Telecom Italia Mobile (TIM) value-added services provider. Paulo has a master's degree in software engineering and is a Sun Certified Architect for Java Technology, with expertise in high-performance enterprise application development.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
sven van t veer 04/01/05 11:07:45 AM EST

The way I see it, the problem was originally found by Marcelo Lavor. The study was done by myself and Bruno Goyanna. The actual implementation as far as I know was done by Bruno since I'd already left the company.

It's nice to be able to take credit for the work of others.

Jack Schueler 01/13/05 09:39:18 AM EST

public class DST{
public static final String DATE_FORMAT = "MM/dd/yyyy";
public static final String START_DST = "10/10/2004";
public static final String END_DST = "03/14/2004";
public static final String TIMEZONE_LOCATION = "America/Santiago";
}
Should this
END_DST = "03/14/2004";
be this
END_DST = "03/14/2005";
to put your end date after your start date?

@ThingsExpo Stories
SYS-CON Events announced today that Akana, formerly SOA Software, has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. Akana’s comprehensive suite of API Management, API Security, Integrated SOA Governance, and Cloud Integration solutions helps businesses accelerate digital transformation by securely extending their reach across multiple channels – mobile, cloud and Internet of Things. Akana enables enterprises to share data as APIs, connect and integrate applications, drive part...
After making a doctor’s appointment via your mobile device, you receive a calendar invite. The day of your appointment, you get a reminder with the doctor’s location and contact information. As you enter the doctor’s exam room, the medical team is equipped with the latest tablet containing your medical history – he or she makes real time updates to your medical file. At the end of your visit, you receive an electronic prescription to your preferred pharmacy and can schedule your next appointment.
SYS-CON Events announced today that Solgenia will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY, and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. Solgenia is the global market leader in Cloud Collaboration and Cloud Infrastructure software solutions. Designed to “Bridge the Gap” between Personal and Professional Social, Mobile and Cloud user experiences, our solutions help large and medium-sized organizations dr...
SYS-CON Events announced today that Liaison Technologies, a leading provider of data management and integration cloud services and solutions, has been named "Silver Sponsor" of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York, NY. Liaison Technologies is a recognized market leader in providing cloud-enabled data integration and data management solutions to break down complex information barriers, enabling enterprises to make smarter decisions, faster.
The WebRTC Summit 2014 New York, to be held June 9-11, 2015, at the Javits Center in New York, NY, announces that its Call for Papers is open. Topics include all aspects of improving IT delivery by eliminating waste through automated business models leveraging cloud technologies. WebRTC Summit is co-located with 16th International Cloud Expo, @ThingsExpo, Big Data Expo, and DevOps Summit.
SYS-CON Events announced today that CommVault has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY, and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. A singular vision – a belief in a better way to address current and future data management needs – guides CommVault in the development of Singular Information Management® solutions for high-performance data protection, universal availability and sim...
Cloud is not a commodity. And no matter what you call it, computing doesn’t come out of the sky. It comes from physical hardware inside brick and mortar facilities connected by hundreds of miles of networking cable. And no two clouds are built the same way. SoftLayer gives you the highest performing cloud infrastructure available. One platform that takes data centers around the world that are full of the widest range of cloud computing options, and then integrates and automates everything. Join SoftLayer on June 9 at 16th Cloud Expo to learn about IBM Cloud's SoftLayer platform, explore se...
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that its Call for Papers is open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
The list of ‘new paradigm’ technologies that now surrounds us appears to be at an all time high. From cloud computing and Big Data analytics to Bring Your Own Device (BYOD) and the Internet of Things (IoT), today we have to deal with what the industry likes to call ‘paradigm shifts’ at every level of IT. This is disruption; of course, we understand that – change is almost always disruptive.
SYS-CON Media announced today that 9 out of 10 " most read" DevOps articles are published by @DevOpsSummit Blog. Launched in October 2014, @DevOpsSummit Blog offers top articles, news stories, and blog posts from the world's well-known experts and guarantees better exposure for its authors than any other publication. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce softw...
Wearable technology was dominant at this year’s International Consumer Electronics Show (CES) , and MWC was no exception to this trend. New versions of favorites, such as the Samsung Gear (three new products were released: the Gear 2, the Gear 2 Neo and the Gear Fit), shared the limelight with new wearables like Pebble Time Steel (the new premium version of the company’s previously released smartwatch) and the LG Watch Urbane. The most dramatic difference at MWC was an emphasis on presenting wearables as fashion accessories and moving away from the original clunky technology associated with t...
The world's leading Cloud event, Cloud Expo has launched Microservices Journal on the SYS-CON.com portal, featuring over 19,000 original articles, news stories, features, and blog entries. DevOps Journal is focused on this critical enterprise IT topic in the world of cloud computing. Microservices Journal offers top articles, news stories, and blog posts from the world's well-known experts and guarantees better exposure for its authors than any other publication. Follow new article posts on Twitter at @MicroservicesE
SYS-CON Events announced today that Site24x7, the cloud infrastructure monitoring service, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Site24x7 is a cloud infrastructure monitoring service that helps monitor the uptime and performance of websites, online applications, servers, mobile websites and custom APIs. The monitoring is done from 50+ locations across the world and from various wireless carriers, thus providing a global perspective of the end-user experience. Site24x7 supports monitoring H...
SYS-CON Events announced today that SafeLogic has been named “Bag Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. SafeLogic provides security products for applications in mobile and server/appliance environments. SafeLogic’s flagship product CryptoComply is a FIPS 140-2 validated cryptographic engine designed to secure data on servers, workstations, appliances, mobile devices, and in the Cloud.
@ThingsExpo has been named the Top 5 Most Influential M2M Brand by Onalytica in the ‘Machine to Machine: Top 100 Influencers and Brands.' Onalytica analyzed the online debate on M2M by looking at over 85,000 tweets to provide the most influential individuals and brands that drive the discussion. According to Onalytica the "analysis showed a very engaged community with a lot of interactive tweets. The M2M discussion seems to be more fragmented and driven by some of the major brands present in the M2M space. This really allows some room for influential individuals to create more high value inter...
SYS-CON Events announced today the IoT Bootcamp – Jumpstart Your IoT Strategy, being held June 9–10, 2015, in conjunction with 16th Cloud Expo and Internet of @ThingsExpo at the Javits Center in New York City. This is your chance to jumpstart your IoT strategy. Combined with real-world scenarios and use cases, the IoT Bootcamp is not just based on presentations but includes hands-on demos and walkthroughs. We will introduce you to a variety of Do-It-Yourself IoT platforms including Arduino, Raspberry Pi, BeagleBone, Spark and Intel Edison. You will also get an overview of cloud technologies s...
Containers and microservices have become topics of intense interest throughout the cloud developer and enterprise IT communities. Accordingly, attendees at the upcoming 16th Cloud Expo at the Javits Center in New York June 9-11 will find fresh new content in a new track called PaaS | Containers & Microservices Containers are not being considered for the first time by the cloud community, but a current era of re-consideration has pushed them to the top of the cloud agenda. With the launch of Docker's initial release in March of 2013, interest was revved up several notches. Then late last...
SOA Software has changed its name to Akana. With roots in Web Services and SOA Governance, Akana has established itself as a leader in API Management and is expanding into cloud integration as an alternative to the traditional heavyweight enterprise service bus (ESB). The company recently announced that it achieved more than 90% year-over-year growth. As Akana, the company now addresses the evolution and diversification of SOA, unifying security, management, and DevOps across SOA, APIs, microservices, and more.
GENBAND has announced that SageNet is leveraging the Nuvia platform to deliver Unified Communications as a Service (UCaaS) to its large base of retail and enterprise customers. Nuvia’s cloud-based solution provides SageNet’s customers with a full suite of business communications and collaboration tools. Two large national SageNet retail customers have recently signed up to deploy the Nuvia platform and the company will continue to sell the service to new and existing customers. Nuvia’s capabilities include HD voice, video, multimedia messaging, mobility, conferencing, Web collaboration, deskt...
The Open Compute Project is a collective effort by Facebook and a number of players in the datacenter industry to bring lessons learned from the social media giant's giant IT deployment to the rest of the world. Datacenters account for 3% of global electricity consumption – about the same as all of Switzerland or the Czech Republic -- according to people I met at the recent Open Compute Summit in San Jose. With increasing mobility at the edge of the cloud and vast new dataflows being predicted with the growth of the Internet of Things (and The Coming Age of Many Zettabytes) in the near...