Set last modified time of a file or directory


/*
  Set last modified time of a file or directory
  This Java example shows how set last modified time of a particular file or directory
  using setLastModified method of Java File class.
*/

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

public class SetFileLastModifiedTime {

 
public static void main(String[] args) {
   
   
//create file object
   
File file = new File("C://FileIO//ReadText.txt");
   
    Date lastModified =
new Date(file.lastModified());
    System.out.println
("Last modified time of file is : " + lastModified );
   
   
/*
     * To set last modified time of a file, use
     * boolean lastModifiedTime(long time) method of Java File class.
     *
     * Here, time is milliseconds since Jan 01, 1970 00:00:00.
     *
     * This method returns true if operation was successful, false otherwise.
     */
    
    
lastModified = new Date();
    
    
//set last modified time to current time
    
boolean blnSuccess = file.setLastModified(lastModified.getTime());
    
     System.out.println
("Was last modified time set successfully ?:" + blnSuccess);
    
     System.out.println
("File last modification date is : " +
      
new Date(file.lastModified()));
    
 
}
}

/*
Typical output would be
Last modified time of file is : Sun Jan 06 16:23:55 EST 2008
Was last modified time set successfully ?:true
File last modification date is : Mon Jan 07 20:50:00 EST 2008
*/