How to get proper file creation date of file?
As yshavit said, not all operating systems record the date created. However, you should be able to use java.nio.file
to determine this information on operating systems that do have this functionality - see the documentation for files.getAttribute
- note that BasicFileAttributeView
has a field for creationTime
.
You can use FileSystems.getDefault();
to determine what FileAttributeView
s are supported on the current operating system.
Files.getAttribute(path, "basic:createdAt");
will return a FileTime
object with the date the file was created on a system that supports BasicFileAttributeView
. You'll have to convert it to a java.util.Date
object, but I'll let you figure that out on your own.
Further Reading
- NIO API for
getAttribute()
- NIO API for
BasicFileAttributeView
- A tutorial for using
readAttributes()
- A comprehensive tutorial on using FileAttributes
- Another StackOverflow thread on the same topic
How to find file creation date in linux using java?
You can use stat
command in Linux to get various date though creation date isn't available.
Instead you can get these 3 dates about a file:
- Time of last access
- Time of last modification (content of the file)
- Time of last change (metadata of file)
EDIT:
For getting creation/modification times of a file in Java (if using JDK 1.7) see: http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html
As per this document:
A word about time stamps: The set of basic attributes includes three
time stamps: creationTime, lastModifiedTime, and lastAccessTime. Any
of these time stamps might not be supported in a particular
implementation, in which case the corresponding accessor method
returns an implementation-specific value.
Unfortunately Linux/Unix doesn't store file's creation time hence you cannot get it.
PS: If you can use ext4 filesystem
then you can get file's creation time in Unix/Linux.
Getting date/time of creation of a file
I'm not sure how you'd get it using Java 6 and below. With Java 7's new file system APIs, it'd look like this:
Path path = ... // the path to the file
BasicFileAttributes attributes =
Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();
As CoolBeans said, not all file systems store the creation time. The BasicFileAttributes Javadoc states:
If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).
Related Topics
Simpledateformat and Locale Based Format String
How to Parse a JSON and Turn Its Values into an Array
Border with Rounded Corners & Transparency
Java Random Numbers Using a Seed
Difference Between Java Hh:Mm and Hh:Mm on Simpledateformat
Regex to Match Only Commas Not in Parentheses
How to Use an Internet Time Server to Get the Time
Java Cannot Make a Static Reference to Non-Static Field
Concurrentmodificationexception Despite Using Synchronized
How to Put Axis on a .Png File in Java
Where Is Java_Home on MACos Mojave (10.14) to Lion (10.7)
How to Inject a Property Value into a Spring Bean Which Was Configured Using Annotations
Difference Between @Before, @Beforeclass, @Beforeeach and @Beforeall
Java - How to Find the Redirected Url of a Url
How to Read Integer Value from the Standard Input in Java
Explicitly Calling a Default Method in Java
Redirect to an External Url from Controller Action in Spring MVC