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
210
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
400
KSPを使ってコード生成
takuji31
0
490
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
450
Featured
See All Featured
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
880
BBQ
matthewcrist
89
10k
Building AI with AI
inesmontani
PRO
1
1.2k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
Crafting Experiences
bethany
1
320
How STYLIGHT went responsive
nonsquared
100
6.3k
Visualization
eitanlees
152
17k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Documentation Writing (for coders)
carmenintech
77
5.5k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
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