JAVA日期时间相关库

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);
原文地址:https://www.cnblogs.com/mkxzy/p/7091381.html