How to get file creation date in Java
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.
package com.mkyong.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 GetCreationDate1 {
public static void main(String[] args) {
String fileName = "/home/mkyong/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: 2020-07-20T09:29:54.627222Z
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.
package com.mkyong.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 GetCreationDate2 {
public static void main(String[] args) {
String fileName = "/home/mkyong/test";
try {
Path file = Paths.get(fileName);
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.
package com.mkyong.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/mkyong/core-java
$ cd java-io
In Java7 there is a more sophisticated way to get (or set!) file attributes, including creation time.
public void printFileCreationTime() throws IOException {
Path path = Paths.get(“C:\\myfile.txt”);
BasicFileAttributeView basicView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes basicAttr = basicView.readAttributes();
LOG.info(String.format(“BasicFileAttribute creationTime: %s”, basicAttr.creationTime()));
}
If you don’t use log4j, replace the LOG.info(String.format()) to System.out.format(), or something similar.
There are lots of other attribute views, with lots of fields. Check them out.
Thanks for your code.
I think we can show AM/PM too.
String date = st.nextToken();//Get date
String time = st.nextToken();//Get time
String amPm = st.nextToken();//Get AM or PM
System.out.println(“Creation Date : ” + date);
System.out.println(“Creation Time : ” + time + ” ” + amPm);
Hi All of you
I am looking for a code,which will display to me date and time Dynamically.means Just like a clock,run time.Please help me ASAP.Thanks in advance
Sufyan Khan
this code will work in Linux platform?
The NIO works for Linux and Windows.
For example 3, this is a Windows solution before Java 7.
Hi, will you please explain how for loop work here :
why i<6 only …i don't understand it…
Skip the first five lines, and we only need the 6th line that contains the file creation time info.
Hi
Please provide the MAC based file/directory creation time or any other work around compatible to java 6.
Thank you very much.
Ramu madabhushani
Extracted value : null
Exception in thread “main” java.lang.NullPointerException
at java.util.StringTokenizer.(Unknown Source)
at java.util.StringTokenizer.(Unknown Source)
at ratna.GetFileCreationDateExample.main(GetFileCreationDateExample.java:32)
Your problem could be the white spaces. Try this:
String path =
Process proc = Runtime.getRuntime().exec(“cmd /c dir \”” + path + “\” /tc”);
Thank youuuuuuuuuuuuuuuu so much !!!! 😀 save me !!!