Android:如何获取文件的创建日期?


75

这是我的代码:

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..?
    }
}

6
如果要创建时间,为什么要接受修改时间?stackoverflow.com/questions/2723838/...
M按照

Answers:


192

该文件的创建日期不可用,但是您可以获取最后修改日期

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

System.out.println("File last modified @ : "+ lastModDate.toString());

1
使用最后修改的HTTP标头。
AhmetB-Google 2011年

5
正如@CommonsWare指出的,lastModified时间不是创建时间。创建时间不可用。
米格尔

1
请先阅读,由于无法使用创建时间,因此可以选择使用“最后修改”。
Jorgesys

2
我想念的一点信息是File.lastModified是本地时间还是UTC的明确规范。共识是它是UTCstackoverflow.com/questions/5264339/…,这也是我在半现代系统中所期望的。但是,为什么Oracle的规格developer.android.com/reference/java/io/…不能明确说明呢?“ ...自1970年1月1日午夜以来以毫秒为单位。” 如果有足够的偏执狂,可以将其解释为1970年1月1日UTC午夜,或1970年1月1日当地UTC午夜。
RenniePet 2014年

2
自1970年1月1日UTC以来,它以毫秒为单位存储,但是您不必担心-它是“时代”的标准定义。为您显示的任何日期都可以以本地时间或UTC时间或任何时区显示。
BeccaP 2015年

23

这就是我会做的

// 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();
   }
}

2
+1我最喜欢这个答案。我在线程中运行了它,效果很好。另外,请喜欢var名称MAX_FILEAGE:P
Ryan R

6
您没有回答这个问题。这是文件的最后修改日期。
Stef

22

文件创建日期不是JavaFile类公开的可用数据。我建议您重新考虑自己在做什么,并更改计划,这样就不再需要它了。


1
错了 文件支持此功能,developer.android.com
模糊

11
@Fuzzy:那是最后修改的时间。修改文件后,它不是文件创建时间。
CommonsWare 2012年

10

从API级别26开始,您可以执行以下操作:

File file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long createdAt = attr.creationTime().toMillis();

1
是的,但是必须等待3-4年,直到API低于26的设备变得很少使用
user924

2
@ user924:取决于您的用例。如果您要为自己或受控制的环境(小型团队)编写应用程序,今天就可以这样做。很高兴知道。
LarsH

8

还有另一种方法。首次打开文件时,请在修改文件夹之前保存lastModified日期。

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

然后当您关闭文件时

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

如果自文件创建以来就这样做了,那么您将一直将createdDate作为lastModified日期。


有趣的想法,尽管只有在您的应用创建了文件的情况下它才起作用。
LarsH

3

考虑到向后兼容性,我宁愿使用以下内容:

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()
}

或者,如果您正在处理jpg,jpeg,则可以使用ExifInterface

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.