Java Program to Change Last Modification Time of a File
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.
- First, use the SimpleDateFormat("mm/dd/yyyy") constructor to make a new SimpleDateFormat class instance.
- Then, construct a String object with the “mm/dd/yyyy” format.
- Use the parse(String) method of the SimpleDateFormat class to create a new Date object with the date value of the String we created.
- 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:
Similar Reads
Java Program to Print the Last Modification Time of a Directory Last modification date of any Folder/Directories having multiple files in it can be displayed using Java. Using last Modified Time method in Java last modification time of any File in any Folder can be extracted. Approach:  In a Folder, we have multiple files in it so there will be multiple dates o
3 min read
Checking Last Modification of a File On the Server in Java In Java, we have different classes like File, URL which provides the functionality to read the Attributes of file like creation time, last access time, and last modified time. Method 1(Using BasicFileAttributes) This example uses java.nio.* to display the metadata of the file and other file attribut
3 min read
Java Program to Get the Creation Time of a File Use java.nio package for extracting the creation date and time of any file through java. To extract the date and time of the file use BasicFileAttributes class. java.nio package helps us to get the creation time, last access time, and last modified time, it works for both file and directory. Approac
2 min read
Java Program to Make a File Read-Only Read-Only is the file attribute that the operating system assigns to the file. When the file is flagged as read-only, it means that it can only be opened or read, one cannot change the name of the file, can not rewrite or append the content of the file, and also cannot delete the file. Method 1: Usi
2 min read
Java Program to Create a File with a Unique Name Java Programming provides a lot of packages for handling real-time problems but in our case, we need to create a File with a unique name. For this, there are different solutions in Java. Like Using timestamp series as the file name or creating a unique random number and then assigning that number as
6 min read