查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss

服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日期而产生的错误:

1 2019-12-05 17:43:55.215  WARN 1460 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
2 Field error in object 'billsVo' on field 'endTime': rejected value [2019-12-05 00:00:00]; codes [typeMismatch.billsVo.endTime,typeMismatch.endTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [billsVo.endTime,endTime]; arguments []; default message [endTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2019-12-05 00:00:00'; nested exception is java.lang.IllegalArgumentException]
3 Field error in object 'billsVo' on field 'startTime': rejected value []; codes [typeMismatch.billsVo.startTime,typeMismatch.startTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [billsVo.startTime,startTime]; arguments []; default message [startTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value ''; nested exception is java.lang.IllegalArgumentException]]

解决方法:在相应的属性上使用 @DateTimeFormat 注解,并指定格式,见第 14 或 16 行:

 1 import java.util.Date;
2
3 import org.springframework.format.annotation.DateTimeFormat;
4
5 import lombok.AllArgsConstructor;
6 import lombok.Data;
7 import lombok.NoArgsConstructor;
8
9 @NoArgsConstructor // 无参构造方法
10 @AllArgsConstructor // 有参构造方法
11 @Data // Getters、Setters、toString() 等方法
12 public class BillsVo {
13
14 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
15 private Date startTime;
16 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
17 private Date endTime;
18 }

顺便一说,如果要指定服务器端返回给客户端的日期的 JSON 格式:

可以在相应的类的属性上使用 @JsonFormat 注解:

1     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
2 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
3 private Date billtime;

如果是 Spring Boot 项目,也可以在 application.yml 文件中指定:

1 spring:
2 jackson:
3 date-format: yyyy-MM-dd HH:mm:ss
4 time-zone: GMT+8

Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'的更多相关文章

  1. spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'

    在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错: Failed to convert property value of type [java.lang.String] ...

  2. 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';

    后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...

  3. Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

    今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...

  4. Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFa ...

  5. 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '

    没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...

  6. Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';

    springboot jdbc查询使用LocalDate报:Failed to convert value of type 'java.lang.String' to required type 'j ...

  7. SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tac ...

  8. 完美解决报错Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

    Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 首先这个错误的意思是 前台页面 ...

  9. 前台传参数时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'

    springMVC action接收参数: org.springframework.validation.BindException: org.springframework.validation.B ...

随机推荐

  1. Android开发之java代码工具类。判断当前网络是否连接并请求下载图片

    package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...

  2. HTTPS的简介

    一.什么是HTTPS HTTPS是在HTTP上建立SSL加密层,并对传输数据进行加密,是HTTP协议的安全版.现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面. HTTPS主要作用是: (1) ...

  3. 读Twinsen的深入探索透视投影变换

    2017.10.16更新,分割线下面是以前的文字,有表达的意思,却言不达意,实属羞耻,看官只需看前面文字即可. Twinsen大神的<深入探索透视投影变换>有几个点说得不够清晰,我这里提一 ...

  4. bootstrap的时间控件使用(双日历)

    这段时间看了下bootstrap的时间控件,发现使用起来还是很简单的,趁着有时间的时候整理了一下,方便自己以后忘记的时候查阅... 废话不多说先上效果图 接下来是代码实现 第一步当然是导入css.js ...

  5. Myabtis动态SQL,你真的会了吗?

    目录 前言 什么是动态SQL? 常用的标签 if choose.when.otherwise where foreach set sql include 总结 拓展一下 Mybatis中如何避免魔数? ...

  6. Java并发编程:volatile关键字解析【转载】

    介绍 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字 ...

  7. Java里一个线程两次调用start()方法会出现什么情况

    Java的线程是不允许启动两次的,第二次调用必然会抛出IllegalThreadStateException,这是一种运行时异常,多次调用start被认为是编程错误. 如果业务需要线程run中的代码再 ...

  8. 从通达信导出文件获取A股所有股票代号名称(至2020年2月24日)

    下文是讲述如何从通达信的输出文件中获得股票信息,如果想用Java爬虫从网页爬取信息请参考:https://www.cnblogs.com/xiandedanteng/p/12808381.html 要 ...

  9. Linux/Unix Terminal中文件/目录的颜色各代表什么意思?

    注意:console/terminal中文件目录的颜色设置是可以更改的,故环境不同颜色就可能不一样. 下面是我所用终端的颜色示例: 颜色说明: 白色:普通文件 紫色:目录 红色:有问题的链接文件 蓝绿 ...

  10. MySQL的事务机制和锁(InnoDB引擎、MVCC多版本并发控制技术)

    一.事务(数据库的事务都通用的定义) 1.1 事务定义 事务是由一步或几步数据库操作序列组成逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行.事务通常以 BEGIN TRANSACTION 开始 ...