Is possible to save custom datatypes like Java's LocalDateTime?

Is there a way to define for example:
var lcalDateTime: LocalDateTime =
in RealmObject in Kotlin SDK and add support for serialization of this class into Realm?
I see that new Kotlin SDK versions support KSerializer but thats probably not meant for this right?

At this moment, I am doing it like so:

    private var _dayScheduled: RealmInstant =
    var dayScheduled: LocalDate
        get() = LocalDate.from(_dayScheduled.toInstant().atZone(ZoneOffset.UTC))
        set(value) {
            _dayScheduled = value.atStartOfDay().toRealmInstant()

This solution works but I am seraching for something with less boilerplate.

The Kotlin SDK is multiplatform and due to the lack of common date/time implementations we don’t have support for standard Java time entities.

Your approach is more or less following our advised workaround which can be found in Realm does not support properties of this type (Date) · Issue #1378 · realm/realm-kotlin · GitHub. You should however remember to @Ignore the transient attribute.

The above example further shows how to wrap these kind of custom wrappers into a delegate that you could reuse for multiple properties.

1 Like

Thanks for info. I am littlebit struggling to make the delegate work for both nullable and non-nullable fields:

class LocalDateAdapter(private val property: KMutableProperty0<RealmInstant?>) {
    operator fun getValue(thisRef: Any?, property: KProperty<*>) = { LocalDate.from(it.toInstant().atZone(ZoneOffset.UTC)) }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: LocalDate?) =

    private var _dayScheduled: RealmInstant =
    var dayScheduled: LocalDate by LocalDateAdapter(this::_dayScheduled) <-- Does not work

Any idea how to fix it?