Dave's Brain

Browse - Programming Tips - How do I get the creation date/time of a file?

Date: 2014dec29
Language: Java

Q.  How do I 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.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.