0Pricing
Java Academy · レッスン

InstantとDuration

機械時刻と期間を学びます

「InstantとDuration」はCoddyKit上の無料Java Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJava Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Java Academyコースには全4レッスンが含まれています。

マシン時刻

LocalDateTimeが人間向けのカレンダーを扱うのに対し、Instantは、UTCの1970-01-01を起点として測定される、マシンのタイムライン上の正確な時点を表します。

Durationは、2つの時点の間の経過時間を表します。

Instantの生成

Instant.now()は現在の瞬間を取得します。予測可能な出力にするには、ofEpochSecondを使って固定したエポック秒から生成できます。

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant fixed = Instant.ofEpochSecond(1000000000);
        System.out.println(fixed);
    }
}

エポック秒とミリ秒

getEpochSecondとtoEpochMilliを使うと、基になるエポック値を読み取れます。これらはタイムスタンプやデータの保存に役立ちます。

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant t = Instant.ofEpochSecond(1500000000);
        System.out.println(t.getEpochSecond());
        System.out.println(t.toEpochMilli());
    }
}

Durationの生成

ofHours、ofMinutes、ofSecondsなどのファクトリメソッドを使ってDurationを生成します。

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        Duration d = Duration.ofMinutes(90);
        System.out.println(d);
        System.out.println(d.toHours());
    }
}

時点間のDuration

Duration.betweenは、2つの時点の間隔を測定します。何かにかかった時間を計測する標準的な方法です。

import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant start = Instant.ofEpochSecond(1000);
        Instant end = Instant.ofEpochSecond(1075);
        Duration elapsed = Duration.between(start, end);
        System.out.println(elapsed.getSeconds());
    }
}

Durationの変換

Durationは、toMinutes、toHours、toDays、toMillisを使って、さまざまな単位で値を取得できます。

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        Duration d = Duration.ofSeconds(7320);
        System.out.println(d.toMinutes());
        System.out.println(d.toHours());
    }
}

Instantへの加算

Durationを渡してplusやminusを使うと、時点を先または後ろに移動できます。

import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant now = Instant.ofEpochSecond(1000);
        Instant later = now.plus(Duration.ofHours(2));
        System.out.println(later.getEpochSecond());
    }
}

Plusのヘルパーメソッド

plusSecondsやplusMillisなどの便利なメソッドを使うと、単純な移動のたびにDurationを生成する必要がありません。

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant t = Instant.ofEpochSecond(0);
        System.out.println(t.plusSeconds(3600).getEpochSecond());
    }
}

Instantの比較

isBeforeとisAfterを使うと、タイムライン上の2つの時点の順序を比較できます。

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant a = Instant.ofEpochSecond(100);
        Instant b = Instant.ofEpochSecond(200);
        System.out.println(a.isBefore(b));
    }
}

負のDuration

終了時点が開始時点より前の場合、Durationは負になります。isNegativeとabsを使うと、この状態を処理できます。

import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Duration d = Duration.between(Instant.ofEpochSecond(200), Instant.ofEpochSecond(100));
        System.out.println(d.isNegative());
        System.out.println(d.abs().getSeconds());
    }
}

InstantとLocalDateTimeの違い

タイムスタンプ、ログ、経過時間の測定には、UTC上で曖昧さのないInstantを使います。タイムゾーンを別の場所で扱う人間向けのカレンダー値には、LocalDateTimeを使います。

確認テスト

InstantとDurationについての理解度を確認しましょう。

まとめ

マシン時刻について学びました。

  • Instantは、1970年のエポックを起点とするUTCのタイムライン上の正確な時点です
  • Durationは、ofHoursなどで生成する時間ベースの量です
  • Duration.betweenで経過時間を測定します
  • plusとminusで時点を移動します
  • タイムスタンプにはInstantを、人間向けの日付にはLocalDateTimeを使います

よくある質問

「InstantとDuration」レッスンは無料ですか?

はい。「InstantとDuration」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Java Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Java Academyコースには全4レッスンが含まれています。

「InstantとDuration」で何を学びますか?

機械時刻と期間を学びます ブラウザで直接実行するハンズオンコードでJava Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Java Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJava Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「InstantとDuration」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJava Academyレッスンでコードを書いて実行できますか?

はい。すべてのJava Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. LocalDate、LocalTime、LocalDateTime
  2. InstantとDuration
  3. Periodと日付計算
  4. DateTimeFormatterによる書式設定
← Java Academyに戻る