Java : TimeZone (タイムゾーン) - API使用例
TimeZone (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
注意
- TimeZone クラスは、非推奨(@Deprecated) にこそなっていませんが、古い API です。
代わりに、java.time パッケージにある ZoneId や ZoneOffset などの新しい API を使うことをおすすめします。
概要
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);
フィールド
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
コンストラクタ
TimeZone ()
サブクラス用です。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
メソッド
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);
}
// 結果
// ↓
//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);
}
// 結果
// ↓
//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()); // ja-JP
final var zone1 = TimeZone.getDefault();
System.out.println(zone1.toZoneId()); // Asia/Tokyo
System.out.println(zone1.getDisplayName()); // 日本標準時
System.out.println(zone1.getDisplayName(Locale.US)); // Japan Standard Time
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
final var zone2 = TimeZone.getDefault();
System.out.println(zone2.toZoneId()); // America/Los_Angeles
System.out.println(zone2.getDisplayName()); // アメリカ太平洋標準時
System.out.println(zone2.getDisplayName(Locale.US)); // Pacific Standard Time
final String getDisplayName ()
System.out.println(Locale.getDefault().toLanguageTag()); // ja-JP
final var zone1 = TimeZone.getDefault();
System.out.println(zone1.toZoneId()); // Asia/Tokyo
System.out.println(zone1.getDisplayName()); // 日本標準時
System.out.println(zone1.getDisplayName(Locale.US)); // Japan Standard Time
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
final var zone2 = TimeZone.getDefault();
System.out.println(zone2.toZoneId()); // America/Los_Angeles
System.out.println(zone2.getDisplayName()); // アメリカ太平洋標準時
System.out.println(zone2.getDisplayName(Locale.US)); // Pacific Standard Time
final String getDisplayName (boolean daylight, int style)
System.out.println(Locale.getDefault().toLanguageTag()); // ja-JP
final var zone = TimeZone.getTimeZone("America/Los_Angeles");
{
final var name1 = zone.getDisplayName(false, TimeZone.LONG);
System.out.println(name1); // アメリカ太平洋標準時
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); // アメリカ太平洋夏時間
final var name2 = zone.getDisplayName(true, TimeZone.SHORT);
System.out.println(name2); // PDT
}
String getDisplayName (boolean daylight, int style, Locale locale)
各パラメータについては、下記の使用例をご参照ください。
- daylight, style パラメータ : getDisplayName(boolean daylight, int style)
- locale パラメータ : getDisplayName(Locale locale)
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()); // ja-JP
final var zone1 = TimeZone.getDefault();
System.out.println(zone1.toZoneId()); // Asia/Tokyo
System.out.println(zone1.getDisplayName()); // 日本標準時
System.out.println(zone1.getDisplayName(Locale.US)); // Japan Standard Time
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
final var zone2 = TimeZone.getDefault();
System.out.println(zone2.toZoneId()); // America/Los_Angeles
System.out.println(zone2.getDisplayName()); // アメリカ太平洋標準時
System.out.println(zone2.getDisplayName(Locale.US)); // Pacific Standard Time
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
関連記事
- 日付・時刻の基本
- Date, CalendarではなくLocalDateTime, ZonedDateTimeを使おう
- 文字列と日付・時刻の変換
- 日付と時刻、曜日の計算
- 現在時刻(日時)の取得いろいろ
- 現在の曜日(DayOfWeek)を取得
- ZoneIdとZoneOffsetの違い
- API 使用例
- 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 (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)