java 如何使用 DateFormat 将 FileTime 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28304751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:22:16  来源:igfitidea点击:

How to Convert FileTime to String with DateFormat

javadatetimenio

提问by user2150250

I'm attempting to convert the creationTime attribute of a file to a string with a date format of MM/dd/yyyy. I am using Java nio to obtain the the creationTime attribute, which is of FileTimetype, but I just want the date from this FileTimeas a string with the date format specified previously. So far I have ...

我正在尝试将文件的 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);

However, it throws an exception saying it cannot format the FileTime dateobject as a Date. FileTime seems to output in form of 2015-01-30T17:30:57.081839Zfor example. What solution would you recommend to best solve this? Should I just use regex on that output or is there a more elegant solution?

但是,它抛出一个异常,说它不能将FileTime date对象格式化为日期。FileTime 似乎以2015-01-30T17:30:57.081839Z例如的形式输出。你会推荐什么解决方案来最好地解决这个问题?我应该在该输出上使用正则表达式还是有更优雅的解决方案?

采纳答案by Sotirios Delimanolis

Just get the milliseconds since epochfrom the FileTime.

拿到毫秒纪元以来FileTime

String dateCreated = df.format(date.toMillis());
//                                 ^

回答by Selvaraj

Convert FileTime to millis by toMillis()method.

通过toMillis()方法将 FileTime 转换为毫秒。

String file = "C:\foobar\example.docx";
Path filepath = Paths.get(file);
        BasicFileAttributes attr = Files.readAttributes(filepath, BasicFileAttributes.class);
        FileTime date = attr.creationTime();
        SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        String dateCreated = df.format(date.toMillis());
        System.out.println(dateCreated);

Use this code to get formatted value.

使用此代码获取格式化值。

回答by Mincong Huang

In Java 8, you can convert the FileTimeinto ZonedDateTimebefore formatting it:

在 Java 8 中,您可以在格式化之前将其转换FileTimeZonedDateTime

BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
long cTime = attr.creationTime().toMillis();
ZonedDateTime t = Instant.ofEpochMilli(cTime).atZone(ZoneId.of("UTC"));
String dateCreated = DateTimeFormatter.ofPattern("MM/dd/yyyy").format(t);
System.out.println(dateCreated);

which prints:

打印:

06/05/2018

回答by adesh

Converting FileTime to Date

将文件时间转换为日期

Path path = Paths.get("C:\Logs\Application.evtx");
DateFormat df=new SimpleDateFormat("dd/MM/yy");
try {
    BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
    Date d1 = df.parse(df.format(attr.creationTime().toMillis()));
    System.out.println("File time  : " +d1);
} catch (Exception e) {
    System.out.println("oops error! " + e.getMessage());
}

use this code to convert

使用此代码进行转换

回答by chia yongkang

To summarise:

总结一下:

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.toMillis());