How to get last modification date of file in java

This example shows you how to get the last modification date of a file in java.

To get file’s last modification date you can use the lastModified() method of the File object. This method returns a long value, this long value can be used to get the Date object by passing as millisecond to java.util.Date class.

Below is the example of code which get the last modification date of a file in java.

package com.java.connect.io;

import java.io.File;
import java.util.Date;

//last modification date of file in java

public class FileLastModificationDateExample {
	public static void main(String[] args) {
		// Get the file object
		File file = new File("download_report.txt");

		// Get the last modification information
		Long lastModified = file.lastModified();

		// Get the Date object by passing last modified long value
		Date date = new Date(lastModified);

		System.out.println("The last modification date of file is : " + date);
	}
}

The output of the above example will be as:

The last modification date of file is : Thu Jan 01 00:00:00 GMT 1970