尝试使用 ZonedDateTime 与 MongoDB 。我能够在 MongoDB 中保存 ZonedDateTime 但是当我查看记录时,那里有太多不必要的东西:
Trying to use ZonedDateTime with MongoDB. I'm able to save ZonedDateTime in MongoDB but when i look at the record it has so much unnecessary stuffs in there:
> "timestamp" : { > "dateTime" : ISODate("2016-12-13T13:45:53.991Z"), > "offset" : { > "_id" : "-05:00", > "totalSeconds" : -18000 > }, > "zone" : { > "_class" : "java.time.ZoneRegion", > "_id" : "America/New_York", > "rules" : { > "standardTransitions" : [ > NumberLong(-2717650800) > ], > "standardOffsets" : [ > { > "_id" : "-04:56:02", > "totalSeconds" : -17762 > }, > { > "_id" : "-05:00", > "totalSeconds" : -18000 > } > ], > "savingsInstantTransitions" : [ > NumberLong(-2717650800), > NumberLong(-1633280400), > NumberLong(-1615140000), > NumberLong(-1601830800), > NumberLong(-1583690400), > NumberLong(-1570381200), > and so on....此外,当我尝试检索同一个日期时,它会让我关注:
Also when i try to retrieve this same date, it gives me following:
> org.springframework.data.mapping.model.MappingException: No property > null found on entity class java.time.ZonedDateTime to bind constructor > parameter to!使用 LocalDateTime 。 第一个问题是,我们可以更改某些设置,只会持续 ISODate ZonedDateTime ? 第二个问题,是否有类似 Jsr310JpaConverters 的 mongodb ?
更新: 引用以下问卷我创建了自定义转换器并注册了它们,但问题仍然存在。 带有Java 8 LocalDate MappingException的Spring Data MongoDB
public class ZonedDateTimeToLocalDateTimeConverter implements Converter<ZonedDateTime, LocalDateTime> { @Override public LocalDateTime convert(ZonedDateTime source) { return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId .systemDefault()); } }和
public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime, ZonedDateTime> { @Override public ZonedDateTime convert(LocalDateTime source) { return source == null ? null : ZonedDateTime.of(source, ZoneId.systemDefault()); } }将其注册如下:
@Bean public CustomConversions customConversions(){ List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>(); converters.add(new ZonedDateTimeToLocalDateTimeConverter()); converters.add(new LocalDateTimeToZonedDateTimeConverter()); return new CustomConversions(converters); } @Bean public MongoTemplate getMongoTemplate() throws UnknownHostException { MappingMongoConverter converter = new MappingMongoConverter( new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext()); converter.setCustomConversions(customConversions()); converter.afterPropertiesSet(); return new MongoTemplate(getMongoDbFactory(), converter); }推荐答案
看起来Spring支持所有java时间转换器减去分区日期时间转换器。您可以按如下方式注册:
Looks like Spring has support for all the java time converter minus zoned date time converter. You can register one as follows.
@Bean public CustomConversions customConversions(){ List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>(); converters.add(new DateToZonedDateTimeConverter()); converters.add(new ZonedDateTimeToDateConverter()); return new CustomConversions(converters); } @Bean public MongoTemplate getMongoTemplate() throws UnknownHostException { MappingMongoConverter converter = new MappingMongoConverter( new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext()); converter.setCustomConversions(customConversions()); converter.afterPropertiesSet(); return new MongoTemplate(getMongoDbFactory(), converter); } class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> { @Override public ZonedDateTime convert(Date source) { return source == null ? null : ofInstant(source.toInstant(), systemDefault()); } } class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> { @Override public Date convert(ZonedDateTime source) { return source == null ? null : Date.from(source.toInstant()); } }另一种替代解决方案是使用ZonedDateTime并在持久化到MongoDB时将其更改为日期。在获取时,您可以轻松地将其从日期返回到Zoned Date Time。
One other alternative solution would be to just use the ZonedDateTime and change it to date while persisting into MongoDB. You can easily change it back from date back to Zoned Date Time while fetching.
以下是帮助转换的相关方法。
Below are the relavant methods to help with conversions.
ZoneId zoneID = ZoneId.of("America/Chicago");从ZonedDateTime到java util date。
From ZonedDateTime to java util date.
Instant instant = Instant.now(); ZonedDateTime zonedDateTime = instant.atZone(zoneId); Date date = Date.from(zdt.toInstant());从日期到ZonedDateTime
From Date to ZonedDateTime
Instant instant = date.toInstant(); ZonedDateTime zonedDateTime = instant.atZone(zoneId);另一种方法是实现自定义编解码器以帮助进行转换。我在Mongo文件中过滤YearMonth为YearMonth创建了一个。如果他们想为Zoned Date Time创建自定义编解码器,我会将它作为练习留给读者。
The other alternative is to implement custom codec to help with conversions. I've created one for YearMonth at Filtering YearMonth from Mongo document. I'll leave it as exercise to the reader if they want to create custom codec for Zoned Date Time.
您可以使用以下库来实现基于编解码器的方法。
You can use below library for codec based approach.
github/ylemoigne/mongo -jackson-codec
更多推荐
ZonedDateTime与MongoDB
发布评论