How to get the last modification date of a File using Java

The last modification date can be accessed using the lastModified method from the File Object

Call the following method:

	
long lastModified = file.lastModified();
		 

Here is an example for getting the last modification Date of a file in Java.

The code first creates a File Object with the file path; then it calls the lastModified method to get the a long for the date. Finally, the long is used to create a date that is then formatted and printed in the output.

1- Create the following java file:

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

public class FileLastModificationDate {

	public static void main(String[] argv){

		File file = new File( "V:/tmp/test.txt" );

		long lastModified = file.lastModified();

		String pattern = "yyyy-MM-dd hh:mm aa";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		
		Date lastModifiedDate = new Date( lastModified );

		System.out.println( "The file " + file + " was last modified on " + simpleDateFormat.format( lastModifiedDate ) );
	}
	
}

The output will be:

The file V:\tmp\test.txt was last modified on 2017-01-16 04:54 PM


References:

java io