java8使用LocalDateTime,因为之前的Date如果不格式化,打印输出可读性差,同时使用SimpleDateFormat对时间格式化方法是线程不安全的,所以推荐使用java8的时间。使用了java8的时间后在使用一些工具的时候可能因为项目需要,要进行一些配置才能符合要求,例如时间格式、序列化、反序列化等。

时间格式化

  1. DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  2. System.out.println(LocalDateTime.now().format(dateTimeFormatter));

场景一:前端传字符串,后端实体是LocalDateTime类型

  • 项目中可能会出现前端组件传参是字符串,但是后端定义是时间类型,如果Vo定义是字符串但是实体类却是时间类型的时候,使用拷贝的时候字段类型匹配错误将报错
  • 如果使用字符串接收后再用工具类将字符串转时间比较麻烦,代码冗余,于是使用注解如下
    1. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    2. private LocalDateTime localDateTime;

场景二:使用easyexcel导出LocalDateTime类型字段的时候,不进行配置会报错

  1. 写转换器实现Converter,重写方法
  2. 在导出的字段上关联转换器 ```java public class LocalDateTimeConverter implements Converter {

    @Override public Class supportJavaTypeKey() {

    1. return LocalDateTime.class;

    }

    @Override public CellDataTypeEnum supportExcelTypeKey() {

    1. return CellDataTypeEnum.STRING;

    }

    @Override public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,

    1. GlobalConfiguration globalConfiguration) {
    2. return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    }

    @Override public CellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,

    1. GlobalConfiguration globalConfiguration) {
    2. return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    }

}

  1. ```java
  2. @ExcelProperty(value="时间",converter=LocalDateTimeConverter.class)
  3. private LocalDateTime localDateTime;

场景三:LocalDateTime序列化相关

阿里的fastJson工具存在安全问题,这里使用Jackson工具来实现序列化和反序列化

序列化

  1. Student stu = new Student();
  2. // stu.setAge(12);
  3. stu.setName("123");
  4. stu.setWeight(123);
  5. stu.setLocalDateTime(LocalDateTime.now());
  6. ObjectMapper objectMapper = new ObjectMapper();
  7. String s = objectMapper.writeValueAsString(stu);

反序列化

  1. Student student = new ObjectMapper().readValue(s, Student.class);
  2. System.out.println("对象是" + student);

时间格式化配置

早期配置写死在代码里比较冗余,后来Jackson支持了注解的形式,只要在相关字段上加上注解就行

  1. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  2. @JsonSerialize(using = LocalDateTimeSerializer.class)
  3. @JsonDeserialize(using = LocalDateTimeDeserializer.class)
  4. private LocalDateTime localDateTime;

反序列化成Map

开发过程中数据出参入参是实体,有的时候数据处理中间需要用map更加方便,于是有可能反序化成Map

  • 使用TypeReference指定描述泛型

    1. Map<String, Object> stringObjectMap = new ObjectMapper()
    2. .readValue(jsonString, new TypeReference<Map<String, Object>>() {
    3. });

    其他常用配置

  • 反序列化 存在实体中没有的字段 进行忽略

    1. objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  • jackson默认时间格式为数组,用代码配置或者在字段用注解 ```java JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class,

    1. new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

objectMapper.registerModule(javaTimeModule); ```