Android: How to get file creation date?

This is my code:

File TempFiles = new File(Tempfilepath); if (TempFiles.exists()) { String[] child = TempFiles.list(); for (int i = 0; i < child.length; i++) { Log.i("File: " + child[i] + " creation date ?"); // how to get file creation date..? } } 
+47
android
Mar 05 '10 at 19:07
source share
4 answers

File creation date is not available , but you can get the date of the last change :

 File file = new File(filePath); Date lastModDate = new Date(file.lastModified()); Log.i("File last modified @ : "+ lastModDate.toString()); 
+134
Mar 05 '10 at 21:50
source share

The file creation date is not an accessible piece of data that is open by the Java File class. I recommend that you rethink what you are doing and change your plan so you don't need it.

+19
Mar 05 '10 at 19:55
source share

Here is how I would do it

 // Used to examplify deletion of files more than 1 month old // Note the L that tells the compiler to interpret the number as a Long final int MAXFILEAGE = 2678400000L; // 1 month in milliseconds // Get file handle to the directory. In this case the application files dir File dir = new File(getFilesDir().toString()); // Obtain list of files in the directory. // listFiles() returns a list of File objects to each file found. File[] files = dir.listFiles(); // Loop through all files for (File f : files ) { // Get the last modified date. Milliseconds since 1970 Long lastmodified = f.lastModified(); // Do stuff here to deal with the file.. // For instance delete files older than 1 month if(lastmodified+MAXFILEAGE<System.currentTimeMillis()) { f.delete(); } } 
+19
Sep 16 '10 at 8:53
source share

There is an alternative way. When you first open a file, save the last date with the modification before changing the folder.

 long createdDate =new File(filePath).lastModified(); 

And then when you close the file do

 File file =new File(filePath); file.setLastModified(createdDate); 

If you did this since the file was created, you will have the created date as lastModified date all the time.

+4
Feb 07 '13 at 4:38
source share



All Articles