【发布时间】:2021-03-23 12:08:37
【问题描述】:
当我们的应用程序从 MongoDB 加载 DBOject/Document 实例时,我们需要使用 DBObject/Document 实例中的 zoneIdName 将其中的所有 UTC 日期转换为 ZoneDateTime 值。我们在 DBObject 中有很多 Date 字段,所以我想避免实现 DBOject 级别的 Spring Data MongoDB 转换器。
是否可以使用 DBOject 字段级别的 Spring Data MongoDB 转换器,如下所示,它使用 DBObject 中的字段(即 zoneIdName)来执行到 ZoneDateTime 的转换?如果没有,我将不得不实现一个对象级转换器,但这会很乏味......
感谢您的关注和时间。
class ZonedDateTimeReadConverter :
Converter<Date, ZonedDateTime> {
override fun convert(date: Date): ZonedDateTime {
// Need to replace "America/LosAngeles" with
// a value from a field in the object being processed.
return date.toInstant().atZone(ZoneId.of("America/LosAngeles"))
}
}
【问题讨论】:
标签: java mongodb jackson spring-data spring-data-mongodb