Java.Time.LocalDateTime Converter For Room (Kotlin)

Create converter for java.time.LocalDateTime to epoch milli seconds.

class Converters {    @TypeConverter    fun fromTimestamp(value: Long?): LocalDateTime? {        return value?.let { LocalDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.UTC) }    }    @TypeConverter    fun dateToTimestamp(date: LocalDateTime?): Long? {        return date?.atZone(ZoneOffset.UTC)?.toInstant()?.toEpochMilli()    }}

Register Converters with RoomDatabase.

@Database(...)@TypeConverters(Converters::class)abstract class AppDatabase : RoomDatabase() {    ...}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.