How do I filter files based on their last-modified dates?
Author: Deron Eriksson
Description: This Java tutorial describes how to filter files based on their last-modified dates.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
The AgeFileFilter in the ApacheSW Commons IOS library is a filter that can be used to filter files based on their last-modified dates. The AgeFileFilter class can take a 'cutoff' Date, long, of File (using the file's last-modified time as the reference) in its constructor. It can take a boolean as a second argument to the constructor. The boolean value specifies whether the returned files should come before or after the cutoff date/time. This is demonstrated by the AgeFileFilterTest class. AgeFileFilterTest.javapackage test; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.Date; import java.util.GregorianCalendar; import org.apache.commons.io.filefilter.AgeFileFilter; public class AgeFileFilterTest { public static void main(String[] args) throws IOException { File directory = new File("stuff"); GregorianCalendar cal = new GregorianCalendar(); cal.set(2008, 0, 15, 0, 0, 0); // January 15th, 2008 Date cutoffDate = cal.getTime(); System.out.println("All Files"); displayFiles(directory, null); System.out.println("\nBefore " + cutoffDate); displayFiles(directory, new AgeFileFilter(cutoffDate)); System.out.println("\nAfter " + cutoffDate); displayFiles(directory, new AgeFileFilter(cutoffDate, false)); } public static void displayFiles(File directory, FileFilter fileFilter) { File[] files = directory.listFiles(fileFilter); for (File file : files) { Date lastMod = new Date(file.lastModified()); System.out.println("File: " + file.getName() + ", Date: " + lastMod + ""); } } } The AgeFileFilterTest class first creates a directory (File) object to the "stuff" directory. It then creates a Date object for January 15th, 2008. Next, it lists all files in the "stuff" directory. It creates an AgeFileFilter object based on our Date object, and this is used to display the files in "stuff" that were last modified before January 15th, 2008. Following this, it creates an AgeFilter object based on our Date object but with "false" passed in as the second constructor argument. This is used to display the files in "stuff" that were last modified after January 15th, 2008. The console output from executing AgeFileFilterTest is shown below: ResultsAll Files File: test.txt, Date: Tue Mar 27 23:45:29 PDT 2007 File: test1.txt, Date: Sun Jan 27 06:09:53 PST 2008 File: test2.txt, Date: Sun Jan 27 06:09:49 PST 2008 File: test3.txt, Date: Sun Jan 27 06:09:58 PST 2008 File: TestClientSocket.java, Date: Mon May 28 21:44:26 PDT 2007 File: TestServerSocket.java, Date: Mon May 28 21:36:03 PDT 2007 Before Tue Jan 15 00:00:00 PST 2008 File: test.txt, Date: Tue Mar 27 23:45:29 PDT 2007 File: TestClientSocket.java, Date: Mon May 28 21:44:26 PDT 2007 File: TestServerSocket.java, Date: Mon May 28 21:36:03 PDT 2007 After Tue Jan 15 00:00:00 PST 2008 File: test1.txt, Date: Sun Jan 27 06:09:53 PST 2008 File: test2.txt, Date: Sun Jan 27 06:09:49 PST 2008 File: test3.txt, Date: Sun Jan 27 06:09:58 PST 2008 Related Tutorials:
|