Wednesday, December 26, 2012
How to display date in multiple timezone in Java with Example - PST GMT
We can use SimpleDateFormat class to display date in multiple Timezone in Java. While working in global Java application its quite common to display dates in different timezone, classical example is Server is running on either PST or GMT timezone and clients are global or at least running on global trading hubs like Hong-kong, Mumbai, Tokyo, London etc. Unfortunately Date and Time API in Java is quite tricky and until you have good understanding of Date and Time classes and methods e.g. Calendar, SimpleDateFormat and thread-safety issue , You can easily create bugs. One of misconception Java programmer has is converting date in different timezone. Actually Date in Java is always in GMT and it represent number of millisecond since 01-01-1970 00:00 and when we print Date, it calls toString method and display date time information in local timezone. If we want to display date in different timezone we can do this by using SimpleDateFormat class in Java. In this Java tutorial we will see couple of example of displaying date in IST and PST timezone.
Here is complete code example of printing Date in different timezone in Java. In order to get timezone we use TimeZone.getTimeZone(String id), where we pass timezone id e.g. America/Los_Angeles, you can also pass three digit timezone abbreviation e.g. PST but that is deprecated and not advised. As short form can be confusing and may represent two different timezone in different locale. Also its worth remembering that SimpleDateFormat is not thread-safe, So don't store it on static field or share among different threads. But at same time if you want to share SimpleDateformat safely in multi-threading environment you can use ThreadLocal variable to make SimpleDateFormat thread-safe.
Finally important notes
Here is some of the worth remembering points while converting timezones for Date in Java :
1) Date always represent millisecond passed since 01-01-1970 00:00 GMT irrespective of which timezone you are displaying e.g. IST or PST.
2) Try not to use abbreviated timezone ID e.g. IST or PST to retrieve timezone in Java. That is deprecated and future version of Java may not support it. Always use full String Timezone id e.g. America/Los_Angeles.
3) Always include timezone in your DateFormat while converting Date to String in Java. you can do this by using z or Z. z shown timezone in abbreviated format e.g. IST or PST while Z shows relative to GMT e.g. GMT +8.00 or GMT -8.00. This helps to see on which timezone date is displaying.
4) Becareful while execuing df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); as if String either short form or full name or custome id e.g. GMT -8:00, representing Timezone is not valid, TimeZone.getDefaultZone() returns GMT and doesn't throw any Error or Exception and you may see incorrect dates. That's when displaying timezone along with formatted date using "z" helps.
That's all on How to display date and time in multiple timezone in Java. Java Date and Time API are powerful and has sufficient tool to display date in various timezone but has some thread-safety and design issues. There are alternatives available as well e.g. JODA Date and Time API but I prefer to stuck with core Java library and hoping new Java 8 Date and Time API will resolve all design and thread-safety issue.
Other Java Date and Time tutorials you may like