Created
September 9, 2022 07:18
-
-
Save 0OZ/c37021c4bc1916caec6e675e3b88dc10 to your computer and use it in GitHub Desktop.
ZonedDateTimeConvert | Reactive MongoConfig (Kotlin)
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
@Configuration | |
class MongoConfig { | |
@Bean | |
fun mongoCustomConversions(): MongoCustomConversions { | |
val converters = listOf( | |
ZonedDateTimeToDate.INSTANCE, | |
DateToZonedDateTime.INSTANCE | |
) | |
return MongoCustomConversions(converters) | |
} | |
@ReadingConverter | |
internal enum class DateToZonedDateTime : Converter<Date?, ZonedDateTime?> { | |
INSTANCE; | |
override fun convert(date: Date): ZonedDateTime = | |
date.toInstant() | |
.atZone(ZoneId.systemDefault()) | |
.truncatedTo(ChronoUnit.MILLIS) | |
} | |
@WritingConverter | |
internal enum class ZonedDateTimeToDate : Converter<ZonedDateTime?, Date?> { | |
INSTANCE; | |
override fun convert(zonedDateTime: ZonedDateTime): Date = | |
Date.from(zonedDateTime.toInstant()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment