87

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..?
    }
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 6
    If you wanted create time why you accepted modified time ? http://stackoverflow.com/questions/2723838/determine-file-creation-date-in-java – M at Sep 14 '16 at 05:56

6 Answers6

217

The file creation date is not an available, but you can get the last-modified date:

File file = new File(filePath);
Date lastModDate = new Date(file.lastModified());

System.out.println("File last modified @ : "+ lastModDate.toString());
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    use last-modified HTTP header. – ahmet alp balkan Mar 16 '11 at 09:37
  • 5
    As @CommonsWare points out below the lastModified time is not the creation time. The creation time is not available. – miguel May 21 '12 at 21:45
  • 2
    please read first, use of last-modified is an option cause the creation time is not available. – Jorgesys May 21 '12 at 23:02
  • 2
    One bit of information I miss is a clear specification of whether File.lastModified is local time or UTC. The consensus is that it is UTC http://stackoverflow.com/questions/5264339/how-to-convert-a-date-to-utc and that's also what I'd expect in a semi-modern system. But why don't Oracle's specs http://developer.android.com/reference/java/io/File.html#lastModified%28%29 clearly state this? "... measured in milliseconds since January 1st, 1970, midnight." can, if one is sufficiently paranoid, be interpreted as either UTC January 1st, 1970, midnight, or local January 1st, 1970, midnight. – RenniePet Sep 01 '14 at 09:09
  • 2
    It is stored in milliseconds since UTC Jan 1st, 1970, but you don't have to worry about that - it is the standard definition of "epoch". Anything Date displays for you can be displayed in local time or UTC time or any time zone. – BeccaP Mar 20 '15 at 05:24
  • This is not the answer! I don't understand why so many up votes, I know about last modified date, but I f need creation date, like in windows explorer you can sort files by date creation or date modified. What is happening here??... – user924 Jan 28 '18 at 18:29
  • Guys, is there any way to find the last modified file and retreive that particular file alone from the group of files? – Anish Kumar May 06 '18 at 15:51
25

Here's 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();
   }
}
LA_
  • 19,823
  • 58
  • 172
  • 308
  • 2
    +1 I like this answer the best. I ran this in a thread and it worked pretty well. Also, love the var name MAX_FILEAGE :P – Ryan R Dec 01 '11 at 06:57
  • 7
    You're not answering the question. This is the file last modified date. – stef Aug 05 '17 at 10:59
22

The file creation date is not an available piece of data exposed by the Java File class. I recommend you rethink what you are doing and change your plan so you will not need it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    This is wrong. File supports this, http://developer.android.com/reference/java/io/File.html#lastModified() – Fuzzy Mar 22 '12 at 21:40
  • 11
    @Fuzzy: That is the last-modified time. It is not the file creation time once the file has been modified. – CommonsWare Mar 22 '12 at 21:44
  • @CommonsWare The file creation date is very important piece of data for file explorer applications for example. – ENSATE Oct 30 '22 at 15:38
  • @ENSATE: That does not change the fact that `File` does not expose an API for it. – CommonsWare Oct 30 '22 at 15:45
15

Starting in API level 26, you can do this:

File file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long createdAt = attr.creationTime().toMillis();
Steve Strates
  • 433
  • 3
  • 13
  • 3
    yeah, but have to wait 3-4 years until devices with API below 26 become rarely used – user924 Jan 28 '18 at 18:32
  • 5
    @user924: Depends on your use case. If you're writing an app for yourself, or a controlled environment (small team), you could do this today. Good to know about. – LarsH May 14 '18 at 18:06
9

There is an alternate way. When you open the file for the first time save the lastModified date, before you modify 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 have done this since the file was created, then you will have the createdDate as the lastModified date all the time.

coolcool1994
  • 3,704
  • 4
  • 39
  • 43
4

Having backward compatibility in mind I would rather use the following:

fun getLastModifiedTimeInMillis(file: File): Long? {
    return try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getLastModifiedTimeFromBasicFileAttrs(file)
        } else {
            file.lastModified()
        }
    } catch (x: Exception) {
        x.printStackTrace()
        null
    }
}

@RequiresApi(Build.VERSION_CODES.O)
private fun getLastModifiedTimeFromBasicFileAttrs(file: File): Long {
    val basicFileAttributes = Files.readAttributes(
        file.toPath(),
        BasicFileAttributes::class.java
    )
    return basicFileAttributes.creationTime().toMillis()
}

alternatively, if you are dealing with jpg, jpegs you can use ExifInterface

Lukas
  • 1,216
  • 12
  • 25