Get Last modification time of a file or directory


/*
  Get Last modification time of a file or directory
  This Java example shows how to determine last modification time of a particular
  file or directory using lastModified method of Java File class.
*/

import java.io.*;
import java.util.Date;

public class GetLastModificationTimeOfAFile {

 
public static void main(String[] args) {
   
   
//create file object
   
File file = new File("C://FileIO//demo.txt");
   
   
/*
     * To determine last modification time of a particular file or directory, use
     * long lastModified() method of Java File class.
     *
     * This method returns long representing the time file was last modified as
     * milliseconds since Jan 01, 1970 00:00:00. This
     * method returns 0L if the file does not exists or in case of IOException.
     *
     */
    
    
long lastModified = file.lastModified();
    
     System.out.println
("File was last modifed at : " + new Date(lastModified));
 
}
}

/*
Typical output would be
File was last modifed at : Sun Jan 06 18:24:01 EST 2008
*/