I'm making a basic file browser and want to get the last modified date of each file in a directory. How might I do this? I already have the name and type of each file (all stored in an array), but need the last modified date, too.
You can get the last modified time of a File using File. lastModified() . To list all of the files in a directory, use File. listFiles() .
How to find the last modified date of a file in Java. Before Java 7 e.g. in JDK 1.6, You can use the lastModified() method of the File class to get the last updated time. Just create a File object by passing the path as String to File class and call the last modified method as shown below: File source = new File(".
Use parse(String date) method of the SimpleDateFormat class to create a new Date object with the date value of the String . Use File. setLastModified(Date. getTime()) method to set the new “Last Modified” date of the file.
To really know if the file has changed, you'd need to store a hash of the files (md5sum, sha256, etc) and for the comparison re-hash and compare the values of the 2 hashes.
As in the javadocs for java.io.File
:
new File("/path/to/file").lastModified()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With