Reputation:
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..?
}
}
Upvotes: 89
Views: 102882
Reputation: 384
Checks if a folder is at least 1 week old. @param folder The folder to check. @return true if the folder is at least 1 week old, false otherwise.
public static boolean isFolderOneWeekOldOrOlder(File folder) {
// Ensure the folder exists
if (folder == null || !folder.exists() || !folder.isDirectory()) {
return false;
}
try {
// Get the creation time of the folder
Path path = folder.toPath();
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
long creationTimeMillis = attrs.creationTime().toMillis();
// Calculate the threshold for 1 week ago
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -7); // 1 week ago
Date oneWeekAgo = calendar.getTime();
// Check if the creation time is before or equal to the one-week threshold
Date creationDate = new Date(creationTimeMillis);
return creationDate.before(oneWeekAgo) || creationDate.equals(oneWeekAgo);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Upvotes: 0
Reputation: 1215
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
Upvotes: 4
Reputation: 126455
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());
Upvotes: 220
Reputation: 513
Starting in API level 26, you can do this:
File file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long createdAt = attr.creationTime().toMillis();
Upvotes: 18
Reputation: 275
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();
}
}
Upvotes: 25
Reputation: 3804
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.
Upvotes: 9
Reputation: 1006614
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.
Upvotes: 22