获取文件的最后修改日期/时间作为本地日期/时间字符串

问题描述:

new File(url).lastModified()返回 long 等于自纪元以来的毫秒数,这是基于GMT的。

new File(url).lastModified() returns a long equal to the number of milliseconds since the epoch, which is GMT-based.

将此转换为表示系统本地的 String 的简单方法是什么?日期/时间?

What is a simple way to convert this to a String representing system-local date/time?

如果你真的需要在这里看到我的尝试,但这是一个可怕的混乱,无论如何它是错的:

If you really need to see an attempt from me here it is but it's a terrible mess and it's wrong anyway:

LocalDateTime.ofEpochSecond(new File(url).lastModified()/1000,0,ZoneOffset.UTC).atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG))

超出 LocalDateTime 我只是不知道时间API是如何工作的。

Beyond LocalDateTime I just have no idea how the time API works.

获取上次修改时间文件,您应该使用Java NIO.2 API,它直接解决您的问题:

To get the last modified time of a file, you should use Java NIO.2 API, which directly resolves your problem:

FileTime fileTime = Files.getLastModifiedTime(Paths.get(url));
System.out.println(fileTime); // will print date time in "YYYY-MM-DDThh:mm:ss[.s+]Z" format

如果要访问其他属性(如上次访问时间,创建时间),可以使用 Files.readAttributes(path,BasicFileAttributes.class)$ c读取路径的基本属性$ c>。

If you want to access other properties (like last access time, creation time), you can read the basic attributes of a path with Files.readAttributes(path, BasicFileAttributes.class).