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());
    }


    }

  • 相关阅读:
    软件工程课程总结
    c#代码分析
    运用visual studio进行简单的单元测试
    安装visual studio过程
    忙着,快乐着
    软件工程心得
    session
    XML
    期末团队评价
    黄金点游戏
  • 原文地址:https://www.cnblogs.com/NJM-F/p/10275815.html
Copyright © 2011-2022 走看看