kotlinx-datetimeで日付を扱う
時刻と日付を安全にマルチプラットフォームで扱う方法を学びます
「kotlinx-datetimeで日付を扱う」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Dates Need a Library
The core stdlib has no shared date type, so KMP uses kotlinx-datetime, an official multiplatform library for time.
Add the Dependency
You pull kotlinx-datetime into commonMain in your build file, and then every target can work with dates the same way.
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0")Instant Is a Moment
An Instant is a single point on the timeline, the same exact moment no matter which device reads it.
val now: Instant = Clock.System.now()Time Zones Matter
The same instant looks different per TimeZone. The library lets you convert moments into local wall-clock time safely.
val zone = TimeZone.currentSystemDefault()LocalDateTime for People
A LocalDateTime is the human-readable date and time, like a calendar entry, without any time-zone baggage attached.
val dt = now.toLocalDateTime(zone)Read the Parts
From a LocalDateTime you can read fields like year, month, day and hour, all in plain common Kotlin.
val y = dt.year
val h = dt.hourLocalDate for Calendars
When you only care about the day, use LocalDate. It models a date with no time component at all.
val today: LocalDate = dt.dateDo Math With Durations
You can add or subtract a Duration from an Instant to shift moments forward or backward in time.
val later = now.plus(1, DateTimeUnit.HOUR, zone)Parse and Format Text
kotlinx-datetime can parse ISO strings into instants and turn them back into text, ideal for API payloads.
val parsed = Instant.parse("2024-01-01T00:00:00Z")Identical On Every Target
Because the library is multiplatform, your date logic produces the same result on Android, iOS and desktop. 🙂
Keep Dates in Shared Code
Put all your time handling in commonMain with kotlinx-datetime, so the two apps never disagree about dates.
Quick Check
Which type marks one exact moment in time?
Recap
Use kotlinx-datetime for dates: Instant for moments, LocalDateTime for people, and shared code that agrees across platforms.
よくある質問
「kotlinx-datetimeで日付を扱う」レッスンは無料ですか?
はい。「kotlinx-datetimeで日付を扱う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「kotlinx-datetimeで日付を扱う」で何を学びますか?
時刻と日付を安全にマルチプラットフォームで扱う方法を学びます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「kotlinx-datetimeで日付を扱う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- どこでも使える文字列、数値、コレクション
- 共有コードのmap、filter、fold
- kotlinx-datetimeで日付を扱う
- Common stdlibにないもの