Java Get Files in Directory based on Cutoff Time using Apache Commons IO

Tags: FileUtils Apache Commons Apache Commons IO File Iterator AgeFileFilter

In this Java tutorial we learn how to get all Files in a directory that is older than or newer than a given cutoff time using FileUtils and AgeFileFilter class of the Apache Commons IO library.

How to add Apache Commons IO library to your Java project

To use the Apache Commons IO library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'commons-io:commons-io:2.8.0'

To use the Apache Commons IO library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.8.0</version>
</dependency>

To have more information about the Apache Commons IO library you can visit the library home page at commons.apache.org/proper/commons-io/

How to get Files in a Directory older than cutoff time in Java

For example we have a directory D:\TestData and we want to get all Files that have modification date older than one day ago we can write the code to iterate Files as below program.

IterateFilesWithAgeFileFilter1.java

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AgeFileFilter;

import java.io.File;
import java.util.Iterator;

public class IterateFilesWithAgeFileFilter1 {
    public static void main(String... args) {
        File directory = new File("D:\\TestData");

        // 24 hours * 60 minutes * 60 seconds * 1000 milliseconds is a number of milliseconds of one day
        long oneDayAgo = System.currentTimeMillis() - (24 * 60 * 60 * 1000);

        AgeFileFilter ageFileFilter = new AgeFileFilter(oneDayAgo);

        Iterator<File> iterator = FileUtils.iterateFiles(directory, ageFileFilter, ageFileFilter);

        while(iterator.hasNext()) {
            File file = iterator.next();
            System.out.println(file);
        }
    }
}
The output is:
D:\TestData\data.txt
D:\TestData\data1.txt

Or the following example to get all Files older than 1 hour ago.

IterateFilesWithAgeFileFilter2.java

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AgeFileFilter;

import java.io.File;
import java.util.Iterator;

public class IterateFilesWithAgeFileFilter2 {
    public static void main(String... args) {
        File directory = new File("D:\\TestData");

        // 60 minutes * 60 seconds * 1000 milliseconds is a number of milliseconds of one hour
        long oneHourAgo = System.currentTimeMillis() - (60 * 60 * 1000);

        AgeFileFilter ageFileFilter = new AgeFileFilter(oneHourAgo, true);

        Iterator<File> iterator = FileUtils.iterateFiles(directory, ageFileFilter, ageFileFilter);

        while(iterator.hasNext()) {
            File file = iterator.next();
            System.out.println(file);
        }
    }
}
The output is:
D:\TestData\data.txt
D:\TestData\data1.txt
D:\TestData\SubDirectory\data1.txt
D:\TestData\SubDirectory\data2.txt

How to get Files in a Directory newer than cutoff time in Java

To get Files newer than a given cutoff time we can provide different argument values to instantiate AgeFileFilter object as the following example code to get all Files newer than one day ago.

IterateFilesWithAgeFileFilter3.java

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AgeFileFilter;

import java.io.File;
import java.util.Iterator;

public class IterateFilesWithAgeFileFilter3 {
    public static void main(String... args) {
        File directory = new File("D:\\TestData");

        // 24 hours * 60 minutes * 60 seconds * 1000 milliseconds is a number of milliseconds of one day
        long oneDayAgo = System.currentTimeMillis() - (24 * 60 * 60 * 1000);

        AgeFileFilter ageFileFilter = new AgeFileFilter(oneDayAgo, false);

        Iterator<File> iterator = FileUtils.iterateFiles(directory, ageFileFilter, ageFileFilter);

        while(iterator.hasNext()) {
            File file = iterator.next();
            System.out.println(file);
        }
    }
}
The output is:
D:\TestData\data2.txt

Happy Coding 😊