How to get file last modified time in Java?

He class named File of the 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.


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

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: ");


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