获取文件的最后修改日期/时间作为本地日期/时间字符串 [英] Obtain last modification date/time of file as local date/time string

查看:155
本文介绍了获取文件的最后修改日期/时间作为本地日期/时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)

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).

这篇关于获取文件的最后修改日期/时间作为本地日期/时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