Android Store LocalDateTime in SharedPreferences

UTC Epoch miliseconds to LocalDateTime

val milliseconds = sharedPref.getLong(key, 0)val localDateTime = if (milliseconds == 0L) null else LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneOffset.UTC)

LocalDateTime to UTC Epoch miliseconds

val milliseconds = localDateTime?.atZone(ZoneOffset.UTC)?.toInstant()?.toEpochMilli() ?: 0sharedPref.edit {    putLong(key, milliseconds)}

NOTE: Android KTX is used for sharedPref.edit.

❤️ 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.