时间类格式化

2023-03-01 17:44:34 浏览数 (1)

文章目录[隐藏]

  • Date
  • LocalDate等

Date

代码语言:javascript复制
方法1:properties文件添加
# 时间
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT 8 
代码语言:javascript复制
方法2:
// 时间格式化
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setDateFormat(new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN));

LocalDate等

代码语言:javascript复制
simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
simpleModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
simpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
simpleModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
simpleModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));

完整配置

代码语言:javascript复制
/**
 *  java 8 时间默认序列化,long转string
 *
 * @author dream
 * @since 2020/5/6 17:43
 * @version 1.0
 */
public class JavaTimeModule extends SimpleModule {

   public JavaTimeModule() {
      super(PackageVersion.VERSION);
      this.addSerializer(Long.class, ToStringSerializer.instance);
      this.addSerializer(Long.TYPE, ToStringSerializer.instance);
      this.addSerializer(long.class, ToStringSerializer.instance);
      this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
      this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
      this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
      this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
      this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
      this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
   }
}
代码语言:javascript复制
/**
 *  JacksonConfig
 *
 * @author dream
 * @since 2020/5/6 17:42
 * @version 1.0
 */
@Configuration
@ConditionalOnClass(ObjectMapper.class)
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class JacksonConfig {
   @Bean
   @Primary
   public Jackson2ObjectMapperBuilderCustomizer customizer() {
      return builder -> {
         builder.locale(Locale.CHINA);
         builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
         builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
         builder.modules(new JavaTimeModule());
      };
   }
}

导入包:

代码语言:javascript复制
//hutool
compileOnly "cn.hutool:hutool-all:$rootProject.ext.hutool"

0 人点赞