Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
kotlinx.datetime 使ってみた
Search
Takuji Nishibayashi
July 10, 2024
Technology
0
580
kotlinx.datetime 使ってみた
Takuji Nishibayashi
July 10, 2024
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
190
HiltのCustom Componentについて
takuji31
0
260
java.timeをAndroidで使う
takuji31
0
110
KSPを使ってコード生成
takuji31
0
360
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
2.6k
kotlinx.serialization
takuji31
0
600
kanmoba-returns-02.pdf
takuji31
0
220
AndroidXとKotlin Coroutines
takuji31
0
370
AndroidXに潜む便利なヤツら
takuji31
0
180
Other Decks in Technology
See All in Technology
PagerDuty×ポストモーテムで築く障害対応文化/Building a culture of incident response with PagerDuty and postmortems
aeonpeople
3
540
ドキュメント管理の理想と現実
kazuhe
3
320
ここはMCPの夜明けまえ
nwiizo
32
13k
MySQL Indexes and Histograms – How they really speed up your queries
lefred
0
150
Compose におけるパスワード自動入力とパスワード保存
tonionagauzzi
0
190
10分で学ぶ、RAGの仕組みと実践
supermarimobros
0
830
AI 코딩 에이전트 더 똑똑하게 쓰기
nacyot
0
500
読んで学ぶ Amplify Gen2 / Amplify と CDK の関係を紐解く #jawsug_tokyo
tacck
PRO
1
300
今日からはじめるプラットフォームエンジニアリング
jacopen
8
1.9k
Oracle Cloud Infrastructure:2025年4月度サービス・アップデート
oracle4engineer
PRO
0
350
AndroidアプリエンジニアもMCPを触ろう
kgmyshin
2
600
正式リリースされた Semantic Kernel の Agent Framework 全部紹介!
okazuki
1
500
Featured
See All Featured
Speed Design
sergeychernyshev
29
930
Git: the NoSQL Database
bkeepers
PRO
430
65k
Unsuck your backbone
ammeep
671
57k
RailsConf 2023
tenderlove
30
1.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.7k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
105
19k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.4k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
33k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
41
2.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
Transcript
kotlinx.datetime 使ってみた Sansan モバイル勉強会 vol.1 Takuji Nishibayashi(@takuji31)
自己紹介 西林 拓志 (にしばやし たくじ ) Twitter/GitHub takuji31 Sansan 株式会社
2024/04/01 入社 技術本部 Mobile Application グループ 6 月〜 Eight Android チーム テックリード Android (2009〜 ) Kotlin (2014〜 ) 1
日付処理してますか? 2
Kotlin JVM や Android で日付関連クラスといえば 3
java.util.Date / java.util.Calendar 4
java.time (JSR310) 5
今なら java.time を使うことが多い 6
see. java.time を Android で使う 7
Kotlin Multiplatform だとどうか? 8
kotlinx.datetime 9
Kotlin Multiplatform 対応の公式ライブラリー 10
kotlinx.serialization 対応 11
JVM/JS/WASM/Native 等実装あり 12
github.com/Kotlin/kotlinx-datetime 13
kotlinx.datetime の使い方 14
build.gradle.kts repositories { mavenCentral() } kotlin { sourceSets { commonMain
{ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0") } } } } 15
kotlinx.datetime のクラスたち Instant Clock LocalDateTime LocalDate LocalTime TimeZone Month DayOfWeek
etc. 16
今回はよく使いそうなクラスだけ紹介 17
Instant 18
エポック秒を持っているクラス 19
いわゆる Date っぽいやつ 20
使い方 val now = Clock.System.now() now.epochSeconds // 1970/01/01 00:00:00からの経過秒数 now.toEpochMilliseconds()
// 1970/01/01 00:00:00からの経過ミリ秒数 now.nanosecondsOfSecond // ナノ秒 val secondLater = now + 1.seconds// 1秒後 val sameTimeOfYesterday = now - 1.days // 昨日の同じ時間 21
LocalDateTime 22
タイムゾーンを含まない DateTime 23
LocalDate + LocalTime 24
使い方 val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()) now.year // 2024 now.month //
Month.JULY now.monthNumber // 7 now.date // 10 now.hour // 12 now.minute // 34 now.second // 56 25
注意点 26
まだ alpha なので破壊的変更入るかも? 27
JVM では java.time を使った方がいいかも? 28
実装が JVM だと java.time のラッパー 29
java.time みたいに ZonedDateTime や OffsetDateTime といったものはない 30
→ Instant を適宜タイムゾーンに応じて変換する 31
LocalDateTime は直接計算できない 32
→ Instant にしてから計算して戻す 33
Instant にしてから計算して戻す val timeZone = TimeZone.currentSystemDefault() val now = Clock.System.now().toLocalDateTime(timeZone)
val sameTimeInTomorrow = (now.toInstant(timeZone) + 1.days) .toLocalDateTime(timeZone) 34
Enjoy kotlinx.datetime 35