Java Program to Get Last Modification Date of a File
Last Updated :
21 Feb, 2022
The last modifying date of the file can get through Java using the File class of Java i.e File.LastModified() 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.
lastModified() method:
The class named File of java.io package represents both file and directory in the system. This class provides various methods to operate on files and directories. It contains a method called lastModified() which returns the last modified date of a file or directory in form of a long millisecond epoch value, which can be made readable using format() method of SimpleDateFormat class. The output can be displayed in any desired format using SimpleDateFormat class. This method can be used in all Java versions. lastModified() method returns 0l if the file does not exist. 0l means the number zero of type long. It uses this constructor to instantiate a Date that refers to zero milliseconds after the 'epoch' i.e, January 1, 1970, 00:00:00 GMT or January 1, 1970, 05:30 AM IST.
Function signature:
public long lastModified()
Now to get 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.LastModified() method to set the new “Last Modified” date of our file.
Note: The program does not run in an online IDE. Please use an offline IDE and then set the path of the file.
Java
// Java program to demonstrate
// last modified time of a file
// using lastModified() method
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
class GFG {
public static void main(String[] args)
{
// Creating two instances of file class
// file1.txt exists in the system
File file1 = new File("/home/mayur/GFG.java");
// file2.txt does not exist in the system
File file2 = new File("/home/mayur/file.txt");
// last modified returns date in milliseconds
long time1 = file1.lastModified();
long time2 = file2.lastModified();
// Convert milliseconds into readable date time
// format any desirable format can be achieved using
// SimpleDateFormat
DateFormat sdf
= new SimpleDateFormat("MMMM dd, yyyy hh:mm a");
System.out.println("GFG.java modified date is : "
+ sdf.format(time1));
System.out.println("file.txt modified date is : "
+ sdf.format(time2));
}
}
Output:

File1 exists in the system while File2 does not exist in the system. Therefore, in the case of file2, 0l is returned while in the case of file1 the last modified date is returned.
Similar Reads
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
3 min read
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 Extract Last two Digits of a Given Year As the name suggests where there is an execution to be operated required to deal with digits of the number, the modulo operator plays a vital role. Here the goal is to extract the last digits of a number. So making the problem easier to think about what if the goal is to extract the last digit from
3 min read
Java Program to Get the Basic File Attributes Basic File Attributes are the attributes that are associated with a file in a file system, these attributes are common to many file systems. In order to get the basic file attributes, we have to use the BasicFileAttributes interface. This interface is introduced in 2007 and is a part of the nio pack
3 min read