Click here to close now.

Welcome!

Java Authors: Pat Romanski, Elizabeth White, Carmen Gonzalez, Roger Strukhoff, Liz McMillan

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 Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, 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. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
Temasys has announced senior management additions to its team. Joining are David Holloway as Vice President of Commercial and Nadine Yap as Vice President of Product. Over the past 12 months Temasys has doubled in size as it adds new customers and expands the development of its Skylink platform. Skylink leads the charge to move WebRTC, traditionally seen as a desktop, browser based technology, to become a ubiquitous web communications technology on web and mobile, as well as Internet of Things compatible devices.
SYS-CON Events announced today that IDenticard 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. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada. Our partner network encompasses some 300 of the world's leading systems integrators and security s...
“With easy-to-use SDKs for Atmel’s platforms, IoT developers can now reap the benefits of realtime communication, and bypass the security pitfalls and configuration complexities that put IoT deployments at risk,” said Todd Greene, founder & CEO of PubNub. PubNub will team with Atmel at CES 2015 to launch full SDK support for Atmel’s MCU, MPU, and Wireless SoC platforms. Atmel developers now have access to PubNub’s secure Publish/Subscribe messaging with guaranteed ¼ second latencies across PubNub’s 14 global points-of-presence. PubNub delivers secure communication through firewalls, proxy ser...
SYS-CON Events announced today that On the Avenue Marketing Group, a sales and marketing firm that utilizes events to market and sell products to consumers, 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. On the Avenue Marketing Group (OTA) is a sales and marketing firm that utilizes events to market and sell products to consumers. On behalf of our clients, we attend thousands of fairs, festivals, expos, concerts, conferences, and sporting events annually, helping them reach millions of individuals ...
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...
“In the past year we've seen a lot of stabilization of WebRTC. You can now use it in production with a far greater degree of certainty. A lot of the real developments in the past year have been in things like the data channel, which will enable a whole new type of application," explained Peter Dunkley, Technical Director at Acision, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Media announced today that @WebRTCSummit Blog, the largest WebRTC resource in the world, has been launched. @WebRTCSummit 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. @WebRTCSummit Blog can be bookmarked ▸ Here @WebRTCSummit conference site can be bookmarked ▸ Here
SYS-CON Events announced today that Ciqada will exhibit at SYS-CON's @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Ciqada™ makes it easy to connect your products to the Internet. By integrating key components - hardware, servers, dashboards, and mobile apps - into an easy-to-use, configurable system, your products can quickly and securely join the internet of things. With remote monitoring, control, and alert messaging capability, you will meet your customers' needs of tomorrow - today! Ciqada. Let your products take flight. For more inform...
Health care systems across the globe are under enormous strain, as facilities reach capacity and costs continue to rise. M2M and the Internet of Things have the potential to transform the industry through connected health solutions that can make care more efficient while reducing costs. In fact, Vodafone's annual M2M Barometer Report forecasts M2M applications rising to 57 percent in health care and life sciences by 2016. Lively is one of Vodafone's health care partners, whose solutions enable older adults to live independent lives while staying connected to loved ones. M2M will continue to gr...
The best mobile applications are augmented by dedicated servers, the Internet and Cloud services. Mobile developers should focus on one thing: writing the next socially disruptive viral app. Thanks to the cloud, they can focus on the overall solution, not the underlying plumbing. From iOS to Android and Windows, developers can leverage cloud services to create a common cross-platform backend to persist user settings, app data, broadcast notifications, run jobs, etc. This session provides a high level technical overview of many cloud services available to mobile app developers, includi...
SYS-CON Events announced today that GENBAND, a leading developer of real time communications software solutions, has been named “Silver Sponsor” of SYS-CON's WebRTC Summit, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. The GENBAND team will be on hand to demonstrate their newest product, Kandy. Kandy is a communications Platform-as-a-Service (PaaS) that enables companies to seamlessly integrate more human communications into their Web and mobile applications - creating more engaging experiences for their customers and boosting collaboration and productiv...
Dave will share his insights on how Internet of Things for Enterprises are transforming and making more productive and efficient operations and maintenance (O&M) procedures in the cleantech industry and beyond. Speaker Bio: Dave Landa is chief operating officer of Cybozu Corp (kintone US). Based in the San Francisco Bay Area, Dave has been on the forefront of the Cloud revolution driving strategic business development on the executive teams of multiple leading Software as a Services (SaaS) application providers dating back to 2004. Cybozu's kintone.com is a leading global BYOA (Build Your O...
SYS-CON Events announced today that BroadSoft, the leading global provider of Unified Communications and Collaboration (UCC) services to operators worldwide, has been named “Gold Sponsor” of SYS-CON's WebRTC Summit, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. BroadSoft is the leading provider of software and services that enable mobile, fixed-line and cable service providers to offer Unified Communications over their Internet Protocol networks. The Company’s core communications platform enables the delivery of a range of enterprise and consumer calling...
While not quite mainstream yet, WebRTC is starting to gain ground with Carriers, Enterprises and Independent Software Vendors (ISV’s) alike. WebRTC makes it easy for developers to add audio and video communications into their applications by using Web browsers as their platform. But like any market, every customer engagement has unique requirements, as well as constraints. And of course, one size does not fit all. In her session at WebRTC Summit, Dr. Natasha Tamaskar, Vice President, Head of Cloud and Mobile Strategy at GENBAND, will explore what is needed to take a real time communications ...
WebRTC is an up-and-coming standard that enables real-time voice and video to be directly embedded into browsers making the browser a primary user interface for communications and collaboration. WebRTC runs in a number of browsers today and is currently supported in over a billion installed browsers globally, across a range of platform OS and devices. Today, organizations that choose to deploy WebRTC applications and use a host machine that supports audio through USB or Bluetooth can use Plantronics products to connect and transit or receive the audio associated with the WebRTC session.
What exactly is a cognitive application? In her session at 16th Cloud Expo, Ashley Hathaway, Product Manager at IBM Watson, will look at the services being offered by the IBM Watson Developer Cloud and what that means for developers and Big Data. She'll explore how IBM Watson and its partnerships will continue to grow and help define what it means to be a cognitive service, as well as take a look at the offerings on Bluemix. She will also check out how Watson and the Alchemy API team up to offer disruptive APIs to developers.
The IoT Bootcamp is coming to Cloud Expo | @ThingsExpo on June 9-10 at the Javits Center in New York. Instructor. Registration is now available at http://iotbootcamp.sys-con.com/ Instructor Janakiram MSV previously taught the famously successful Multi-Cloud Bootcamp at Cloud Expo | @ThingsExpo in November in Santa Clara. Now he is expanding the focus to Janakiram is the founder and CTO of Get Cloud Ready Consulting, a niche Cloud Migration and Cloud Operations firm that recently got acquired by Aditi Technologies. He is a Microsoft Regional Director for Hyderabad, India, and one of the f...
The 17th International Cloud Expo has announced that its Call for Papers is open. 17th International Cloud Expo, to be held November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA, brings together Cloud Computing, APM, APIs, Microservices, Security, Big Data, Internet of Things, DevOps and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding business opportunity. Submit your speaking proposal today!
As enterprises move to all-IP networks and cloud-based applications, communications service providers (CSPs) – facing increased competition from over-the-top providers delivering content via the Internet and independently of CSPs – must be able to offer seamless cloud-based communication and collaboration solutions that can scale for small, midsize, and large enterprises, as well as public sector organizations, in order to keep and grow market share. The latest version of Oracle Communications Unified Communications Suite gives CSPs the capability to do just that. In addition, its integration ...