How to deserialize date in format "yyyy-mm-dd hh:mm:ss"?

Viewed 968

I am getting a date in a format like this "2021-03-26 22:02:53" from the API which is not ISO format, and I created a custom serializer for it but I am getting an error. I also do not want to use ISO format, and I want to send back the date as "yyyy-mm-dd hh:mm:ss".

Since We can not use Java.Date in Kotlin multiplatform, what is the proper solution to serialize date format like this?

IDE with code file open

Debug frame variables

Thanks

1 Answers

Currently, kotlinx-datetime only supports the ISO format.

However, the format you expect is close enough to ISO, so you could tweak it by simply replacing the space by a T and should be good to go. It's not ideal but it should do the job while waiting for proper formatting support from Kotlinx datetime.

return decoder.decodeString().replace(' ', 'T').toLocalDateTime()

You can also do the opposite when serializing:

encoder.encodeString(value.toString().replace('T', ' '))
Related