Date: 2014dec29
Update: 2025sep25
Language: Java
Q. Java: Get the creation date/time of a file
(This is different than the modified date/time)
A. Starting in Java 7 there is an official way.
Shown in this full example:
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.*;
class Demo {
// Requires Java7+
// Returns time in milliseconds since epoch
static long getCreateTime(final String filename, final long defaultCreateTime) {
Path path = Paths.get(filename);
try {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
FileTime fileCreated = attr.creationTime();
return fileCreated.toMillis();
} catch (IOException ex) {
return defaultCreateTime;
}
}
public static final void main(String []args) {
final long created = getCreateTime("/home/myuser/.bashrc", -1);
System.out.println("created=" + created);
}
}
Output (for me):
created=1664050896241
Linux/Unix only keeps track of modify, update, access times -- not create time.
You'll get the modify time from this function on a *nix box.