Java file last modified date


 

Java file last modified date

In this section, you will learn how to get the last modification date and time of any file or a directory.

In this section, you will learn how to get the last modification date and time of any file or a directory.

Java file last modified date

In this section, you will learn how to get the last modification date and time of any file or a directory.

Java IO has provide such a useful tools that without even opening the file, using the File class, you can find information about a file or directory, you can rename or delete it. You can use the lastModified() method to get the file?s last modified timestamps. This method will return the time in milliseconds as long value, you may format it with SimpleDateFormat to make it a readable format.

Here is the code:

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

public class FileLastModified {
	public static void main(String[] args) {
		File f = new File("C:/file.txt");
		long datetime = f.lastModified();
		Date d = new Date(datetime);
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
		String dateString = sdf.format(d);
		System.out.println("The file was last modified on: " + dateString);
	}
}

Through the method lastModified(), you can find the last modified timestamp of any file.

Output:

The file was last modified on: 14-09-2010 05:08:32

Ads