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
java.timeをAndroidで使う
Search
Takuji Nishibayashi
April 25, 2024
200
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
java.timeをAndroidで使う
Takuji Nishibayashi
April 25, 2024
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
compose-hot-reload を試そうとした話
takuji31
0
160
CameraX使ってみた
takuji31
0
310
kotlinx.datetime 使ってみた
takuji31
0
1.1k
HiltのCustom Componentについて
takuji31
0
380
KSPを使ってコード生成
takuji31
0
470
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
3.2k
kotlinx.serialization
takuji31
0
690
kanmoba-returns-02.pdf
takuji31
0
290
AndroidXとKotlin Coroutines
takuji31
0
440
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
170
The Cult of Friendly URLs
andyhume
79
6.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
Docker and Python
trallard
47
3.9k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
160
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
210
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Ruling the World: When Life Gets Gamed
codingconduct
0
260
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Accessibility Awareness
sabderemane
1
140
Transcript
java.time を Android で使う 関西モバイルアプリ研究会 A #3 @takuji31
自己紹介 西林 拓志(にしばやし たくじ) Twitter/GitHub takuji31 Sansan 株式会社 技術本部 Mobile
Application グループ Eight Android エンジニア Android (2009〜) Kotlin (2014〜) 1
java.time 使ってますか? 2
Java で日付関連クラスといえば 3
java.util.Date 4
java.util.Calendar 5
java.util.Date / java.util.Calendar java.util.Date は 1970/01/01 00:00:00.000 からの秒数を表したクラス タイムゾーンや歴(カレンダー)の情報はない java.util.Calendar
は現在の日時を表したクラス タイムゾーンや暦の情報が含まれる よく使われる具象クラスは GregorianCalendar 古い API でよく使われる たとえば Realm の日付型は java.util.Date 6
Date と Calendar の問題点 7
問題点 やれることが少ない、制限が多い 範囲を表すクラスがない 差分計算 年は 1900〜 月は zero indexed Mutable
である final や val でも setter で値が書き換わってしまう etc. 8
2024 年に積極的に使う API ではない 9
代替ライブラリー使う? 10
Joda-Time 11
ThreeTen Backport 12
ThreeTen Android Backport 13
一昔前なら ThreeTen Android Backport 使いましょうだった 14
ThreeTen -> 310 -> JSR310 -> java.time 15
今なら java.time 使ったらよい 16
でも java.time って Java 8 からなんでしょ? 17
今なら java.time 使えます 18
D8 19
D8 の API desugaring で Java8 以上の一部 API が使える 20
see. https://developer.android.com/studio/write/java8- support-table 21
how to enable D8 API desugaring 22
Android Gradle Plugin 4.0.0+ 23
build.gradle.kts android { defaultConfig { multiDexEnabled = true } compileOptions
{ isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_1_8 // 11 でもOK targetCompatibility = JavaVersion.VERSION_1_8 } } dependencies { coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3") } 24
java.time の使い方 25
java.time のクラスたち Instant LocalDateTime LocalDate LocalTime OffsetDateTime ZonedDateTime Duration Period
26
今回はよく使う 3 つだけ紹介 27
Instant 28
Date っぽいやつ 29
使い方 val now = Instant.now() now.epochSecond // 1970/01/01 00:00:00 からの経過秒数
now.toEpochMilli() // 1970/01/01 00:00:00 からの経過ミリ秒数 now.nano // ナノ秒 val secondLater = now + Duration.ofSeconds(1) // 1 秒後 val sameTimeOfYesterday = now - Duration.ofDays(-1) // 昨日の同じ時間 30
LocalDateTime 31
タイムゾーンを含まない DateTime 32
LocalDate + LocalTime 33
使い方 val now = LocalDateTime.now() now.year // 2024 now.month //
4 now.dayOfMonth // 25 val eventStartAt = LocalDateTime.of(2024, 4, 25, 18, 30, 0) // 2024/04/25 18:30:00 val eventEndAt = eventStartAt + Duration.ofMinutes(90) // 2024/04/25 20:00:00 val roomOpenAt = eventStartAt - Duration.ofMinutes(30) // 2024/04/25 18:00:00 34
ZonedDateTime 35
タイムゾーンの情報を含む DateTime 36
使い方 val now = ZonedDateTime.now() now.year // 2024 now.month //
4 now.dayOfMonth // 25 // convert other classes val nowInstant = now.toInstant() val nowLocalDateTime = now.toLocalDateTime() val currentZone = ZoneId.systemDefault() // Asia/Tokyo val JST = ZoneId.of("Asia/Tokyo") val UTC = ZoneId.of("UTC") val eventStartAtUTC = ZonedDateTime.now(UTC) // 2024/04/25 9:30:00 val eventEndAt = eventStartAtUTC + Duration.ofMinutes(90) // 2024/04/25 11:00:00 val roomOpenAt = eventStartAtUTC - Duration.ofMinutes(30) // 2024/04/25 9:00:00 37
Let's java.time 38