How to get Folder.lastModified() in Java? The option of recursive viewing of the directory in depth and taking the date of the last file is not suitable, since the directory may be empty.
2 answers
Yes, it seems the same as for any file:
new Date(new File("C:\\Windows").lastModified()) // Sat Feb 10 04:20:29 MSK 2018 |
Not certainly in that way. The fact is that if there are subdirectories in the parent directory, then with changes in them, the lastmodified parent does not change. Therefore, recursion has not done without:
private static long getLatestModifiedDate(File dir) { if (dir.isDirectory()) { File[] files = dir.listFiles(); long latestDate = 0; for (File file : files) { long fileModifiedDate = file.isDirectory() ? getLatestModifiedDate(file) : file.lastModified(); if (fileModifiedDate > latestDate) { latestDate = fileModifiedDate; } } return Math.max(latestDate, dir.lastModified()); } else { return dir.lastModified(); } } |