3种SpringBoot全局时间格式化配置
时间格式化在项目中使用频率是非常高的,当我们的 API
接口返回结果,需要对其中某一个 date
字段属性进行特殊的格式化处理,通常会用到 SimpleDateFormat
工具处理,代码如下:
1 2
| SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date stationTime = dateFormat.parse(dateFormat.format(PayEndTime()));
|
这种操作频繁,还产生很多重复臃肿的代码,而此时如果能将时间格式统一配置,就可以省下更多时间专注于业务开发了
@JsonFormat 注解
部分格式化,因为@JsonFormat
注解需要用在实体类的时间字段上,而只有使用相应的实体类,对应的字段才能进行格式化
1 2 3 4 5 6 7 8 9
| @Data public class OrderDTO {
@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd") private LocalDateTime createTime;
@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; }
|
@JsonComponent 注解
使用 @JsonFormat
注解并不能完全做到全局时间格式化,所以接下来我们使用 @JsonComponent
注解自定义一个全局格式化类,分别对 Date
和 LocalDate
类型做格式化处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| @JsonComponent public class DateFormatConfig {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern;
@Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() {
return builder -> { TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat(pattern); df.setTimeZone(tz); builder.failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .dateFormat(df); }; }
@Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); }
@Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); } }
|
实际开发中如果我有个字段不想用全局格式化设置的时间样式,想自定义格式怎么办,那就和@JsonFormat注解混合使用,@JsonFormat
注解的优先级比较高,会以 @JsonFormat
注解的时间格式为主。
@Configuration 注解
这种全局配置之后,jsonformat注解将不再生效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| @Configuration public class DateFormatConfig2 {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern;
public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Bean @Primary public ObjectMapper serializingObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); objectMapper.registerModule(javaTimeModule); return objectMapper; }
@Component public class DateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException { String formattedDate = dateFormat.format(date); gen.writeString(formattedDate); } }
@Component public class DateDeserializer extends JsonDeserializer<Date> {
@Override public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { try { return dateFormat.parse(jsonParser.getValueAsString()); } catch (ParseException e) { throw new RuntimeException("Could not parse date", e); } } }
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern))); } }
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern)); } } }
|