How to check the Date and time of Access (last modified) of a File using Java?

Java 8Object Oriented ProgrammingProgramming

You can get the last modified date of a file using the lastModified() method of the File class.

Example

Live Demo

import java.io.File;
import java.util.Date;

public class FindingDirectories {
   public static void main(String args[]) {
      String dir ="C:/movies";
      File directory = new File(dir);
      File[] fileList = directory.listFiles();

      for (File file : fileList) {
         System.out.println(file.getName());
         Date date = new Date(file.lastModified());
         System.out.println(date.toString());
      }
   }
}

Output

Arundhati HD.mp4
Fri Dec 01 17:16:22 IST 2017
Okka Ammai Tappa.mkv
Fri Nov 17 14:29:35 IST 2017
Padamati Sandhya Ragam.mp4
Wed Dec 06 09:53:07 IST 2017
raja
Published on 12-Jan-2018 06:22:03
Advertisements