Spring Mvc 及Jpa对LocalDateTime,LocalDate,LocalTime等日期处理

博客围绕SpringBoot2.0 + jdk1.8环境展开,主要讲述两方面内容,一是SpringMvc对日期传参返参的处理,二是处理jpa查询时间的映射问题,还提及启动类及实体类所在包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:
SpringBoot2.0+ jdk1.8

首先是SpringMvc对日期传参返参的处理:

SrpingMvc返回参数及接收参数时时间(LocalDateTime,LocalDate,LocalTime)的处理问题,因在项目中接收和
返回日期参数需要手动转换(有可能客户端传给你的格式还不对),而且很繁琐,以下就是对日期进行统一处理的配置

配置以下日期转换类:注意一定要保证被注入让spring管理
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class DateConverterConf{
         /**
         * 返回json的日期处理
         *
         * @return
         */
        @Bean(name = "objectMapper")
        public ObjectMapper getObjectMapper() {
            ObjectMapper om = new ObjectMapper();
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
            om.registerModule(javaTimeModule);
            return om;
        }

        @Bean
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
            return new MappingJackson2HttpMessageConverter(objectMapper);
        }

        /**
         * 接收前端日期的转换处理
         *
         * @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 {
                        if (StringUtils.isNotBlank(source)) {
                            date = LocalDateTime.parse(source, df);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return date;
                }
            };
        }

        @Bean
        public Converter<String, LocalDate> LocalDateConvert() {
            return new Converter<String, LocalDate>() {
                @Override
                public LocalDate convert(String source) {

                    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    LocalDate date = null;
                    try {
                        if (StringUtils.isNotBlank(source)) {
                            date = LocalDate.parse(source, df);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return date;
                }
            };
        }

        @Bean
        public Converter<String, LocalTime> LocalTimeConvert() {
            return new Converter<String, LocalTime>() {
                @Override
                public LocalTime convert(String source) {

                    DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm:ss");
                    LocalTime time = null;
                    try {
                        if (StringUtils.isNotBlank(source)) {
                            time = LocalTime.parse(source, df);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return time;
                }
            };
        }
    }

其次处理jpa查询时间的映射问题:

        实体类包含LocalDateTime,LocalDate 的时候,需要在启动类添加以下配置:**

启动类:

    @EntityScan(basePackages = {"com.xinaml.frame.entity"}, basePackageClasses = Jsr310JpaConverters.class)
    public class AppRoot {
        public static void main(String[] args) {
            SpringApplication.run(AppRoot.class, args);
    }


}

注:com.xinaml.frame.entity为实体类所在的包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值