Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试分析System.currentTimeMillis()计算的时间戳时出现不可解析的日期错误_Java_Date_Time_Date Difference - Fatal编程技术网

Java 尝试分析System.currentTimeMillis()计算的时间戳时出现不可解析的日期错误

Java 尝试分析System.currentTimeMillis()计算的时间戳时出现不可解析的日期错误,java,date,time,date-difference,Java,Date,Time,Date Difference,我的一个文件名附加了一个时间戳。该时间戳是使用System.currentTimeMillis()计算的 现在我想得到该文件的时间戳和当前系统时间之间的时间差。我得到这个错误: 无法解释的日期:“1509083378768” 其中,1509083378768实际上是附加了文件名的时间戳。 下面是我的代码片段: private void calculateTimeDifference(String fileTimeStamp){ Date fileTimeStampDate ;

我的一个文件名附加了一个时间戳。该时间戳是使用
System.currentTimeMillis()
计算的

现在我想得到该文件的时间戳和当前系统时间之间的时间差。我得到这个错误:

无法解释的日期:“1509083378768”

其中,
1509083378768
实际上是附加了文件名的时间戳。 下面是我的代码片段:

 private void calculateTimeDifference(String fileTimeStamp){
    Date fileTimeStampDate ;

    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    long currentTime = System.currentTimeMillis();
    Date currentDate = new Date(currentTime);
    long timeDifference = 0 ;

    try {
        fileTimeStampDate = formatter.parse(fileTimeStamp);
        timeDifference = (currentDate.getTime() - fileTimeStampDate.getTime())/1000 ;

    } catch (ParseException e) {
        e.printStackTrace();
    }

    LOG.info("message='{}' {}", "Time Difference is", timeDifference);
}

新的SimpleDataFormat(“HH:mm:ss”)
可以解析类似
10:15:25
的字符串,它不是Timmestamp的格式,因此无法工作

您的时间戳是从纪元开始的毫秒数,您可以直接使用它来创建
日期

Date fileTimeStampDate = new Date(Long.parseLong(fileTimeStamp));
另外,我也不知道为什么要使用日期,因为使用
getTime
返回毫秒

因此,您可以去掉大部分代码,只需编写:

timeDifference = (currentTime - Long.parseLong(fileTimeStamp)) / 1000;

新的SimpleDataFormat(“HH:mm:ss”)
可以解析类似
10:15:25
的字符串,它不是Timmestamp的格式,因此无法工作

您的时间戳是从纪元开始的毫秒数,您可以直接使用它来创建
日期

Date fileTimeStampDate = new Date(Long.parseLong(fileTimeStamp));
另外,我也不知道为什么要使用日期,因为使用
getTime
返回毫秒

因此,您可以去掉大部分代码,只需编写:

timeDifference = (currentTime - Long.parseLong(fileTimeStamp)) / 1000;

您为什么坚持使用陈旧、过时且对程序员不太友好的类
Date
SimpleDateFormat
?如果您需要日期时间类(据您所知可能不需要),我强烈推荐。
long-timeDifference=TimeUnit.millises.toSeconds(System.currentTimeMillis()-long.parseLong(fileTimeStamp))刚刚运行我得到了8134。谢谢你。。。我不知道简单的方法。感谢分享最新资料。您为什么坚持使用陈旧、过时且对程序员不太友好的类
Date
SimpleDateFormat
?如果您需要日期时间类(据您所知可能不需要),我强烈推荐。
long-timeDifference=TimeUnit.millises.toSeconds(System.currentTimeMillis()-long.parseLong(fileTimeStamp))刚刚运行我得到了8134。谢谢你。。。我不知道简单的方法。谢谢分享最新的东西。