Programming Tips - Java: get the creation date/time of a file?

Date: 2014dec29 Language: Java Q. Java: get the creation date/time of a file? A. Starting in Java 7 there is an official way.
import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.*; // Requires Java7+ long getCreateTime(String filename, 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; } } // Example use long created = getCreateTime("/home/myuser/.bashrc", -1);
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.