我解决Jackson时间戳转换为LocalDateTime,好不容易解决了报错,但是却发现控制台中输出的时间是1970-01-01T00:00:00Z
与我想要的时间,差距过大。
这是我的代码
Long timestamp = jsonParser.getLongValue();
System.out.println("时间戳:" + timestamp);
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
System.out.println("LocalDate:" + localDate);
System.out.println("LocalDateTime:" + localDateTime);
控制台输出
特意去百度了一下,发现是时间单位问题,我也不太清楚,我是菜鸡。
特地去阿里云的时间戳转换根据测试了一下。
时间戳转换为当前时间,当是秒的时候。
这是毫秒的时间戳转换
可以发现当我们单位是毫秒的时候都输出LocalDateTime
的纪元参考点是UTC的1970-01-01T00:00:00Z
解决办法
时间戳这里乘以1000就可以了
Long timestamp = jsonParser.getLongValue()*1000;
System.out.println("时间戳:" + timestamp);
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
System.out.println("LocalDate:" + localDate);
System.out.println("LocalDateTime:" + localDateTime);