Determining file creation date in Java

There is another similar question to my StackOverflow ( How to get the file creation date in Java ), but the answer doesn’t actually exist, because the OP had another need that could be solved using other mechanisms. I am trying to create a list of files in a directory that can be sorted by age, therefore, you must specify the date the file was created.

I did not have a good way to do this after repeatedly trailing on the Internet. Is there a way to get file creation dates?

By the way, currently on a Windows system this may be required to work with a Linux system. In addition, I cannot guarantee that the naming convention will be respected where the creation date and time is embedded in the name.

+91
java date filesystems
Apr 27 '10 at 18:19
source share
8 answers

Java nio has options for accessing createTime and other metadata while the file system provides it. Check this link out

For example (Provided based on @ydaetskcoR comment):

Path file = ...; BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); System.out.println("creationTime: " + attr.creationTime()); System.out.println("lastAccessTime: " + attr.lastAccessTime()); System.out.println("lastModifiedTime: " + attr.lastModifiedTime()); 
+141
Apr 27 '10 at 18:43
source share

I solved this problem using JDK 7 with this code:

 package FileCreationDate; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.Date; import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] args) { File file = new File("c:\\1.txt"); Path filePath = file.toPath(); BasicFileAttributes attributes = null; try { attributes = Files.readAttributes(filePath, BasicFileAttributes.class); } catch (IOException exception) { System.out.println("Exception handled when trying to get file " + "attributes: " + exception.getMessage()); } long milliseconds = attributes.creationTime().to(TimeUnit.MILLISECONDS); if((milliseconds > Long.MIN_VALUE) && (milliseconds < Long.MAX_VALUE)) { Date creationDate = new Date(attributes.creationTime().to(TimeUnit.MILLISECONDS)); System.out.println("File " + filePath.toString() + " created " + creationDate.getDate() + "/" + (creationDate.getMonth() + 1) + "/" + (creationDate.getYear() + 1900)); } } } 
+13
Nov 18 '14 at 13:41
source share

As a continuation of this question - since it relates specifically to the time of creation and discusses its receipt through the new nio classes - it seems that now you are out of luck in the JDK7 implementation. Application: same behavior in OpenJDK7.

On Unix file systems, you cannot get the creation timestamp, you just get a copy of the last modification time. So sad, but unfortunately it's true. I'm not sure why this is so, but the code specifically does this, as shown below.

 import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.*; public class TestFA { static void getAttributes(String pathStr) throws IOException { Path p = Paths.get(pathStr); BasicFileAttributes view = Files.getFileAttributeView(p, BasicFileAttributeView.class) .readAttributes(); System.out.println(view.creationTime()+" is the same as "+view.lastModifiedTime()); } public static void main(String[] args) throws IOException { for (String s : args) { getAttributes(s); } } } 
+12
Jan 22 '13 at 9:56
source share

This is a basic example of how to get the file creation date in Java using the BasicFileAttributes class:

  Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt"); BasicFileAttributes attr; try { attr = Files.readAttributes(path, BasicFileAttributes.class); System.out.println("Creation date: " + attr.creationTime()); //System.out.println("Last access date: " + attr.lastAccessTime()); //System.out.println("Last modified date: " + attr.lastModifiedTime()); } catch (IOException e) { System.out.println("oops error! " + e.getMessage()); } 
+11
Sep 24 '15 at
source share

The java.io.File API only supports the last modified time. And the internet is very quiet on this topic.

If I have not missed something significant, the Java library, as it is (up to Java 7), does not include this feature. Therefore, if you desperately needed this, one solution would be to write C (++) code to call the system routines and call it using JNI. However, most of this work has already been done for you in the JNA library.

However, for this you still need to do a little OS encoding in Java, although you probably won't find the same system calls available on Windows and Unix / Linux / BSD / OS X.

+9
Apr 27 '10 at 18:29
source share

On Windows, you can use the free FileTimes library.

This will be easier in the future with Java NIO.2 (JDK 7) and the java.nio.file.attribute package .

But remember that most Linux file systems do not support file creation timestamps .

+9
Apr 27 '10 at 19:02
source share

On Windows, you can try this tutorial, it offers a workaround for java versions prior to Java 7 ....

0
Dec 21 '17 at 17:01
source share

in java1. 7+ You can use this code to get file creation time!

 private static LocalDateTime getCreateTime(File file) throws IOException { Path path = Paths.get(file.getPath()); BasicFileAttributeView basicfile = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); BasicFileAttributes attr = basicfile.readAttributes(); long date = attr.creationTime().toMillis(); Instant instant = Instant.ofEpochMilli(date); return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); } 
0
Apr 26 '19 at 3:28
source share



All Articles