Sometimes we need to get the file last modified date in Java, usually for listeners like JBoss config file changes hot deployment. java.io.File
class lastModified()
returns last modified date in long, we can construct date object in human readable format with this time.
Java File last modified date
A simple example showing how to get file last modified date in java.
package com.journaldev.files;
import java.io.File;
import java.util.Date;
public class FileDate {
public static void main(String[] args) {
File file = new File("employee.xml");
long timestamp = file.lastModified();
System.out.println("employee.xml last modified date = "+new Date(timestamp));
}
}
Output of the above program is:
employee.xml last modified date = Fri Dec 07 14:19:10 PST 2012
If file doesn’t exists, lastModified()
returns 0L, if I delete employee.xml then the output is:
employee.xml last modified date = Wed Dec 31 16:00:00 PST 1969
Above time is the start of time in java. The 0L in java time. That’s all about finding the last modified time of a file in java.