Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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/8/file/3.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文件?_Java_File_Date - Fatal编程技术网

上次在默认时区修改的Java文件?

上次在默认时区修改的Java文件?,java,file,date,Java,File,Date,我想在我的计算机时区中获取上次修改的日期(与我在windows文件资源管理器中看到的相同) 从文件 返回:一个长值,表示文件上次修改的时间,从历元(00:00:00 GMT, 1970年1月1日),如果文件不存在或I/O错误,则为0L 发生 因此,您需要将其转换为日期,如果您使用的是Java 8+,则可以使用如下API: LocalDateTime date = LocalDateTime.ofInstant( Instant.ofEpochMilli(myFile.lastMo

我想在我的计算机时区中获取上次修改的日期(与我在windows文件资源管理器中看到的相同)

从文件

返回:一个长值,表示文件上次修改的时间,从历元(00:00:00 GMT, 1970年1月1日),如果文件不存在或I/O错误,则为0L 发生

因此,您需要将其转换为日期,如果您使用的是Java 8+,则可以使用如下API:

LocalDateTime date = LocalDateTime.ofInstant(
        Instant.ofEpochMilli(myFile.lastModified()), ZoneId.systemDefault()
);

System.out.println(date);//example result : 2018-06-06T15:05:19.113

如果需要更高的精度,可以使用:

File myFile = new File("pathname");
Long timeMs = myFile.lastModified();
if (timeMs != 0) {
    LocalDateTime date = LocalDateTime.ofInstant(
            Instant.ofEpochMilli(myFile.lastModified()), ZoneId.systemDefault()
    );
    System.out.println(date);
}else{
    System.out.println("File not exist!");
}
从文件

返回:一个长值,表示文件上次修改的时间,从历元(00:00:00 GMT, 1970年1月1日),如果文件不存在或I/O错误,则为0L 发生

因此,您需要将其转换为日期,如果您使用的是Java 8+,则可以使用如下API:

LocalDateTime date = LocalDateTime.ofInstant(
        Instant.ofEpochMilli(myFile.lastModified()), ZoneId.systemDefault()
);

System.out.println(date);//example result : 2018-06-06T15:05:19.113

如果需要更高的精度,可以使用:

File myFile = new File("pathname");
Long timeMs = myFile.lastModified();
if (timeMs != 0) {
    LocalDateTime date = LocalDateTime.ofInstant(
            Instant.ofEpochMilli(myFile.lastModified()), ZoneId.systemDefault()
    );
    System.out.println(date);
}else{
    System.out.println("File not exist!");
}

您只需使用
ZonedDateTime
对象并将系统的默认时间偏移应用于该对象

ZonedDateTime zt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(myFile.lastModified()), ZoneId.systemDefault());

然后,您可以简单地打印出此对象或进一步使用它。

您可以简单地使用
ZonedDateTime
对象并将系统的默认时间偏移应用于它

ZonedDateTime zt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(myFile.lastModified()), ZoneId.systemDefault());
然后,您只需打印出此对象或进一步使用它。

“与我看到的相同”
。。我们怎么知道你看到了什么?为什么不给我们举个例子,让我们自己来决定呢?请将此信息包括在您的问题中。:)请在问题本身中举例说明。可能重复。还有许多其他类似的问题和答案,请使用您的搜索引擎。
“与我看到的相同”
。。我们怎么知道你看到了什么?为什么不给我们举个例子,让我们自己来决定呢?请将此信息包括在您的问题中。:)请在问题本身中举例说明。可能重复。还有许多其他类似的问题和答案,请使用您的搜索引擎。