Created
March 13, 2023 06:22
-
-
Save mutkuensert/362665425a91e4f233791281daa83a9f to your computer and use it in GitHub Desktop.
LocalDateTime converters for room database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.room.TypeConverter | |
import java.time.Instant | |
import java.time.LocalDateTime | |
import java.time.ZoneId | |
import java.time.ZoneOffset | |
class Converters { | |
@TypeConverter | |
fun fromLocalDatetime(value: LocalDateTime?): Long? { | |
return value?.atZone(ZoneId.of(ZoneOffset.systemDefault().id))?.toInstant()?.toEpochMilli() | |
} | |
@TypeConverter | |
fun toLocalDateTime(value: Long?): LocalDateTime? { | |
return value?.let { | |
Instant.ofEpochMilli(it).atZone(ZoneId.systemDefault()).toLocalDateTime() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment