Java的日期时间库比较乱,同样一个Date在java.sql下定义,同时也在java.util下也定义了一遍,真不知道SUN是怎么想的。

java8以后,java通过jsr310标准引入了一套符合ISO国际标准的时间库。

Clock: 时钟

Instant: 瞬时时间,类似于时间戳

LocalDateTime、LocalDate、LocalTime: 本地化时间

 

Jackson启用ISO格式的日期

@JsonFormat(shape = JsonFormat.Shape.STRING)

 

public Page<DeviceInfo> getDeviceListForCustomerBetweenDates( @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime startDate, @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime endDate, Pageable pageable)

Jackson统一控制Java8Time格式化

JavaTimeModule module = new JavaTimeModule();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
module.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_DATE));
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ISO_DATE_TIME));
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_DATE));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
ObjectMapper mapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(module);

 

相关文章:

  • 2021-08-22
  • 2021-06-23
  • 2021-07-11
  • 2022-12-23
  • 2022-01-30
  • 2021-12-15
  • 2021-11-07
  • 2021-06-25
猜你喜欢
  • 2021-10-23
  • 2021-07-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2021-11-20
相关资源
相似解决方案