0

There are multiple files in a single folder. How do I get only specific file ( file name with modified date and time ), which is modified on only today's date?

Rao
  • 20,781
  • 11
  • 57
  • 77
Saran
  • 1
  • 1
  • 3
    Please post some code we can help you with. If you are looking for some code please use search options or Goolge search. It took me one minute to find a lot of ideas....http://stackoverflow.com/questions/2723838/determine-file-creation-date-in-java http://stackoverflow.com/questions/203030/best-way-to-list-files-in-java-sorted-by-date-modified http://www.avajava.com/tutorials/lessons/how-do-i-filter-files-based-on-their-last-modified-dates.html – Łukasz Motyczka Feb 19 '16 at 12:05

1 Answers1

1
public static void main(String[] args) {
    long now = new Date().getTime();
    long todayMidnightTS = midnightTimeStamp(now);
    //Mind the path used to list children... And also, you may want to retain only child files if that's required.
    List<File> filesModifiedToday = Arrays.asList(new File(".").list()).stream().map(fileName -> new File(fileName)).filter(file -> todayMidnightTS == midnightTimeStamp(file.getCanonicalFile().lastModified())).collect(Collectors.toList());
}
//Returns the timestamp at midnight of the given date time stamp.
//If you use commons-lang, you could call DateUtils.trunc
static long midnightTimeStamp(long dateTS) {
    return dateTS - (dateTS % (24 * 60 * 60000));
}
ernest_k
  • 44,416
  • 5
  • 53
  • 99