How to get last modified date of file

package JavaIOExample;

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

/*
 * Here we will learn to check Last Modification Time of a File / Folder
 */
public class CheclLastModificationTimeOfFile {

	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the file path");
		String filePath = scanner.next();
		File fileToTest = new File(filePath);

		if (!fileToTest.isDirectory()) {
			System.out.println("File donated with given path is a File");
			Date dt = new Date(fileToTest.lastModified());
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss ");
			System.out.println("Last Modification time is :"
					+ sdf.format(dt).toString());
		} else {
			System.out.println("File donated with given path is a Directory");
			Date dt = new Date(fileToTest.lastModified());
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss ");
			System.out.println("Last Modification time is :"
					+ sdf.format(dt).toString());
		}

	}
}

 

By | 2012-09-21T19:41:56+00:00 September 21st, 2012|Code Base|0 Comments

About the Author:

Leave A Comment