Open In App

Java Program to Print the Last Modification Time of a Directory

Last Updated : 09 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
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

  1. Put all files in one array named File[].
  2. Use the comparator to sort all the files in an array-based in the last Modification Date/Time in increasing order.
  3. 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);
    }
}




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads