查询时发送给服务器的日期的字符串格式: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. rpc中的注册中心

    使用模板模式,对注册中心进行设计,可以方便后续添加注册中心 模板抽象类,提供注册中心必要的方法. public abstract class ServiceRegistry { //这是一个模板的抽象 ...

  2. python简介以及简单代码——python学习笔记(一)

    学习来源:https://www.liaoxuefeng.com/wiki/1016959663602400 了解python 简单编写并实现python代码 命令行模式和python交互模式 了解p ...

  3. 支持MySQL数据库的agumaster版本

    下载地址:https://files.cnblogs.com/files/xiandedanteng/agumaster20200501.zip

  4. Explain Plan试分析

    注:以下是本人对Explain Plan的试分析,有不对的地方希望大家指出.关于如何查看Oracle的解释计划请参考:https://www.cnblogs.com/xiandedanteng/p/1 ...

  5. nginx高可用

    15.1. 传统的高可用思路 tomcat的高可用的思路,是在tomcat集群前面加一层负载服务nginx.如下图 这种做法,解决了tomcat的高可用问题.但是引入了前面的负载机器的高可用问题(Ng ...

  6. 关于Vue-CLI的那些事儿

    Vue CLI是基于Vue.js进行快速发展的完整系统,提供了: 交互式的项目脚手架 实现零配件的原型开发 图形化的创建和管理项目的界面 基本框架的构建: . ├── build/ # webpack ...

  7. cannary

    canary是Linux为防止栈溢出的一种保护机制,接着我们分析glibc对canary的实现过程,首先给出跟canary相关的调用栈: security_init()  //在elf/rtld.c中 ...

  8. 获取JSO字符串的key和value值

    import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.HashMap; import java. ...

  9. charles 入门配置(win10+vivoX20)(Charles一)

    charles的几个功能可以参考:https://www.cnblogs.com/mihoutao/p/10601171.html 首先是charles的下载 下载地址:https://www.cha ...

  10. Windows docker镜像文件无法删除

    最近刚开始玩docker,下载镜像之前没有修改docker的保存路径,因此默认存在了c:\programdata下面,导致C盘空间不足. 之后修改了保存路径之后( docker engin里加&quo ...