Java TimeZone Examples
TimeZone is an abstract class represents a time zone offset, and also figures out daylight savings. The java.util.TimeZone class is used in conjunction with the java.util.Calendar class.
Creating a TimeZone Instance
There are three ways to obtain a TimeZone instance:
- static TimeZone getDefault(): Gets the default TimeZone of the Java virtual machine.
- static TimeZone getTimeZone(String ID): Gets the TimeZone for the given ID.
- static TimeZone getTimeZone(ZoneId zoneId): Gets the TimeZone for the given zoneId.
import java.time.ZoneId;
import java.util.TimeZone;
public class TimeZoneInitExample {
static void printTimeZone(TimeZone tz) {
System.out.println("ID: " + tz.getID() + ", DisplayName: " + tz.getDisplayName());
}
public static void main(String[] args) {
TimeZone tz1 = TimeZone.getDefault();
printTimeZone(tz1);
TimeZone tz2 = TimeZone.getTimeZone("Australia/Sydney");
printTimeZone(tz2);
TimeZone tz3 = TimeZone.getTimeZone(ZoneId.systemDefault());
printTimeZone(tz3);
TimeZone tz4 = TimeZone.getTimeZone(ZoneId.of("Asia/Jakarta"));
printTimeZone(tz4);
}
}
ID: Asia/Singapore, DisplayName: Singapore Time ID: Australia/Sydney, DisplayName: Australian Eastern Standard Time (New South Wales) ID: Asia/Singapore, DisplayName: Singapore Time ID: Asia/Jakarta, DisplayName: West Indonesia Time
The result of above program may vary, because you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, my program which running in Singapore, getDefault creates a TimeZone object based on Singapore Standard Time.
We can also get a TimeZone using getTimeZone along with a time zone ID. For instance, the time zone ID for Jakarta (Indonesia) Time zone is "Asia/Jakarta".
Get a TimeZone from a Calendar
We can get the TimeZone, and set TimeZone from/to a Calendar:
- TimeZone getTimeZone(): Gets the time zone.
- void setTimeZone(TimeZone value): Sets the time zone with the given time zone value.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class TimeZoneCalendarExample {
static void printCalendarTimeZone(Calendar calendar) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
TimeZone tz = calendar.getTimeZone();
System.out.printf("Calendar's Date/Time: %s\n", sdf.format(calendar.getTime()));
System.out.printf("Time Zone ID : %s\n", tz.getID());
System.out.printf("Time Zone Name : %s\n", tz.getDisplayName());
System.out.println();
}
public static void main(String[] args) {
Calendar cal1 = Calendar.getInstance();
cal1.set(1980, 3, 9, 20, 30, 40);
printCalendarTimeZone(cal1);
TimeZone tz = TimeZone.getTimeZone("Asia/Jakarta");
Calendar cal2 = Calendar.getInstance();
cal2.setTimeZone(tz);
cal2.set(1980, 3, 9, 20, 30, 40);
printCalendarTimeZone(cal2);
}
}
Calendar's Date/Time: 09/04/1980 20:30:40 Time Zone ID : Asia/Singapore Time Zone Name : Singapore Time Calendar's Date/Time: 09/04/1980 21:00:40 Time Zone ID : Asia/Jakarta Time Zone Name : West Indonesia Time
Convert Between Time Zones
We can use Calendar's setTimeZone() to convert between TimeZone
import java.util.Calendar;
import java.util.TimeZone;
public class TimeZoneConvertExample {
static void printCalendarTimeZone(Calendar calendar) {
TimeZone tz = calendar.getTimeZone();
System.out.printf("Time In Millis: %s\n", calendar.getTimeInMillis());
System.out.printf("Hour : %s\n", calendar.get(Calendar.HOUR_OF_DAY));
System.out.printf("Time Zone ID : %s\n", tz.getID());
System.out.printf("Time Zone Name: %s\n", tz.getDisplayName());
System.out.println();
}
public static void main(String[] args) {
TimeZone tz1 = TimeZone.getTimeZone("Asia/Jakarta");
TimeZone tz2 = TimeZone.getTimeZone("Asia/Tokyo");
Calendar cal = Calendar.getInstance(tz1);
printCalendarTimeZone(cal);
cal.setTimeZone(tz2);
printCalendarTimeZone(cal);
}
}
Time In Millis: 1566100076793 Hour : 10 Time Zone ID : Asia/Jakarta Time Zone Name: West Indonesia Time Time In Millis: 1566100076793 Hour : 12 Time Zone ID : Asia/Tokyo Time Zone Name: Japan Standard Time
There are no changes in milliseconds in both time zones, but that the hour of day has changed from 10 to 12 (Tokyo time is 2 hours ahead of Jakarta).
Time Zone Names and ID's
From examples above, we already use getDisplayName() to get display names from a TimeZone and getID() to get ID of a TimeZone object.
- String getDisplayName(): Returns a long standard time name of this TimeZone suitable for presentation to the user in the default locale.
- String getDisplayName(boolean daylight, int style): Returns a name in the specified style of this TimeZone suitable for presentation to the user in the default locale.
- String getDisplayName(boolean daylight, int style,Locale locale): Returns a name in the specified style of this TimeZone suitable for presentation to the user in the specified locale.
- String getDisplayName(Locale locale): Returns a long standard time name of this TimeZone suitable for presentation to the user in the specified locale.
- String getID(): Gets the ID of this time zone.
And setID(String ID) to set the time zone ID.
import java.util.Locale;
import java.util.TimeZone;
public class TimeZoneGetSetIDExample {
static void printTimeZone(TimeZone tz) {
System.out.println("ID: " + tz.getID() + ", DisplayName: " + tz.getDisplayName(Locale.ENGLISH));
}
public static void main(String[] args) {
TimeZone tz = TimeZone.getDefault();
printTimeZone(tz);
tz.setID("Asia/Jakarta");
printTimeZone(tz);
}
}
ID: Asia/Singapore, DisplayName: Singapore Time ID: Asia/Jakarta, DisplayName: West Indonesia Time
To get a list of the available time zone ids in the TimeZone class, we can use method getAvailableIDs(...):
- static String[] getAvailableIDs(): Gets all the available IDs supported.
- static String[] getAvailableIDs(int rawOffset): Gets the available IDs according to the given time zone offset in milliseconds.
import java.util.TimeZone;
public class TimeZoneGetAvailableIDsExample {
public static void main(String[] args) {
// getting available Ids
String[] availId = TimeZone.getAvailableIDs();
for (String availId1 : availId) {
System.out.println(availId1);
}
}
}
Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara ... ... Purposely truncated ... PNT PRT PST SST VST
Time Zone Offsets
- abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds): Gets the time zone offset, for current date, modified in case of daylight savings.
- int getOffset(long date): Returns the offset of this time zone from UTC at the specified date.
- abstract int getRawOffset(): Returns the amount of time in milliseconds to add to UTC to get standard time in this time zone.
and setRawOffset(int offsetMillis) to set the base time zone offset to GMT.
import java.util.Calendar;
import java.util.TimeZone;
public class TimeZoneOffsetExample {
public static void main(String[] args) {
TimeZone tz = TimeZone.getDefault();
long now = System.currentTimeMillis();
System.out.println("CurrentTimeMillis: " + now);
System.out.println("TimeZone offset : " + tz.getOffset(now));
System.out.println("Raw Offset value : " + tz.getRawOffset());
Calendar cal1 = Calendar.getInstance(tz);
System.out.println("Hour : " + cal1.get(Calendar.HOUR_OF_DAY));
// set raw offset
tz.setRawOffset(3600000);
// checking offset value
System.out.println("Raw Offset value now: " + tz.getRawOffset());
Calendar cal2 = Calendar.getInstance(tz);
System.out.println("Hour : " + cal2.get(Calendar.HOUR_OF_DAY));
}
}
CurrentTimeMillis: 1566099508146 TimeZone offset : 28800000 Raw Offset value : 28800000 Hour : 11 Raw Offset value now: 3600000 Hour : 4
If you use a Calendar you can still use the TimeZone class. In the Java 8, time zones represented by the java.time.ZoneId class. Use ZoneId class if we are working with Java 8 date time classes (like the ZonedDateTime class).