时间戳转换LocalDateTime输出为1970-01-01T00:00:00Z
时间:2023-01-28 13:00:00
我解决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);