We can use SimpleDateFormat class to display a date in multiple Timezone in Java. While working in a global Java application it's quite common to display dates in the different time zone, 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, the Date and Time API in Java is quite tricky and until you have a good understanding of Date and Time classes and methods like Calendar, SimpleDateFormat, and thread-safety issues, You can easily create bugs.
One of misconception Java programmer has is converting a date in the different timezone. Actually, Date in Java is always in GMT and it represents a number of milliseconds since 01-01-1970 00:00 and when we print Date, it calls the toString method and displays date-time information in the local timezone.
If we want to display date at a different timezone we can do this by using SimpleDateFormat class in Java. In this Java tutorial, we will see a couple of examples of displaying a date in the IST and PST timezone.
If we want to display date at a different timezone we can do this by using SimpleDateFormat class in Java. In this Java tutorial, we will see a couple of examples of displaying a date in the IST and PST timezone.
How to Print Date in different Timezone in Java - Example
Here is a complete code example of printing Date at 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 time zones in a different locale. Also, it's worth remembering that SimpleDateFormat is not thread-safe So doesn't store it in a static field or share it among different threads.
But at the same time if you want to share SimpleDateformat safely in a multi-threading environment you can use a ThreadLocal variable to make SimpleDateFormat thread-safe.
As short form can be confusing and may represent two different time zones in a different locale. Also, it's worth remembering that SimpleDateFormat is not thread-safe So doesn't store it in a static field or share it among different threads.
But at the same time if you want to share SimpleDateformat safely in a multi-threading environment you can use a ThreadLocal variable to make SimpleDateFormat thread-safe.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
*
* Java program to display a date in different timezone in Java. Internally Java
* stores date as millisecond passed since 01-01-1970 00:00 GMT, which can be
* converted and display in any timezone using SimpleDateFormat in Java. In
* this Java program we will display the same date in two timezones, Indian time (IST)
* and PST, America/Los_Angeles .
*
* @author http://java67.blogspot.com
*/
public class TimezoneConversionExample {
public static void main(String args[]) {
//capturing today's date
Date today = new Date();
//displaying this date on IST timezone
DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm:SS z");
df.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String IST = df.format(today);
System.out.println("Date in Indian Timezone (IST) : " + IST);
//dispalying date on PST timezone
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String PST = df.format(today);
System.out.println("Date in PST Timezone : " + PST);
}
}
Output:
Date in Indian Timezone (IST) : 06-11-12 07:39:61 IST
Date in PST Timezone : 05-11-12 18:09:61 PST
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
*
* Java program to display a date in different timezone in Java. Internally Java
* stores date as millisecond passed since 01-01-1970 00:00 GMT, which can be
* converted and display in any timezone using SimpleDateFormat in Java. In
* this Java program we will display the same date in two timezones, Indian time (IST)
* and PST, America/Los_Angeles .
*
* @author http://java67.blogspot.com
*/
public class TimezoneConversionExample {
public static void main(String args[]) {
//capturing today's date
Date today = new Date();
//displaying this date on IST timezone
DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm:SS z");
df.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String IST = df.format(today);
System.out.println("Date in Indian Timezone (IST) : " + IST);
//dispalying date on PST timezone
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String PST = df.format(today);
System.out.println("Date in PST Timezone : " + PST);
}
}
Output:
Date in Indian Timezone (IST) : 06-11-12 07:39:61 IST
Date in PST Timezone : 05-11-12 18:09:61 PST
Finally important notes
Here are some of the worth remembering points while converting timezones for Date in Java :
1) The date always represents millisecond passed since 01-01-1970 00:00 GMT irrespective of which timezone you are displaying like IST or PST. You also see these JDBC courses to learn more about JDBC fundamentals like timezones.
2) Try not to use abbreviated timezone ID like IST or PST to retrieve timezone in Java. That is deprecated and future versions 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 which timezone date is displaying.
4) Be careful while executing df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); as if String either short form or full name or custom 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 time zone along with formatted date using "z" helps.
That's all on How to display date and time in multiple timezones in Java. Java Date and Time API are powerful and have sufficient tools to display the 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 stick with the core Java library and hoping the new Java 8 Date and Time API will resolve all design and thread-safety issues.
Other Java Date and Time tutorials you may like
i wrote ths on fly hhahaha im sorry :) goood template thoughh for a calender print
ReplyDelete//this code prints the days in month w a name how many skip days in between the months and total days
public class PrintCal{
public static final int DAYINWEEK = 7;
public static final int START = 0;
public static void main(String[] args) {
//month(monthname, totald, skip)
}
public static void month(String monthp, int totald int skip) {
System.out.println(monthp);
System.out.println ("Sun Mon Tue Wed Thu Fri Sat");
for (int i = START;i<=skip; ++i) {
System.out.print ("");
} for (int k = START + 1; k <= DAYINWEEK - skip; ++k) {
System.out.print (k + "\n");
} for (int f = START +1; f <= ((totald + skip)) - 1; f++) {
for (int g =8-skip+(7*g-7); f<=8-skip+6+(7*f-7);++g){
System.out.print(g+ "\n");
}
}for (int i=7-skip+6+(7*(skip + totald)/7)-1)-7+1);i <= totald; ++i) {
System.out.print (i);
}
}
}
GOOD
ReplyDeleteThanks for examples and notes but I noticed that you should use lower case "ss" for seconds. Using upper case "S" shows the milliseconds part of the Date object.
ReplyDeleteThanks for pointing that Anonymous, may be you are right, I will double check the Java docs and update it.
ReplyDelete