Java : TimeZone with Examples

TimeZone (Java SE 21 & JDK 21) with Examples.
You will find code examples on most TimeZone methods.

Warning :

  • The TimeZone class is a legacy API. It is recommended to use the newer APIs such as ZoneId and ZoneOffset in the java.time package instead.


Summary

TimeZone represents a time zone offset, and also figures out daylight savings.

Class diagram

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getID()); // America/Los_Angeles
System.out.println(zone.getDisplayName(Locale.US)); // Pacific Standard Time

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setTimeOfDay(14, 30, 59)
        .setTimeZone(zone)
        .build();

// 2100-01-01 14:30:59 -0800 PST
System.out.printf("%tF %tT %tz %tZ%n", calendar, calendar, calendar, calendar);

Fields

static final int LONG

A style specifier for getDisplayName() indicating a long name, such as "Pacific Standard Time."

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

final var name1 = zone.getDisplayName(false, TimeZone.LONG, Locale.US);
System.out.println(name1); // Pacific Standard Time

final var name2 = zone.getDisplayName(false, TimeZone.SHORT, Locale.US);
System.out.println(name2); // PST

static final int SHORT

A style specifier for getDisplayName() indicating a short name, such as "PST."

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

final var name1 = zone.getDisplayName(false, TimeZone.LONG, Locale.US);
System.out.println(name1); // Pacific Standard Time

final var name2 = zone.getDisplayName(false, TimeZone.SHORT, Locale.US);
System.out.println(name2); // PST

Constructors

TimeZone ()

Sole constructor.

This method is for a subclass. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

Methods

Object clone ()

Creates a copy of this TimeZone.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getID()); // America/Los_Angeles

if (zone.clone() instanceof TimeZone cloned) {
    System.out.println(cloned.getID()); // America/Los_Angeles
}

static String[] getAvailableIDs ()

Gets all the available IDs supported.

final var ids = TimeZone.getAvailableIDs();
for (final var id : ids) {
    System.out.println(id);
}

// Result
// ↓
//Africa/Abidjan
//Africa/Accra
//Africa/Addis_Ababa
//Africa/Algiers
//Africa/Asmara
//Africa/Asmera
//Africa/Bamako
//Africa/Bangui
//...

static String[] getAvailableIDs (int rawOffset)

Gets the available IDs according to the given time zone offset in milliseconds.

final var ids = TimeZone.getAvailableIDs(-28800000);
for (final var id : ids) {
    System.out.println(id);
}

// Result
// ↓
//America/Ensenada
//America/Los_Angeles
//America/Santa_Isabel
//America/Tijuana
//America/Vancouver
//Canada/Pacific
//Etc/GMT+8
//Mexico/BajaNorte
//PST
//PST8PDT
//Pacific/Pitcairn
//SystemV/PST8
//SystemV/PST8PDT
//US/Pacific

static TimeZone getDefault ()

Gets the default TimeZone of the Java virtual machine.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var zone1 = TimeZone.getDefault();
System.out.println(zone1.toZoneId()); // America/Los_Angeles
System.out.println(zone1.getDisplayName()); // Pacific Standard Time
System.out.println(zone1.getDisplayName(Locale.JAPAN)); // アメリカ太平洋標準時

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

final var zone2 = TimeZone.getDefault();
System.out.println(zone2.toZoneId()); // UTC
System.out.println(zone2.getDisplayName()); // Coordinated Universal Time
System.out.println(zone2.getDisplayName(Locale.JAPAN)); // 協定世界時

final String getDisplayName ()

Returns a long standard time name of this TimeZone suitable for presentation to the user in the default locale.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var zone1 = TimeZone.getDefault();
System.out.println(zone1.toZoneId()); // America/Los_Angeles
System.out.println(zone1.getDisplayName()); // Pacific Standard Time
System.out.println(zone1.getDisplayName(Locale.JAPAN)); // アメリカ太平洋標準時

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

final var zone2 = TimeZone.getDefault();
System.out.println(zone2.toZoneId()); // UTC
System.out.println(zone2.getDisplayName()); // Coordinated Universal Time
System.out.println(zone2.getDisplayName(Locale.JAPAN)); // 協定世界時

