当前位置: 首页 > 知识库问答 >
问题:

在Spring Data MongoDB中为ZonedDateTime注册一个新的可审计的日期转换器

柳业
2023-03-14

我希望我的可审计(@CreatedDate@LastModifiedDate)MongoDB文档与ZonedDateTime字段一起使用。

显然,Spring数据不支持这种类型(请查看org.springframework.Data.auditing.AnnotationAuditingMetadata)。

框架版本:Spring Boot 2.0.0和Spring Data MongoDB 2.0.0

java.lang.IllegalArgumentException: Invalid date type for member <MEMBER NAME>!
Supported types are [org.joda.time.DateTime, org.joda.time.LocalDateTime, java.util.Date, java.lang.Long, long].
@Configuration
@EnableMongoAuditing
public class MongoConfiguration {

}
public abstract class BaseDocument {

    @CreatedDate
    private ZonedDateTime createdDate;

    @LastModifiedDate
    private ZonedDateTime lastModifiedDate;

}

我还尝试为ZonedDateTime创建一个自定义转换器,但Spring数据不考虑它。类DateConvertingAuditableBeanRapper具有一个转换服务,该服务在构造函数方法中配置为JodaTimeConverters、Jsr310Converters和ThreeTenBackPortConverters。

@Component
public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime, ZonedDateTime> {

    @Override
    public ZonedDateTime convert(LocalDateTime source) {
        return source.atZone(ZoneId.systemDefault());
    }

}
class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {

    abstract static class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper {

        private final ConversionService conversionService;

    }
}

是否可以审核ZonedDateTime字段?

如何注册转换器?

共有1个答案

江正德
2023-03-14

创建DateTimeProvider以提供审核时使用的当前时间:

@Component("dateTimeProvider")
public class CustomDateTimeProvider implements DateTimeProvider {
    
    @Override
    public Optional<TemporalAccessor> getNow() {
        return Optional.of(ZonedDateTime.now());
    }
}

然后:

  • @EnableMongoAudting注释中引用DateTimeProvider组件;
  • DateZonedDateTime创建转换器s;
  • Converter实例添加到MongoCustomConversion实例;
  • MongoCustomConversion实例公开为@Bean
@Configuration
@EnableMongoAuditing(dateTimeProviderRef = "dateTimeProvider")
public class MongoConfiguration {
    
    @Bean
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new DateToZonedDateTimeConverter());
        converters.add(new ZonedDateTimeToDateConverter());
        return new MongoCustomConversions(converters);
    }

    class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        @Override
        public ZonedDateTime convert(Date source) {
            return source == null ? null : 
                    ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        }
    }

    class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        @Override
        public Date convert(ZonedDateTime source) {
            return source == null ? null : Date.from(source.toInstant());
        }
    }
}

然而,我不会为此使用ZonedDateTime。我会坚持使用OffsetDateTime:

OffsetDateTimeZonedDateTimeInstant都将时间线上的瞬间存储为纳秒精度。Instant是最简单的,简单地表示Instant<代码>OffsetDateTime将UTC/格林威治时间的偏移量添加到即时数据中,从而可以获取本地日期时间<代码>ZonedDateTime添加完整时区规则。

旨在使用ZonedDateTimeInstant在更简单的应用程序中对数据进行建模。此类可用于更详细地建模日期时间概念,或用于与数据库或网络协议通信时。

 类似资料:
  • 我有一个,其日期格式为:。需要将此转换为类型。试图用这种方法来做,但无法解析文本。时间可以是00:00:00。有什么建议吗?

  • 我正在用spring boot编写一个代码,这是一个web项目。Im还使用了thymeleaf和bootstrap。 我有一个模型,它包含一个名为'validade'的ZonedDateTime变量。在我的HTML/thymeleaf文件中,我有一个datepicker,允许您选择日、月和年。 出现错误(类型=错误请求,状态=400)。Object='tool'的验证失败。错误计数:1 org.s

  • 问题内容: 我正在尝试在数据库中设置服务器不可知日期时间,并且我相信这样做的最佳做法是设置UTC DateTime。我的数据库服务器是Cassandra,Java的数据库驱动程序仅理解Date类型。 因此,假设在我的代码中我正在使用新的Java 8 ZonedDateTime立即获取UTC(),如何将ZonedDateTime实例转换为“旧版” Date类? 问题答案: 您可以将ZonedDate

  • 我有一个,日期格式为:。需要将此转换为类型。尝试以这种方式执行,但无法解析Text。时间可以是00:00:00。有什么建议吗?

  • 问题内容: 我正在寻找一种将一个时区的日期转换为另一个时区的函数。 它需要两个参数, 日期(格式为“ 2019/04/10 10:10:30 +0000”) 时区字符串(“亚洲/雅加达”) 是否有捷径可寻? 问题答案: var aestTime = new Date().toLocaleString(“en-US”, {timeZone: “Australia/Brisbane”});

  • 为了实现这一点,我使用碳通过模型突变体转换日期。这意味着,首先我有日期: 我的变种人有以下几种: 现在,除了一个领域:“policy_cancellation”,一切都很好 当从表单向数据库插入数据时,如果policy_cancellation为空,则提交成功,但如果有数据,则PDO返回错误,例如: 触发以下命令: 另一个奇怪的情况(至少对我来说)是,在policy_cancellation突变体