首页 > 解决方案 > 将纳秒(100 纳秒单位)转换为文件时间并在 java 中查看为日期

问题描述

纳秒值130016196641685504

预期结果:2013 年 1 月 2 日星期三下午 5:01:04

此代码的输出:1974-02-14 01:06:36

   FileTime time = FileTime.from(nanoSeconds,TimeUnit.NANOSECONDS);
   String pattern = "yyyy-MM-dd HH:mm:ss";
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
   Date date = new Date(time.toMillis());
   System.out.println(simpleDateFormat.format(date));

更具体地说,我正在尝试将纳秒转换为 java 中的 humanReadable 格式。有关更多详细信息,请参阅此链接...

标签: javadatetimetimetimestamp

解决方案


为什么您会期待 2013 年 1 月 2 日星期三下午 5:01:04?

阅读规格

Parameters:
value - the value since the epoch (1970-01-01T00:00:00Z); can be negative

https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/FileTime.html#from-long-java.util.concurrent.TimeUnit-

而 130016196641685504 大约是 4 年。

所以你的期望是错误的。


推荐阅读