Java Program to Get Last Modification Date of a File - GeeksforGeeks

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.
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() methodimport 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.
 
arorakashish0911
Java-File Class
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Exceptions in Java
StringBuilder Class in Java with Examples
Convert a String to Character array in Java
Java Programming Examples
Implementing a Linked List in Java using Class
Convert Double to Integer in Java
How to Iterate HashMap in Java?