简体   繁体   English

找不到能够从类型 [java.time.LocalDateTime] 转换为类型 [java.util.Date] 的转换器

[英]No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date]

I am using MongoDBTempalate with Springboot and trying to aggregate data basis LocalDateTime in which I am getting this error: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date]我在 Springboot 中使用 MongoDBTempalate 并尝试聚合数据基础 LocalDateTime,其中我收到此错误:org.springframework.core.convert.ConverterNotFoundException:找不到能够从类型 [java.time.LocalDateTime] 转换为类型 [java .util.Date]

I tried adding a custom convertor but it did not help, the code I added is:我尝试添加自定义转换器但没有帮助,我添加的代码是:

`@Bean
    public MongoCustomConversions customConversions(){
        List<Converter<?,?>> converters = new ArrayList<>();
        converters.add(DateToLocalDateTimeConverter.INSTANCE);
        converters.add( LocalDateTimeToDateConverter.INSTANCE);
        return new MongoCustomConversions(converters);
    }

    enum DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {

        INSTANCE;

        @Override
        public LocalDateTime convert(Date source) {
            return ofInstant(source.toInstant(), systemDefault());
        }
    }

    enum LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> {

        INSTANCE;

        @Override
        public Date convert(LocalDateTime source) {
            return Date.from(source.toInstant(ZoneOffset.UTC));
        }
    }`

Can someone tell me where have I gone wrong in creating the convertor, or is there some alternative apart from changing the LocalDateTime to Date in the code, as the occurance are very huge and refactoring might take a lot of time and effort有人可以告诉我在创建转换器时哪里出错了,或者除了在代码中将 LocalDateTime 更改为 Date 之外还有其他选择吗,因为发生的次数非常多,重构可能需要花费大量时间和精力

You can try this annotation on your entity or DTO, it will automatically format the date.您可以在您的实体或 DTO 上尝试此注释,它会自动格式化日期。

@Document(collection = "sample") public class Foo{ @Document(collection = "sample") 公共 class Foo{

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = 'yyyy-MM-dd HH:mm', locale = "en-PH") private Date createdAt; @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = 'yyyy-MM-dd HH:mm', locale = "en-PH") private Date createdAt;

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB 找不到能够从 [java.lang.String] 类型转换为 [java.time.LocalDateTime] 类型的转换器 - MongoDB No converter found capable of converting from type [java.lang.String] to type [java.time.LocalDateTime] 春季启动2:ConverterNotFoundException:未找到能够从类型[java.time.ZonedDateTime]转换为类型[java.util.Date]的转换器 - Spring boot 2 : ConverterNotFoundException: No converter found capable of converting from type [java.time.ZonedDateTime] to type [java.util.Date] 在 java.time.LocalDateTime 和 java.util.Date 之间转换 - Converting between java.time.LocalDateTime and java.util.Date 使用ZoneId.systemDefault()将java.util.Date转换为java.time.LocalDateTime - Converting java.util.Date to java.time.LocalDateTime using ZoneId.systemDefault() 找不到能够从类型 [java.util.LinkedHashMap 进行转换的转换器<?, ?> ] 输入 - No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type 找不到能够从类型 [java.util.LinkedHashMap 进行转换的转换器<?, ?> ] 输入 [java.lang.String] - Spring 配置服务器 - No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type [java.lang.String] - Spring config server 无法从 String 反序列化 java.time.LocalDateTime 类型的值 - Can not deserialize value of type java.time.LocalDateTime from String Spring 数据 Elasticsearch - 找不到能够从类型 [java.lang.Long] 转换为类型 [java.time.Instant] 的转换器 - Spring Data Elasticsearch - No converter found capable of converting from type [java.lang.Long] to type [java.time.Instant] 转换为java.util.Date时,Joda LocalDateTime IllegalFieldValueException - Joda LocalDateTime IllegalFieldValueException when converting to java.util.Date 将 java.time.LocalDate 转换为 java.util.Date 类型 - Convert java.time.LocalDate into java.util.Date type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM