Home Tutorial Java Core Files Java file last modified date

 
 

Share on Google+Share on Google+
Java file last modified date
Posted on: September 11, 2006 at 12:00 AM
Advertisement
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
Advertisement

Related Tags for Java file last modified date:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 11, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com