springboot日期时间格式全局处理

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间格式化配置
 *
 * @date 2022-10-04 09:00:35
 */
@ControllerAdvice
public class TimeFormatConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // Date类型返回及接收时间格式化
            builder.dateFormat(dateFormat);
            // 返回LocalDateTime时间类型序列化
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
            // 接收LocalDateTime时间类型反序列化,只对@RequestBody生效,对url中的参数不生效
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
        };
    }

    /**
     * 只对url中的参数生效,如http://localhost:8080/user/user/insert?createTime=2022-10-07 08:17:54,对@RequestBody不生效
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // 接收Date时间类型格式化
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        // 接收LocalDateTime时间类型格式化
        binder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                if (StringUtils.isNotBlank(text)) {
                    setValue(LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                }
            }
        });
    }
}

在这里插入图片描述

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,全局配置时间格式可以通过配置文件或代码方式来实现。如果全局配置时间格式无效,有以下几种可能的原因和解决方法: 1. 配置文件错误:首先需要确认配置文件是否正确,并且在正确的位置。在application.properties或application.yml文件中,可以使用以下配置来设置时间格式时间格式为yyyy-MM-dd HH:mm:ss: - 配置文件中:spring.jackson.date-format=yyyy-MM-dd HH:mm:ss - 代码中:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") 时间格式为自定义格式: - 配置文件中:spring.jackson.date-format=自定义格式 - 代码中:@JsonFormat(pattern = "自定义格式") 2. 代码中覆盖配置文件值:在代码中手动指定时间格式,可能会覆盖配置文件中的值。在代码中,可以使用注解@JsonFormat(pattern = "时间格式")来设置时间格式。需要确认代码中是否存在这样的设置。 3. 全局配置不生效:如果以上方法都没有生效,可以尝试在代码中自定义全局时间格式。在Spring Boot中,可以创建一个配置类,使用注解@Configuration来标识该类。在配置类中,通过注解@Bean来自定义时间格式并设置给ObjectMapper,代码示例如下: ``` @Configuration public class MyConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); // 其他配置... return objectMapper; } } ``` 需要注意的是,如果使用了自定义的ObjectMapper配置,会覆盖默认的配置。 综上所述,如果全局配置时间格式无效,可以通过检查配置文件、代码中的设置和自定义配置类来解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值