Java时间格式保存出错处理:java.util.Date,typeMismatch

Field error in object 'role' on field 'createDatetime': rejected value [2021-02-19 11:38:38]; codes [typeMismatch.role.createDatetime,typeMismatch.createDatetime,typeMismatch.java.util.Date,typeMismatch];

第一种解决方案:添加注解@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss"),然后前台以这种方式传递

    @ApiModelProperty("创建时间")
    @Column
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createDatetime;

第二种解决方案:使用 @InitBinder注解,注册一个父类Controller(BaseController),其他Controller去继承它

 

public class BaseController<T extends IDEntity, ID extends Serializable> {


    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new DateEditor());
    }


  public class DateEditor extends PropertyEditorSupport {
        public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        public SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public void setAsText(String text)
        {
            setValue(parseString(text));
        }
        private Object parseString(String text)
        {
            Date date = null;
            if (null != text && !"".equals(text) && !"null".equals(text)) {
                try {
                    String newText = null;
                    if (text.contains("T") && text.contains("Z")) { // 处理2019-07-07T16:00:00.000Z时间格式
                        text = text.replace("Z", " UTC");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
                        Date date1 = sdf.parse(text);
                        newText = dateFormat.format(date1);
                        newText = newText + " 00:00:00";
                    }else if(text.contains("GMT")){
                        text = text.replace("GMT", "").replaceAll("\\(.*\\)", "");
                        //将字符串转化为date类型,格式2016-10-12
                        SimpleDateFormat format =  new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);
                        date = format.parse(text);
                    }
                    if(date == null){
                        dateTimeFormat.setLenient(false);
                        if (null != newText) {
                            date = dateTimeFormat.parse(newText);
                        } else {
                            date = dateTimeFormat.parse(text);
                        }

                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (date == null) {
                    try {
                        dateFormat.setLenient(false);
                        date = dateFormat.parse(text);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
            return date;
        }
    }






}

参考来源:https://www.jb51.net/article/117071.htm

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值