Skip to content
Related Articles

Related Articles

Java Program to Change Last Modification Time of a File

Improve Article
Save Article
Like Article
  • Last Updated : 22 Oct, 2020

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 program to change last 
// modification time of a 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
                "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
            // Print the updated "last modified" date
                "Latest Last Modified Date : "
                + sdf.format(file.lastModified()));
        catch (ParseException e) {
            // In case of any errors


Java Program to Change Last Modification Time of a File

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!