我正在尝试将文件的creationTime属性转换为日期格式为MM / dd / yyyy的字符串。我正在使用Java nio获取creationTime属性,该属性是FileTime
类型,但是我只想从中将日期FileTime
作为具有前面指定的日期格式的字符串。到目前为止我有...
String file = "C:\\foobar\\example.docx";
Path filepath = Paths.get(file);
BasicFileAttributes attr = Files.readAttributes(filepath,BasicFileAttributes.class);
FileTime date = attr.creationTime();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String dateCreated = df.format(date);
但是,它引发异常,表示无法将FileTime date
对象格式化为日期。FileTime似乎以2015-01-30T17:30:57.081839Z
例如形式输出。您会推荐什么解决方案来最好地解决此问题?我应该只在该输出上使用正则表达式,还是有一个更优雅的解决方案?
刚拿到毫秒纪元以来从FileTime
。
String dateCreated = df.format(date.toMillis());
// ^
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句