Java Program to Print the Last Modification Time of a Directory Last Updated : 09 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Last modification date of any Folder/Directories having multiple files in it can be displayed using Java. Using last Modified Time method in Java last modification time of any File in any Folder can be extracted. Approach: In a Folder, we have multiple files in it so there will be multiple dates of formation. Then to get the correct modified date for a folder is Difficult because there is a case where a file is created/modified and then deleted from the folder. So the approach to find the modified date for a folder is by iterating to each file in that folder and calculating the modified time/date for each file whichever time/Date will be small that will be considered as a modified date for a folder Below is the implementation of the above approach Java // Java Program to Print the last // modification time of a directory import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Create an object of the File class // Replace the file path with path of the file // who's "last modified" date you want to change File myFile = new File("/home/mayur/GFG.java"); long modifiedValue = myFile.lastModified(); Date modifiedDate = new Date(modifiedValue); System.out.println(modifiedDate); } } Output: Modified date/time for a folder: Here we use the Comparator to the compare the file Modified time with the others file in the same Folder Approach Put all files in one array named File[].Use the comparator to sort all the files in an array-based in the last Modification Date/Time in increasing order.Return the first index of the array which will return the modified date/Time for the folder. Java package com.BotArmy; // Java Program to Print the last // modification time of a directory import java.io.*; import java.util.*; class time { public static Date getLastModified(File directory) { // Accessing each file from a folder in File array: File[] files = directory.listFiles(); if (files.length == 0) return new Date(directory.lastModified()); // Sorting each file based on its lastModified // Time using the comparator Arrays.sort(files, new Comparator<File>() { public int compare(File o1, File o2) { // returning the file modified time // in the increasing way return Long.valueOf(o2.lastModified()) .compareTo( o1.lastModified()); // latest 1st } }); return new Date(files[0].lastModified()); } } class Main { public static void main(String[] args) { // taking the file directory location File directory = new File( "/home/mayur/"); Date myDate = time.getLastModified(directory); System.out.println("date: " + myDate); } } Comment More infoAdvertise with us Next Article Java Program to Print the Last Modification Time of a Directory R rohit2sahu Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Change Last Modification Time of a File Modifying the date of the file is possible through Java using the File class of Java i.e File.setLastModified() method Java File Class The File class is Javaâs representation of a file or directory pathname. The File class contains several methods for working with the pathname, deleting and renaming 3 min read Java Program to Get the Size of a Directory The size of files in Java can be obtained using the File class. The in-built function of 'fileName.length()' is used to find size of file in Bytes. The directory may contain 'N' number of files, for calculating the size of the directory summation of the size of all the files is required. length() Me 2 min read Checking Last Modification of a File On the Server in Java In Java, we have different classes like File, URL which provides the functionality to read the Attributes of file like creation time, last access time, and last modified time. Method 1(Using BasicFileAttributes) This example uses java.nio.* to display the metadata of the file and other file attribut 3 min read Java Program to Get the Creation Time of a File Use java.nio package for extracting the creation date and time of any file through java. To extract the date and time of the file use BasicFileAttributes class. java.nio package helps us to get the creation time, last access time, and last modified time, it works for both file and directory. Approac 2 min read Java Program to Display all the Directories in a Directory The directory is the organizing structure of the computer file system which is not only responsible for storing files but also to locate them on the memory. File organization structure is a hierarchical structure involving parent and child relationships just like what is there in tree data structure 3 min read Like