时间格式中莫名其妙加了个T怎么处理

目录

为什么会多一个T

应该怎么处理

1.在实体类中加注解

​编辑

 2.直接转换

3.参考网上其他博客


为什么会多一个T

LocalDateTime的源码打印中是默认在日期和时间点中间加了个T的

LocalDateTime源码:

@Override
public String toString() {
return date.toString() + 'T' + time.toString();
}

应该怎么处理

1.在实体类中加注解

在实体类中加入下列代码,我是这样处理的(我是在往前台传展示的时候发现的这个问题)

@JsonFormat(timezone = “GMT+8”,pattern = “yyyy-MM-dd HH:mm:ss”)

时间格式中莫名其妙加了个T怎么处理

 2.直接转换

当前时间.replace(/T/g, ‘ ‘).replace(/.[\d]{3}Z/, ‘ ‘) 即可去掉T ;

3.参考网上其他博客

@Configuration
public class LocalDateTimeSerializerConfig {
    @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());
    }
}

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/31f3c04bf1.html