Java : Clock (時計) - API使用例
Clock (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
Clock クラスは、現在時刻 を取得するために利用するクラスです。
単純に現在時刻を返すだけでなく、
- 固定の時刻を返す
- 現在時刻の xx 時間後を返す
といったことも可能です。
ユニットテストなどで
- 現在時刻を特定の時刻にして実行したい
というケースで便利ですね。
System.out.println(ZoneId.systemDefault()); // Asia/Tokyo
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.943910300Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:09:39.945908600Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.945908600Z
}
{
// 固定で 2100-12-31 00:00 を返します。
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
コンストラクタ
Clock ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
メソッド
boolean equals (Object obj)
final var clock1 = Clock.systemUTC();
final var clock2 = Clock.systemUTC();
final var clock3 = Clock.systemDefaultZone();
System.out.println(clock1.equals(clock2)); // true
System.out.println(clock1.equals(clock3)); // false
static Clock fixed (Instant fixedInstant, ZoneId zone)
System.out.println(ZoneId.systemDefault()); // Asia/Tokyo
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.943910300Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:09:39.945908600Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.945908600Z
}
{
// 固定で 2100-12-31 00:00 を返します。
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
abstract ZoneId getZone ()
System.out.println(ZoneId.systemDefault()); // Asia/Tokyo
final var clock1 = Clock.systemDefaultZone();
System.out.println(clock1.getZone()); // Asia/Tokyo
final var clock2 = Clock.systemUTC();
System.out.println(clock2.getZone()); // Z
final var zone = ZoneId.of("America/Los_Angeles");
final var clock3 = Clock.system(zone);
System.out.println(clock3.getZone()); // America/Los_Angeles
int hashCode ()
final var clock1 = Clock.systemUTC();
System.out.println(clock1.hashCode()); // 1
final var clock2 = Clock.system(ZoneId.of("America/Los_Angeles"));
System.out.println(clock2.hashCode()); // -1536188512
final var clock3 = Clock.system(ZoneId.of("Asia/Tokyo"));
System.out.println(clock3.hashCode()); // -1660747038
abstract Instant instant ()
final var clock = Clock.systemUTC();
for (int i = 0; i < 4; i++) {
final var instant = clock.instant();
final var millis = clock.millis();
System.out.printf("%s (millis = %d)%n", instant, millis);
TimeUnit.SECONDS.sleep(10);
}
// 結果
// ↓
//2024-07-07T06:10:53.794095300Z (millis = 1720332653794)
//2024-07-07T06:11:03.811190700Z (millis = 1720332663811)
//2024-07-07T06:11:13.827016100Z (millis = 1720332673827)
//2024-07-07T06:11:23.841881800Z (millis = 1720332683841)
long millis ()
final var clock = Clock.systemUTC();
for (int i = 0; i < 4; i++) {
final var instant = clock.instant();
final var millis = clock.millis();
System.out.printf("%s (millis = %d)%n", instant, millis);
TimeUnit.SECONDS.sleep(10);
}
// 結果
// ↓
//2024-07-07T06:10:53.794095300Z (millis = 1720332653794)
//2024-07-07T06:11:03.811190700Z (millis = 1720332663811)
//2024-07-07T06:11:13.827016100Z (millis = 1720332673827)
//2024-07-07T06:11:23.841881800Z (millis = 1720332683841)
static Clock offset (Clock baseClock, Duration offsetDuration)
final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z
final var baseClock = Clock.fixed(instant, ZoneOffset.UTC);
System.out.println(baseClock.instant()); // 2100-01-01T00:00:00Z
final var clock1 = Clock.offset(baseClock, Duration.ofDays(-1));
System.out.println(clock1.instant()); // 2099-12-31T00:00:00Z
final var clock2 = Clock.offset(baseClock, Duration.ofHours(14).plusMinutes(30));
System.out.println(clock2.instant()); // 2100-01-01T14:30:00Z
static Clock system (ZoneId zone)
System.out.println(ZoneId.systemDefault()); // Asia/Tokyo
final var clock1 = Clock.systemDefaultZone();
System.out.println(clock1.getZone()); // Asia/Tokyo
final var clock2 = Clock.systemUTC();
System.out.println(clock2.getZone()); // Z
final var zone = ZoneId.of("America/Los_Angeles");
final var clock3 = Clock.system(zone);
System.out.println(clock3.getZone()); // America/Los_Angeles
static Clock systemDefaultZone ()
System.out.println(ZoneId.systemDefault()); // Asia/Tokyo
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.943910300Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:09:39.945908600Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.945908600Z
}
{
// 固定で 2100-12-31 00:00 を返します。
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
static Clock systemUTC ()
System.out.println(ZoneId.systemDefault()); // Asia/Tokyo
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T15:09:39.943910300+09:00[Asia/Tokyo]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.943910300Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:09:39.945908600Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:09:39.945908600Z
}
{
// 固定で 2100-12-31 00:00 を返します。
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
static Clock tick (Clock baseClock, Duration tickDuration)
final var baseClock = Clock.systemUTC();
final var clock = Clock.tick(baseClock, Duration.ofSeconds(2));
for (int i = 0; i < 8; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.SECONDS.sleep(1);
}
// 結果
// ↓
//2024-07-07T06:13:14Z
//2024-07-07T06:13:16Z
//2024-07-07T06:13:16Z
//2024-07-07T06:13:18Z
//2024-07-07T06:13:18Z
//2024-07-07T06:13:20Z
//2024-07-07T06:13:20Z
//2024-07-07T06:13:22Z
static Clock tickMillis (ZoneId zone)
final var clock = Clock.tickMillis(ZoneOffset.UTC);
for (int i = 0; i < 8; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.MILLISECONDS.sleep(1);
}
// 結果
// ↓
//2024-07-07T06:13:40.483Z
//2024-07-07T06:13:40.489Z
//2024-07-07T06:13:40.491Z
//2024-07-07T06:13:40.493Z
//2024-07-07T06:13:40.495Z
//2024-07-07T06:13:40.497Z
//2024-07-07T06:13:40.499Z
//2024-07-07T06:13:40.502Z
static Clock tickMinutes (ZoneId zone)
final var clock = Clock.tickMinutes(ZoneOffset.UTC);
for (int i = 0; i < 4; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.SECONDS.sleep(30);
}
// 結果
// ↓
//2024-07-07T06:15:00Z
//2024-07-07T06:15:00Z
//2024-07-07T06:16:00Z
//2024-07-07T06:16:00Z
static Clock tickSeconds (ZoneId zone)
final var clock = Clock.tickSeconds(ZoneOffset.UTC);
for (int i = 0; i < 8; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.MILLISECONDS.sleep(500);
}
// 結果
// ↓
//2024-07-07T06:18:04Z
//2024-07-07T06:18:05Z
//2024-07-07T06:18:05Z
//2024-07-07T06:18:06Z
//2024-07-07T06:18:06Z
//2024-07-07T06:18:07Z
//2024-07-07T06:18:07Z
//2024-07-07T06:18:08Z
abstract Clock withZone (ZoneId zone)
final var clock1 = Clock.systemUTC();
System.out.println(clock1); // SystemClock[Z]
final var zone = ZoneId.of("America/Los_Angeles");
final var clock2 = clock1.withZone(zone);
System.out.println(clock2); // SystemClock[America/Los_Angeles]
final var clock1 = Clock.tickSeconds(ZoneOffset.UTC);
System.out.println(clock1); // TickClock[SystemClock[Z],PT1S]
final var zone = ZoneId.of("Asia/Tokyo");
final var clock2 = clock1.withZone(zone);
System.out.println(clock2); // TickClock[SystemClock[Asia/Tokyo],PT1S]
関連記事
- 日付・時刻の基本
- Date, CalendarではなくLocalDateTime, ZonedDateTimeを使おう
- 文字列と日付・時刻の変換
- 日付と時刻、曜日の計算
- 現在時刻(日時)の取得いろいろ
- 現在の曜日(DayOfWeek)を取得
- ZoneIdとZoneOffsetの違い
- API 使用例
- Calendar (カレンダー)
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Date (日付・時刻)
- DateTimeException (日付・時刻の例外)
- DateTimeParseException (日付・時刻の解析例外)
- DayOfWeek (曜日)
- Duration (時間の量)
- Era (紀元)
- Instant (時点)
- InstantSource
- JapaneseDate (和暦を使った日付)
- LocalDate (日付・タイムゾーンなし)
- LocalDateTime (日時・タイムゾーンなし)
- LocalTime (時刻・タイムゾーンなし)
- Month (月)
- MonthDay (月・日)
- OffsetDateTime (日時・オフセットあり)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)