Java - Date And Time In Different Time Zones
Chapter:
Date and Time
Last Updated:
23-09-2016 07:43:15 UTC
Program:
/* ............... START ............... */
import java.util.*;
import java.text.*;
public class JavaDateAndTimeInDifferentTimeZone {
public static void main(String[] args) {
TimeZone timeZone = TimeZone.getTimeZone("EST");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DATE, 31);
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.HOUR, 23);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 52);
Date date = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("zzz yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
System.out.println(sdf.format(date));
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Luxembourg"));
System.out.println(sdf.format(date));
}
}
/* ............... END ............... */
Output
GST 2014-01-01 08:45:52
JST 2014-01-01 13:45:52
CET 2014-01-01 05:45:52
Notes:
-
The java.util.Date has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z. But, if you print the Date object directly, the Date object will be always printed with the default system time zone. Check the Date.toString() source code.
Tags
Date And Time In Different Time Zones, Java, Date And Time