Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
java.timeをAndroidで使う
Search
Takuji Nishibayashi
April 25, 2024
220
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
170
CameraX使ってみた
takuji31
0
320
kotlinx.datetime 使ってみた
takuji31
0
1.1k
HiltのCustom Componentについて
takuji31
0
450
KSPを使ってコード生成
takuji31
0
500
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
3.3k
kotlinx.serialization
takuji31
0
710
kanmoba-returns-02.pdf
takuji31
0
300
AndroidXとKotlin Coroutines
takuji31
0
460
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
55
8.3k
Making Projects Easy
brettharned
120
6.8k
A Modern Web Designer's Workflow
chriscoyier
699
190k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
The SEO identity crisis: Don't let AI make you average
varn
0
560
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
350
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.9k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Discover your Explorer Soul
emna__ayadi
2
1.3k
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