锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

时间戳转换LocalDateTime输出为1970-01-01T00:00:00Z

时间:2023-01-28 13:00:00 插座连接器dc060t00

我解决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);
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章