Failed to convert value of type java.lang.String to required type java.util.Date

springboot在接收时间类型的时候,报Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'的错误,应该怎么解决呢?


错误说明:

    该错误的意思是 前台传递 string类型的时间数据,后台使用Date类型去接收,结果就报错了。


解决方案: 

    springboot要解决这个问题很简单,在接收的字段上面,添加下面的注解 就OK

   @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")  

   @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") 

    private Date startTime;


注解说明:

   @DateTimeFormat  该注解自动会解析处理,会把字符串类型 按照格式yyyy-MM-dd HH:mm:ss 转换成时间类型

   @JsonFormat这个注解是从数据库查询出来,返回到页面的的转换. 把时间类型 转换成JSON格式类型,前提取出进行展示.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容