Write a java program to Get the last modification date and time of a file


This Java code defines a class FileLast_Modify with a main method that prints out the last modification date of a file named "file.txt" in the current working directory.

The try block creates a new File object using the string "file.txt", and retrieves the last modification time of the file using the lastModified method of the File class.

The lastModified method returns the time the file was last modified, in milliseconds since the epoch (January 1, 1970, 00:00:00 GMT). This value is then passed as a parameter to the Date constructor, which creates a new Date object representing the last modification time of the file.

Finally, the program prints out a message indicating when the file was last modified. Note that this code assumes that the file "file.txt" exists in the current working directory, and will throw a FileNotFoundException if the file cannot be found.

Source Code

import java.io.*;
import java.util.Date;
public class FileLast_Modify
{
	public static void main(String[] args)
	{
		File f = new File("file.txt");
		// lastModified is the predefined function.
		long last_modify = f.lastModified();
 
		System.out.println("File was Last Modified at : " + new Date(last_modify));
	}
}

Output

File was Last Modified at : Thu Jan 10 05:30:00 IST 1970