Upgrade to Pro — share decks privately, control downloads, hide ads and more …

その日時は Instant?LocalDateTime?

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Kaito-Dogi Kaito-Dogi
May 29, 2026
21

その日時は Instant?LocalDateTime?

2026/05/29 開催の DroidKaigi.collect { #30@Tokyo } にて、『その日時は Instant?LocalDateTime?』 というテーマで発表しました。

Avatar for Kaito-Dogi

Kaito-Dogi

May 29, 2026

Transcript

  1. ❏ 時間軸上の1点 ❏ v0.7.0 以降では kotlin.time に移⾏(Kotlin v2.3 以降で Stable)*

    // 端末の現在時刻から Instant を取得 val instantFromSystemClock = Clock.System.now() // ISO 8601 準拠の文字列を Instant にパース val instantFromUtcOffset = Instant.parse("2026-05-29T19:00:00+09:00") Instant とは * https://kotlinlang.org/docs/whatsnew23.html#standard-library
  2. Use kotlin.time.Instant to represent a timestamp of the event that

    had already happened in the past (like a timestamp of a log entry) or will definitely happen in a well-defined instant of time in the future not far away from now (like an order confirmation deadline in 1 hour from now). Instant とは https://github.com/Kotlin/kotlinx-datetime#type-use-cases
  3. Use kotlin.time.Instant to represent a timestamp of the event that

    had already happened in the past (like a timestamp of a log entry) or will definitely happen in a well-defined instant of time in the future not far away from now (like an order confirmation deadline in 1 hour from now). Instant とは https://github.com/Kotlin/kotlinx-datetime#type-use-cases ログのタイムスタンプ 1時間後の締め切り
  4. Use LocalDateTime to represent a time of the event that

    is scheduled to happen in the far future at a certain local time (like a scheduled meeting in a few months from now). You'll have to keep track of the TimeZone of the scheduled event separately. Try to avoid converting future events to Instant in advance, because time zone rules might change unexpectedly in the future. LocalDateTime とは https://github.com/Kotlin/kotlinx-datetime#type-use-cases
  5. Use LocalDateTime to represent a time of the event that

    is scheduled to happen in the far future at a certain local time (like a scheduled meeting in a few months from now). You'll have to keep track of the TimeZone of the scheduled event separately. Try to avoid converting future events to Instant in advance, because time zone rules might change unexpectedly in the future. LocalDateTime とは https://github.com/Kotlin/kotlinx-datetime#type-use-cases 数ヶ⽉後の予定
  6. Instant と LocalDateTime の使い分け 表現する情報 ユースケース Instant 時間軸上の1点 過去の瞬間や近い将来 ログや登録⽇時、

    数分後、数時間後の 締め切り LocalDateTime どこかの⽇時 未来の予定 イベントの開始時刻
  7. Instant ↔ LocalDateTime の変換 ❏ いずれもタイムゾーンが必要 ❏ Instant → LocalDateTime:どこの⽇時かを決めるため

    ❏ LocalDateTime → Instant:どこの⽇時かを知るため val timeZone = TimeZone.of("Asia/Tokyo") val instant = Clock.System.now() val localDateTimeFromInstant = instant.toLocalDateTime(timeZone) val localDateTime = LocalDateTime(2026, 5, 29, 19, 0) val instantFromLocalDateTime = localDateTime.toInstant(timeZone)
  8. kotlinx.datetime の設計思想 https://github.com/Kotlin/kotlinx-datetime#design-overview However, there are convenience operations that take,

    for example, a physical instant and perform a calendar-based adjustment (such as adding a month); all such operations explicitly take time-zone information as a parameter to clearly state that their result depends on the civil time zone rules which are subject to change at any time.
  9. kotlinx.datetime の設計思想 https://github.com/Kotlin/kotlinx-datetime#design-overview However, there are convenience operations that take,

    for example, a physical instant and perform a calendar-based adjustment (such as adding a month); all such operations explicitly take time-zone information as a parameter to clearly state that their result depends on the civil time zone rules which are subject to change at any time. すべての操作でタイムゾーンが必要
  10. まとめ ❏ Instant ❏ 時間軸上の1点 ❏ 過去の瞬間や近い将来を表すのに適している ❏ LocalDateTime ❏

    どこかの⽇時(タイムゾーンをもたない) ❏ 未来の予定を表すのに適している ❏ Instant ↔ LocalDateTime の変換ではタイムゾーンが必要