final 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.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

{
    final var name1 = zone.getDisplayName(false, TimeZone.LONG);
    System.out.println(name1); // Pacific Standard Time

    final var name2 = zone.getDisplayName(false, TimeZone.SHORT);
    System.out.println(name2); // PST
}
{
    final var name1 = zone.getDisplayName(true, TimeZone.LONG);
    System.out.println(name1); // Pacific Daylight Time

    final var name2 = zone.getDisplayName(true, TimeZone.SHORT);
    System.out.println(name2); // PDT
}

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.

Please see :

final String getDisplayName (Locale locale)

Returns a long standard time name of this TimeZone suitable for presentation to the user in the specified locale.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getDisplayName(Locale.US)); // Pacific Standard Time
System.out.println(zone.getDisplayName(Locale.JAPAN)); // アメリカ太平洋標準時
final var zone = TimeZone.getTimeZone("Asia/Tokyo");
System.out.println(zone.getDisplayName(Locale.US)); // Japan Standard Time
System.out.println(zone.getDisplayName(Locale.JAPAN)); // 日本標準時

int getDSTSavings ()

Returns the amount of time to be added to local standard time to get local wall clock time.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

System.out.println(zone.useDaylightTime()); // true
System.out.println(zone.getDSTSavings()); // 3600000
final var zone = TimeZone.getTimeZone("Asia/Tokyo");

System.out.println(zone.useDaylightTime()); // false
System.out.println(zone.getDSTSavings()); // 0

String getID ()

Gets the ID of this time zone.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getID()); // America/Los_Angeles
System.out.println(zone.getRawOffset()); // -28800000

zone.setID("UTC");
System.out.println(zone.getID()); // UTC
System.out.println(zone.getRawOffset()); // -28800000

zone.setRawOffset(0);
System.out.println(zone.getRawOffset()); // 0

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.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

final var offset1 = zone.getOffset(1, 2100, 0, 3, 1, 0);
System.out.println(offset1); // -28800000

System.out.println(zone.useDaylightTime()); // true

final var offset2 = zone.getOffset(1, 2100, 7, 1, 1, 0);
System.out.println(offset2); // -25200000
final var zone = TimeZone.getTimeZone("UTC");

final var offset = zone.getOffset(1, 2100, 0, 3, 1, 0);
System.out.println(offset); // 0

int getOffset (long date)

Returns the offset of this time zone from UTC at the specified date.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setTimeZone(TimeZone.getTimeZone("UTC"))
        .build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

final var date1 = calendar.getTimeInMillis();
System.out.println(date1); // 4102444800000

final var offset1 = zone.getOffset(date1);
System.out.println(offset1); // -28800000

System.out.println(zone.useDaylightTime()); // true

calendar.set(Calendar.MONTH, Calendar.AUGUST);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-01 00:00:00

final var date2 = calendar.getTimeInMillis();
System.out.println(date2); // 4120761600000

final var offset2 = zone.getOffset(date2);
System.out.println(offset2); // -25200000
final var zone = TimeZone.getTimeZone("UTC");

final var offset = zone.getOffset(4102444800000L);
System.out.println(offset); // 0

abstract int getRawOffset ()

Returns the amount of time in milliseconds to add to UTC to get standard time in this time zone.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getID()); // America/Los_Angeles
System.out.println(zone.getRawOffset()); // -28800000
final var zone = TimeZone.getTimeZone("Asia/Tokyo");
System.out.println(zone.getID()); // Asia/Tokyo
System.out.println(zone.getRawOffset()); // 32400000
final var zone = TimeZone.getTimeZone("UTC");
System.out.println(zone.getID()); // UTC
System.out.println(zone.getRawOffset()); // 0

static TimeZone getTimeZone (String ID)

Gets the TimeZone for the given ID.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getID()); // America/Los_Angeles
System.out.println(zone.getRawOffset()); // -28800000
final var zone = TimeZone.getTimeZone("Asia/Tokyo");
System.out.println(zone.getID()); // Asia/Tokyo
System.out.println(zone.getRawOffset()); // 32400000
final var zone = TimeZone.getTimeZone("UTC");
System.out.println(zone.getID()); // UTC
System.out.println(zone.getRawOffset()); // 0

