HHH
How to get of lastmodified file list between two times in java?
2 Answer(s)      4 years and 4 months ago
Posted in : Java Beginners

Hi, here is a code that list the last modified files in a directory between two dates. This is working well but is it possible to do the same thing between two times like get the last modified file bet 02:00:00am and 01:00:00am (so the interval time is 23h)

import java.io.*;
import java.text.*;
import java.util.*;

public class FileFilterDateIntervalUtils implements FilenameFilter {
    String dateStart;
    String dateEnd;
    SimpleDateFormat sdf;

public FileFilterDateIntervalUtils(String dateStart, String dateEnd) {
    this.dateStart = dateStart;
    this.dateEnd = dateEnd;
    sdf = new SimpleDateFormat("yyyy-MM-dd");
}

public boolean accept(File dir, String name) {
    Date d = new Date(new File(dir, name).lastModified());
    String current = sdf.format(d);
    return ((dateStart.compareTo(current) < 0
            && (dateEnd.compareTo(current) >= 0)));
}

}

// then this

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileSortDateInterval {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        FileFilterDateIntervalUtils filter =
            new FileFilterDateIntervalUtils("2010-01-04", "2011-01-20");
        File folder =  new File("G:/Temp");
        File files[] = folder.listFiles(filter);
        for (File f : files) {
            System.out.println(f.getName() + " "
                    + sdf.format(new Date(f.lastModified())));
        }
    }
}

Google gave me these codes Thank you

Advertisement
View Answers

January 21, 2011 at 12:36 PM


Hi Friend,

Check this:

import java.io.*;
import java.text.*;
import java.util.*;

 class FileFilterDateIntervalUtils implements FilenameFilter { 
     String dateStart;
     String dateEnd;
     SimpleDateFormat sdf;

public FileFilterDateIntervalUtils(String dateStart, String dateEnd) {
    this.dateStart = dateStart;
    this.dateEnd = dateEnd;
    sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
}

public boolean accept(File dir, String name) {
    Date d = new Date(new File(dir, name).lastModified());
    String current = sdf.format(d);
    return ((dateStart.compareTo(current) < 0
            && (dateEnd.compareTo(current) >= 0)));
}

} 
public class FileSortDateInterval {
    public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
    FileFilterDateIntervalUtils filter = new FileFilterDateIntervalUtils("2010-11-20 11:30:00 AM", "2010-11-30 11:15:00 AM");
    File folder = new File("C:/roseindia");
    File files[] = folder.listFiles(filter);
    for (File f : files) {
        System.out.println(f.getName() + " " + sdf.format(new Date(f.lastModified())));
        }
 } 
}

Hope that it will be helpful for you.

Thanks



January 21, 2011 at 5:45 PM


H , thank you very much for youe help.

it works










Related Tutorials/Questions & Answers:
Advertisements