To get file last modification date we can use the lastModified()
method of the File
class. This method returns a long
value. After getting this value you can create an instance of java.util.Date
class and pass this value as the parameter. This Date
will hold the file’s last modification date.
package org.kodejava.io;
import java.io.File;
import java.util.Date;
public class FileLastModificationDate {
public static void main(String[] args) {
// Create an instance of file object.
File file = new File("README.md");
if (file.exists()) {
// Get the last modification information.
long lastModified = file.lastModified();
// Create a new date object and pass last modified
// information to the date object.
Date date = new Date(lastModified);
// We know when the last time the file was modified.
System.out.println(date);
}
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024