zoukankan      html  css  js  c++  java
  • springmvc传参---LocalDateTime、Date等时间类型转换

    此处定义的dateConvert用来转换Date类型,如果是LocalDate、LocalDateTime类型,则将Date
    类型换成相应的类型即可,注意java8的日期类型需要用Formatter格式化:

    编写一个这样的类就可以了

    /**
    * 转换解析器
    * @author wangqingguo 2017/9/25
    */
    @Configuration
    public class MappingConverterAdapter {

    /**
    * 接收前端datetime参数
    * @return
    */
    @Bean
    public Converter<String, LocalDateTime> LocalDateTimeConvert() {
    return new Converter<String, LocalDateTime>() {
    @Override
    public LocalDateTime convert(String source) {

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime date = null;
    try {
    date = LocalDateTime.parse((String) source,df);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return date;
    }
    };
    }


    /**
    * 返回LocalDateTime值(去掉LocalDateTime中的T)
    */
    @org.springframework.beans.factory.annotation.Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
    private String pattern;

    @Bean
    public LocalDateTimeSerializer localDateTimeDeserializer() {
    return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
    }


    }

  • 相关阅读:
    html添加注释怎么弄?
    编程语言本身是怎么开发出来的?
    一句话说明Facbook React证书的矛盾点
    XAMPP是什么?
    HTTP解析
    version control
    函数式编程语言
    Servlet之Filter
    Build tool
    container和injection
  • 原文地址:https://www.cnblogs.com/NJM-F/p/10275815.html
Copyright © 2011-2022 走看看