SpringBoot之日期時間格式化


利用 jackson 的 json 序列化和反序列化

/**
 * @author zhu
 * @description: Jackson 配置
 * @date 2020-05-12 15:15
 */
@Configuration
public class JacksonConfig {

    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    private static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
    private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";


    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = getObjectMapper();
        converter.setObjectMapper(objectMapper);
        return converter;
    }

    private ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        // 忽略 JSON 字符串中不識別的屬性
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 忽略無法轉換的對象
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // PrettyPrinter 格式化輸出
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        // NULL 不參與序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        // 指定時區
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        // 日期類型字符串處理
        objectMapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT));

        // Java8日期處理
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, 
                new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addSerializer(LocalDate.class, 
                new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        javaTimeModule.addSerializer(LocalTime.class, 
                new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDateTime.class, 
                new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDate.class, 
                new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        javaTimeModule.addDeserializer(LocalTime.class, 
                new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
        objectMapper.registerModule(javaTimeModule);
        return objectMapper;
    }

}

總結:

  • 支持 content-type=application/json 請求中格式為 yyyy-MM-dd HH:mm:ss 的字符串,后台用@RequestBody 接收,及返回值 Date 轉為 yyyy-MM-dd HH:mm:ss 格式 String;
  • 支持java8日期api;
  • 不支持 content-type=application/json 請求中 yyyy-MM-dd 等類型的字符串轉為 Date;

個別字段需使用 yyyy-MM-dd 格式時,可使用 SpringBoot 自帶的注解 :

@JsonFormat(pattern = yyyy-MM-dd, timezone=GMT+8)
private Date sendDate;

SpringBoot 默認提供,功能強大,滿足常見場景使用,並可指定時區。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM