日期 时区问题

Date date = new Date();
Instant instant = date.toInstant();
assertEquals(date.getTime(), instant.toEpochMilli());

ZoneId shZoneId = ZoneId.of("Asia/Shanghai");
ZoneId cgZoneId = ZoneId.of("America/Chicago");

LocalDateTime shLocalDateTime =  LocalDateTime.ofInstant(instant, shZoneId);
LocalDateTime cgLocalDateTime = LocalDateTime.ofInstant(instant, cgZoneId);
assertEquals(shLocalDateTime.getSecond(), cgLocalDateTime.getSecond());
assertEquals(Math.abs(shLocalDateTime.getHour() - cgLocalDateTime.getHour()), 13);

ZonedDateTime shZonedDateTime = ZonedDateTime.of(shLocalDateTime, shZoneId);
ZonedDateTime cgZonedDateTime = ZonedDateTime.of(cgLocalDateTime, cgZoneId);
assertEquals(shLocalDateTime.getSecond(), cgZonedDateTime.getSecond());

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String shDateTimeStr = dateTimeFormatter.format(shZonedDateTime);
String cgDateTimeStr = dateTimeFormatter.format(cgZonedDateTime);
assertFalse(shDateTimeStr.equals(cgDateTimeStr));

不同时区获取的【毫秒数相等】的, 但是显【时间显示会有差异】!!!

posted @ 2020-06-19 16:35  梦见舟  阅读(227)  评论(0编辑  收藏  举报