Thursday, September 18, 2025
HomeLanguagesJavaJava Program to Change Last Modification Time of a File

Java Program to Change Last Modification Time of a File

Modifying the date of the file is possible through Java using the File class of Java i.e File.setLastModified() method

Java File Class

The File class is Java’s representation of a file or directory pathname. The File class contains several methods for working with the pathname, deleting and renaming files, creating new directories, listing the contents of a directory, and determining several common attributes of files and directories.

setLastModified Method

The setLastModified() function is a method that comes predefined in the Java File class. The function sets the last modified time of a file or directory. The function sets the last modified value of the file in milliseconds (long type).

Parameters – A string consisting of the new last-modified time in milliseconds.

Return value – It returns a boolean. (True if the operation succeeds, else false).

If the file doesn’t found in the system default date is printed as of 30/01/1970 as the default file is generated by the system.

Now to change the last modified date of a file, follow the given steps.  

  1. First, use the SimpleDateFormat(“mm/dd/yyyy”) constructor to make a new SimpleDateFormat class instance.
  2. Then, construct a String object with the “mm/dd/yyyy” format.
  3. Use the parse(String) method of the SimpleDateFormat class to create a new Date object with the date value of the String we created.
  4. Finally, use File.setLastModified(Date.getTime()) method to set the new “Last Modified” date of our file.

Java




// Java program to change last 
// modification time of a File
  
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
  
public class GFG {
    
    public static void main(String[] args)
    {
  
        try {
  
            // Create an object of the File class
            // Replace the file path with path of the file
            // who's "last modified" date you want to change
            File file = new File("/home/mayur/file.txt");
  
            // Create an object of the SimpleDateFormat
            // class
            SimpleDateFormat sdf
                = new SimpleDateFormat("mm/dd/yyyy");
  
            // Print the current "last modified" date of the
            // file
            System.out.println(
                "Original Last Modified Date : "
                + sdf.format((long)file.lastModified()));
  
            // Create a string containing a new date that
            // has to be set as "last modified" date
            String newLastModified = "10/10/2020";
  
            // Convert this new date into milliseconds
            // format (in long type)
            Date newDate = sdf.parse(newLastModified);
  
            // Set the new date as the "last modified"
            // date of the our file
            file.setLastModified(newDate.getTime());
  
            // Print the updated "last modified" date
            System.out.println(
                "Latest Last Modified Date : "
                + sdf.format(file.lastModified()));
        }
        catch (ParseException e) {
  
            // In case of any errors
            e.printStackTrace();
        }
    }
}


Output:

Java Program to Change Last Modification Time of a File

RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11835 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7053 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS