广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决springboot 实体类String转Date类型的坑
  • 783
分享到

解决springboot 实体类String转Date类型的坑

2024-04-02 19:04:59 783人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot 实体类String转Date类型Date解析String类型的参数springboot 实体类String转Date类型 前端传入一个String的时间字符

springboot 实体类String转Date类型

前端传入一个String的时间字符串如:2019-07-18 23:59:59

后端实体类要在头顶加注解:


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

不然会出现报错

Date解析String类型的参数

1.首先建立String to Date 的解析实现


import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateConverter implements Converter<String, Date> {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String shortDateFormat = "yyyy-MM-dd";
    @Override
    public Date convert(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        value = value.trim();
        try {
            if (value.contains("-")) {
                SimpleDateFormat formatter;
                if (value.contains(":")) {
                    formatter = new SimpleDateFormat(dateFormat);
                } else {
                    formatter = new SimpleDateFormat(shortDateFormat);
                }
                Date dtDate = formatter.parse(value);
                return dtDate;
            } else if (value.matches("^\\d+$")) {
                Long lDate = new Long(value);
                return new Date(lDate);
            }
        } catch (Exception e) {
            throw new RuntimeException(String.format("parser %s to Date failed", value));
        }
        throw new RuntimeException(String.format("parser %s to Date failed", value));
    }
}

2.创建全局的解析配置


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.WEB.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;
@Configuration
public class DateHandlerAdapter {
    @Autowired
    private RequestMappingHandlerAdapter handlerAdapter;
    
    @PostConstruct
    public void initEditableAvlidation() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
                .getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService) initializer
                    .getConversionService();
            genericConversionService.addConverter(new StringToDateConverter());
        }
    }
}

添加完这两个文件以后 在传参数类型为Date的参数时就不会再报 date解析失败的错误了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 解决springboot 实体类String转Date类型的坑

本文链接: https://www.lsjlt.com/news/155342.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作