static TimeZone getTimeZone (ZoneId zoneId)

Gets the TimeZone for the given zoneId.

final var zone = TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles"));
System.out.println(zone.getID()); // America/Los_Angeles
System.out.println(zone.getRawOffset()); // -28800000
final var zone = TimeZone.getTimeZone(ZoneId.of("Asia/Tokyo"));
System.out.println(zone.getID()); // Asia/Tokyo
System.out.println(zone.getRawOffset()); // 32400000
final var zone = TimeZone.getTimeZone(ZoneId.of("UTC"));
System.out.println(zone.getID()); // UTC
System.out.println(zone.getRawOffset()); // 0

boolean hasSameRules (TimeZone other)

Returns true if this zone has the same rule and offset as another zone.

final var zone1 = TimeZone.getTimeZone("UTC");
final var zone2 = TimeZone.getTimeZone("America/Los_Angeles");

System.out.println(zone1.hasSameRules(zone2)); // false

final var zone3 = TimeZone.getTimeZone(ZoneId.of("GMT+0"));

System.out.println(zone1.hasSameRules(zone3)); // true

abstract boolean inDaylightTime (Date date)

Queries if the given date is in Daylight Saving Time in this time zone.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setTimeZone(TimeZone.getTimeZone("UTC"))
        .build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

System.out.println(zone.inDaylightTime(calendar.getTime())); // false

calendar.set(Calendar.MONTH, Calendar.AUGUST);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-01 00:00:00

System.out.println(zone.inDaylightTime(calendar.getTime())); // true

boolean observesDaylightTime ()

Returns true if this TimeZone is currently in Daylight Saving Time, or if a transition from Standard Time to Daylight Saving Time occurs at any future time.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.observesDaylightTime()); // true
final var zone = TimeZone.getTimeZone("UTC");
System.out.println(zone.observesDaylightTime()); // false

static void setDefault (TimeZone zone)

Sets the TimeZone that is returned by the getDefault method.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var zone1 = TimeZone.getDefault();
System.out.println(zone1.toZoneId()); // America/Los_Angeles
System.out.println(zone1.getDisplayName()); // Pacific Standard Time
System.out.println(zone1.getDisplayName(Locale.JAPAN)); // アメリカ太平洋標準時

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

final var zone2 = TimeZone.getDefault();
System.out.println(zone2.toZoneId()); // UTC
System.out.println(zone2.getDisplayName()); // Coordinated Universal Time
System.out.println(zone2.getDisplayName(Locale.JAPAN)); // 協定世界時

void setID (String ID)

Sets the time zone ID.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getID()); // America/Los_Angeles
System.out.println(zone.getRawOffset()); // -28800000

zone.setID("UTC");
System.out.println(zone.getID()); // UTC
System.out.println(zone.getRawOffset()); // -28800000

zone.setRawOffset(0);
System.out.println(zone.getRawOffset()); // 0

abstract void setRawOffset (int offsetMillis)

Sets the base time zone offset to GMT.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(zone.getRawOffset()); // -28800000

zone.setRawOffset(0);

System.out.println(zone.getRawOffset()); // 0

ZoneId toZoneId ()

Converts this TimeZone object to a ZoneId.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

final var zoneId = zone.toZoneId();
System.out.println(zoneId); // America/Los_Angeles
final var zone = TimeZone.getTimeZone("UTC");

final var zoneId = zone.toZoneId();
System.out.println(zoneId); // UTC

abstract boolean useDaylightTime ()

Queries if this TimeZone uses Daylight Saving Time.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

System.out.println(zone.useDaylightTime()); // true
System.out.println(zone.getDSTSavings()); // 3600000
final var zone = TimeZone.getTimeZone("Asia/Tokyo");

System.out.println(zone.useDaylightTime()); // false
System.out.println(zone.getDSTSavings()); // 0

Related posts

To top of page