編輯:我想創建一個文件但沒有時間戳記。 我的代碼是:在Java中沒有時間戳記的creationTime
Path path = Paths.get(selectedFile.toURI());
BasicFileAttributes attr;
attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Creation date: " + attr.creationTime());
輸出:
Creation date: 2013-07-25T13:52:23.043207Z
我該怎麼做才能只:Creation date: 2013-07-25
? 當我使用SimpleDateFormat
,還給了我一個錯誤: 例如,如果我使用(就像我在這個論壇上見過):
Calendar c = GregorianCalendar.getInstance();
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", attr.creationTime);
System.out.println("duke: " +s);
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
我只是用的: 字符串輸出= data.substring(0, 10); ...它似乎工作! – Margherita
是的,它會適用於這種特定的日期格式,因爲它被聲明爲Javadoc [FileTime#toString()](https://docs.oracle.com/javase/8/docs/api/java/nio/file/屬性/ FileTime.html#toString--)將以ISO格式返回。你可以回答你自己的問題 –