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
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
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
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 ()
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 ()
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 ()
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)
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 ()
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 ()
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)
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)
Please see :
- getDisplayName(boolean daylight, int style) for daylight, style parameters.
- getDisplayName(Locale locale) for locale parameter.
final String getDisplayName (Locale 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 ()
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 ()
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)
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)
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 ()
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)
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)
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)
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)
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 ()
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)
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)
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)
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 ()
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 ()
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
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset