How to get file last modified time in Java?


He class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.

The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.

Example

Following Java program gets the last modified time of a directory −

import java.io.File;
import java.util.Date;
public class GettingLastmodifiedTime {
   public static void main(String args[]) {
      String filePath = "D://ExampleDirectory//";
      //Creating the File object
      File file = new File(filePath);
      //Getting the last modified time
      long lastModified = file.lastModified();
      Date date = new Date(lastModified);
      System.out.println("Given file was last modified at: ");
      System.out.println(date);
   }
}

Output

Given file was last modified at:
Wed Jul 03 19:20:50 IST 2019

Updated on: 02-Aug-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements