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


    }

  • 相关阅读:
    hdu 2604 Queuing(矩阵快速幂乘法)
    hdu 5591 ZYB's Game
    hdu 5590 ZYB's Biology
    CodeForces
    uva 193 Graph Coloring(图染色 dfs回溯)
    uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
    CSU
    hdu 5115 Dire Wolf(区间dp)
    腾讯装扮下拉选项卡特效(QQ空间)
    楼梯式定位导航系统
  • 原文地址:https://www.cnblogs.com/NJM-F/p/10275815.html
Copyright © 2011-2022 走看看