Java Examples - Setting File Date

Advertisements


Problem Description:

How to change the last modification time of a file ?

Solution:

This example shows how to change the last modification time of a file with the help of fileToChange.lastModified() and fileToChange setLastModified() methods of File class .

import java.io.File;
import java.util.Date;

public class Main {
   public static void main(String[] args) 
   throws Exception {
      File fileToChange = new File
      ("C:/myjavafile.txt");
      fileToChange.createNewFile();
      Date filetime = new Date
      (fileToChange.lastModified());
      System.out.println(filetime.toString());
      System.out.println
      (fileToChange.setLastModified
      (System.currentTimeMillis()));
      filetime = new Date
      (fileToChange.lastModified());
      System.out.println(filetime.toString());
   }
}

Result:

The above code sample will produce the following result.The result may vary depending upon the system time.

Sat Oct 18 19:58:20 GMT+05:30 2008
true
Sat Oct 18 19:58:20 GMT+05:30 2008


Advertisements
Advertisements