Springboot 时间格式化

方法一:属性上加注解

时间属性上添加注解:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

less 复制代码
@Data  
@Builder  
@NoArgsConstructor  
@AllArgsConstructor  
public class Employee implements Serializable {  
  

    private Long id;  

    private String username;  

    private String name;  


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")  
    private LocalDateTime createTime;  

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")  
    private LocalDateTime updateTime;  

  
}

方法二: 方法参数中添加注解@DateTimeFormat

less 复制代码
/**  
* 营业额统计  
* @param begin  
* @param end  
* @return  
*/  
@GetMapping("/turnoverStatistics")  
@ApiOperation("营业额统计")  
public Result<TurnoverReportVO> turnoverStatistics(  
    @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,  
    @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){  
    log.info("营业额数据统计:{},{}",begin,end);  
    return Result.success(reportService.getTurnoverStatistics(begin,end));  
}

方法三:拓展spring mvc的消息转换器

重写父类方法extendMessageConverters

scala 复制代码
@Configuration  
@Slf4j  
public class WebMvcConfiguration extends WebMvcConfigurationSupport {  
  
    @Autowired  
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;  
    @Autowired  
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;  

    /**  
    * 注册自定义拦截器  
    * @param registry  
    */  
    protected void addInterceptors(InterceptorRegistry registry) {  
        log.info("开始注册自定义拦截器...");  
        registry.addInterceptor(jwtTokenAdminInterceptor)  
        .addPathPatterns("/admin/**")  
        .excludePathPatterns("/admin/employee/login");  

        registry.addInterceptor(jwtTokenUserInterceptor)  
        .addPathPatterns("/user/**")  
        .excludePathPatterns("/user/user/login")  
        .excludePathPatterns("/user/shop/status");  
    }  


    /**  
    * 设置静态资源映射,主要是访问接口文档(html、js、css)  
    * @param registry  
    */  
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {  
    log.info("开始设置静态资源映射...");  
    registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");  
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");  
    }  

    /**  
    * 扩展Spring MVC框架的消息转化器  
    * @param converters  
    */  
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {  
        log.info("扩展消息转换器...");  
        //创建一个消息转换器对象  
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();  
        //需要为消息转换器设置一个对象转换器,对象转换器可以将Java对象序列化为json数据  
        converter.setObjectMapper(new JacksonObjectMapper());  
        //将自己的消息转化器加入容器中  
        converters.add(0,converter);  
    }  
}

创建对象转换器JacksonObjectMapper

arduino 复制代码
/**  
* 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象  
* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]  
* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]  
*/  
public class JacksonObjectMapper extends ObjectMapper {  
  
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";  
    //public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";  
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";  
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";  

    public JacksonObjectMapper() {  
    super();  
    //收到未知属性时不报异常  
    this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);  

    //反序列化时,属性不存在的兼容处理  
    this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);  

    SimpleModule simpleModule = new SimpleModule()  
    .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))  
    .addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))  
    .addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))  
    .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))  
    .addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))  
    .addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));  

    //注册功能模块 例如,可以添加自定义序列化器和反序列化器  
    this.registerModule(simpleModule);  
    }  
}

目录结构

相关推荐
卷无止境1 分钟前
FastAPI 的 WebSocket 装了些什么
后端·python·fastapi
AINative软件工程11 分钟前
Agent 上下文账本工程:别让工具结果把 128K 窗口塞成垃圾场
后端·架构·ai编程
卷无止境15 分钟前
FastAPI 的 Request 类装了什么
后端·python
程序员爱钓鱼16 分钟前
Rust 方法 Method详解:self、方法调用与API设计
后端·面试·rust
橙子家17 分钟前
MSDTC(微软分布式事务处理协调器)对系统稳定性的影响
后端
程序员爱钓鱼25 分钟前
Go break、continue、goto 详解
后端·面试·go
凤山老林1 小时前
复杂检索引擎落地:Spring Boot + Elasticsearch 数据同步与高阶查询实战
spring boot·后端·elasticsearch
笃行3509 小时前
SQLServer数据迁移之后,那张报表还能不能秒出
后端
LucianaiB9 小时前
别再被 AI 骗了:我把腾讯云 4 个 Skill 做成了个「AI 打假侦探」,过程的一个小配置坑惨了我
后端
candyTong10 小时前
Claude Code 如何恢复一段会话
后端·架构·ai编程