Android: How to Get a File's Creation Date

Android: How to get a file's creation date?

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

Kotlin: How to get a file's (IMAGE) creation date?

This is work for me and thank you

// read image from gallary or you can give direct path

        getImage()// read image
val contentURI = data?.getData()
var imageFile = File(contentURI?.let { getRealPathFromURI(it) })
val lastModifiedDate: Date = Date(imageFile.lastModified())
println("creation time: " + attr.creationTime())

// get Native URI Function

private fun getRealPathFromURI(contentURI: Uri): String {
var getApplicationContext = getContext()?.getContentResolver();
var result: String
var cursor: Cursor? =
context?.getContentResolver()?.query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath().toString();
} else {
cursor.moveToFirst();
val idx: Int = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}

Get File Creation Time

    File dir = new File("/sdcard/Files/");
File[] files = dir.listFiles();
for (int i = 0; i < files.length; ++i){
long lastTime = files[i].lastModified();
Date nowDate = new Date();
long nowTime = nowDate.getTime();
if (nowTime - lastTime > 60*60*1000){
files[i].delete();
}
}

I hope it can help you.

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 FileAttributeViews 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

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).

sorting files based on its creation date in android

I want list of file based on my creation date.

As the two previous answers pointed out, you can sort the files according to the modification date:

file.lastModified()

But the modification date is updated e.g. in the instant of renaming a file. So, this won't work to represent the creation date.

Unfortunately, the creation date is not available, thus you need to rethink your basic strategy:

see an old answer of CommonsWare



Related Topics



Leave a reply



Submit