In Java (@since 1.7), we can use the NIO Files.readAttributes
to get all the file metadata, including the file creation date.
Path file = Paths.get(fileName);
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime: " + attr.creationTime());
// creationTime: 2020-07-20T09:29:54.627222Z
1. Files.readAttributes (NIO)
This example uses Files.readAttributes
to print the file creation date.
GetCreationDate1.java
package com.favtuts.io.howto; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; public class GetCreationDate { public static void main(String[] args) { String fileName = "/home/tvt/workspace/favtuts/test.txt"; try { Path file = Paths.get(fileName); BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); System.out.println("creationTime: " + attr.creationTime()); } catch (IOException e) { e.printStackTrace(); } } }
Output
creationTime: 2022-05-18T08:44:22.159855Z
P.S We also can use the same code to get the creation date of a directory.
Further Reading
Read this example to convert the FileTime to another date-time format.
2. Files.getAttribute (NIO)
The Files.readAttributes
will return all the file metadata like creation time, last modified time, file size, etc.
We can use Files.getAttribute
to return a speficied file metadata, for example, creationTime
attribute.
GetCreationDate2.java
package com.favtuts.io.howto; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileTime; public class GetCreationDate { public static void main(String[] args) { String fileName = "/home/tvt/workspace/favtuts/test.txt"; try { Path file = Paths.get(fileName); // Example 2 FileTime creationTime = (FileTime) Files.getAttribute(file, "creationTime"); System.out.println("creationTime: " + creationTime); } catch (IOException e) { e.printStackTrace(); } } }
3. Before Java 7
This example uses Runtime.getRuntime().exec
to issue a dir file /tc
system command to list the file creation date on Windows, and parse the content manually to extract the file creation date.
P.S Before Java 7, there is no official API to get the file creation date.
3.1 Review the dir /tc
command on Windows.
C:\> dir c:\logfile.log /tc
Volume in drive C has no label.
Volume Serial Number is 0410-1EC3
Directory of c:\
31/05/2010 08:05 14 logfile.log
1 File(s) 14 bytes
0 Dir(s) 35,389,460,480 bytes free
C:\> dir /?
Displays a list of files and subdirectories in a directory.
//...
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written
3.2 Java example.
GetFileCreation3.java
package com.favtuts.io.howto; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class GetFileCreation3 { public static void main(String[] args) { Process proc; BufferedReader br = null; try { proc = Runtime.getRuntime() .exec("cmd /c dir c:\\logfile.log /tc"); br = new BufferedReader( new InputStreamReader(proc.getInputStream())); String data = ""; //it's quite stupid but work, ignore first 5 lines for (int i = 0; i < 6; i++) { data = br.readLine(); } System.out.println("Extracted value : " + data); //split by space StringTokenizer st = new StringTokenizer(data); String date = st.nextToken(); //Get date String time = st.nextToken(); //Get time System.out.println("Creation Date : " + date); System.out.println("Creation Time : " + time); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Output
Extracted value : 31/05/2010 08:05 14 logfile.log
Creation Date : 31/05/2010
Creation Time : 08:05
The above code is still working, but it is too complicated to get a file creation time unless you are unable to upgrade the JVM; otherwise, please use the Java 7+ NIO Files.readAttributes
.
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io/howto