How do you represent dates in Android with Kotlin?
1️⃣ java.util.Date
2️⃣ kotlinx-datetime
3️⃣ A value class wrapping a Long number of milliseconds
4️⃣ Desugared Java 8 time APIs
5️⃣ Other, please tell us more in thread!
2️⃣ 5
4️⃣ 19
1️⃣ 1
p
Paul Woitaschek
04/10/2022, 9:52 AM
1) Nope, the mutable legacy date has strong design flaws
2) Nope, it misses too many things like formatting, LocalTime etc
3) Nope, that’s what java.time.Instant is already doing
4) Yes, that’s the one with the best api and platform integration
e
ephemient
04/10/2022, 9:53 AM
4️⃣ in Android and backend code, 2️⃣ in multiplatform code. not using kotlinx-datetime everywhere because it lacks locale-aware formatting and zone-aware offsetting (although we do also use android.icu for some formatting as java.time desugar also lacks some things)
☝️ 2
a
Arun Joseph
04/10/2022, 8:13 PM
I use 2 and 4( for formatting), i assume 2 will have formatting one day.
e
ephemient
04/10/2022, 8:14 PM
Kotlin doesn't have a cross-platform notion of "Locale", and neither does kotlinx-datetime
a
Arun Joseph
04/11/2022, 7:59 AM
@ephemient Do you know if there is any cross-platform DateTimeFormatter equivalent? I use it just for getting the time stamp with '-'s
Copy code
fun getCurrentTimeStamp(): String {
// Formatting currentTime with java.time.*
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-SSS")
return formatter.format(
currentTime.toJavaInstant())
}
private val currentTime
get() = Clock.System.now()