In this post we will understand and learn to display date in Multiple Timezone using example. In real time projects we used this concpet to set multiple date format in their website. As a Java developer must be strong knowledge on this concept,and also should know how to apply this topic in real time projects.
While working in global Java application its quite common to display dates in different timezone, classical exampleis 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...
We can use SimpleDateFormat class to display date in multiple Timezone in Java 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-970 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.
We can use SimpleDateFormat class to display date in multiple Timezone in Java 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-970 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.
Now we will demonstrate this concept using example of displaying date in IST and PST timezone.
Example :
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
*
* Java program to display 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 same date in two timezones, Indian time (IST)
* and PST, America/Los_Angeles .
*
* @author http://learnprogramingbyluckysir.blogspot.com
*/
public class TimezoneConversionExample {
public static void main(String args[]) {
//capturing today's date
Date today = new Date();
Date today = new Date();
//displaying this date on IST timezone
DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm:SS z");
DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm:SS z");
df.setTimeZone(TimeZone.getTimeZone("Asia/Mumbai"));
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"));
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String PST = df.format(today);
System.out.println("Date in PST Timezone : " + PST);
}
}
}
Output:
Example 2:
This program will display current date according to UK,US and ITALY specific ways
import java.text.*;
import java.util.*;
class DateFormatDemo
{
public static void main(String[] args)
{
DateFormat UK = DateFormat.getDateInstance(0,Locale.UK);
DateFormat US = DateFormat.getDateInstance(0,Locale.US);
DateFormat ITALY = DateFormat.getDateInstance(0,Locale.ITALY);
System.out.println("UK Style is " + UK.format(new Date()));
System.out.println("US Style is " + US.format(new Date()));
System.out.println("ITALY Style is " + ITALY.format(new Date()));
}
}
Result:
Here is some of the worth remembering points while converting timezones for Date in Java :
1) Date always represent millisecond passed since 01-01-2015 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) Becarefulwhileexecuingdf.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.
Good explanation on timezone formatting
ReplyDelete