0

Suppose we have a bunch of files in a list and their names consisted of file + a date and time format in the form of dd_mm_yyyy_hour_minute_second. It would look something like this:

[
file_03_12_20_23_11_42
file_29_11_20_23_11_42
file_28_11_20_23_31_41
file_11_11_19_23_31_12
file_23_06_19_23_11_35
file_02_12_20_23_50_31
file_30_11_20_23_41_12
...
]

Is there a way to filter through this list to keep anything within the last week from the current date?

I know how to grab the current date with either the Date class or Calender class, but I have no idea how to keep the last 7 days of these files. This is List of type File, but the file names are going to be strings when using file.toString().

stackerstack
  • 243
  • 4
  • 16
  • If you had a list of Dates, would you know how to select those that are within 7 days from today? – PM 77-1 Dec 03 '20 at 22:49
  • Check https://stackoverflow.com/questions/36621028/how-can-i-get-last-7-days-from-current-day-in-ascending-order-in-java and https://stackoverflow.com/questions/16982056/how-to-get-the-date-7-days-earlier-date-from-current-date-in-java – Arvind Kumar Avinash Dec 03 '20 at 22:52
  • 1
    Yes, there is a way. --- [Why is “Is it possible to…” a poorly worded question?](https://softwareengineering.meta.stackexchange.com/q/7273/202153). – Andreas Dec 03 '20 at 22:53

1 Answers1

4

Say you have a directory of files, like so:

Users/Shash/Desktop/Files
 ├── file_02_12_20_23_50_31
 ├── file_03_12_20_23_11_42
 ├── file_11_11_19_23_31_12
 ├── file_23_06_19_23_11_35
 ├── file_28_11_20_23_31_41
 ├── file_29_11_20_23_11_42
 └── file_30_11_20_23_41_12

You could use parse LocalDate's from the file names and filter the ones that are before the LocalDate corresponding to one week ago:

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

class Main {
    public static void main(String[] args) {
        File fileDirectory = new File("/Users/Shash/Desktop/Files");
        List<File> filesWithDates =
                Arrays.asList(Objects.requireNonNull(fileDirectory.listFiles()));

        System.out.println("Before filtering:");
        filesWithDates.forEach(f -> System.out.println(f.getName()));

        List<File> filesWithDatesUpToWeekOld = filterOldFiles(filesWithDates);

        System.out.println("After filtering:");
        filesWithDatesUpToWeekOld.forEach(f -> System.out.println(f.getName()));
    }

    private static List<File> filterOldFiles(List<File> filesWithDates) {
        List<File> filteredFiles = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                "'file'_dd_MM_yy_HH_mm_ss", Locale.ENGLISH);
        LocalDate oneWeekAgo = LocalDate.now().minus(1, ChronoUnit.WEEKS);
        for (File fileWithDate : filesWithDates) {
            LocalDate dateTime = LocalDate.parse(fileWithDate.getName(), formatter);
            if (!dateTime.isBefore(oneWeekAgo)) {
                filteredFiles.add(fileWithDate);
            }
        }
        return filteredFiles;
    }
}

Output:

Before filtering:
file_30_11_20_23_41_12
file_03_12_20_23_11_42
file_23_06_19_23_11_35
file_28_11_20_23_31_41
file_11_11_19_23_31_12
file_29_11_20_23_11_42
file_02_12_20_23_50_31
After filtering:
file_30_11_20_23_41_12
file_03_12_20_23_11_42
file_28_11_20_23_31_41
file_29_11_20_23_11_42
file_02_12_20_23_50_31
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • @ShashSinha so sorry about this, but I accepted your answer and upvoted it, I'm relatively new to this stuff sorry about that! May I ask for a favor though, could you make it so that the return type is List instead of List? – stackerstack Dec 04 '20 at 17:17
  • No problem, I will do it at around 7pm PST today. – Sash Sinha Dec 04 '20 at 18:17
  • Also, I made a grave mistake, the way the files shown are actually in the format of: dd_mm_yy_hour_minute_second, not dd_mm_yyyy_hour_minute_second. Would this change anything or could i just change the formatter to: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("'file'_dd_MM_yy_HH_mm_ss", Locale.ENGLISH); – stackerstack Dec 04 '20 at 19:39
  • @stackerstack done. – Sash Sinha Dec 05 '20 at 02:09
  • 1
    Thank you!, Do you happen to know how to get the time modified with the zip files and the size of them? – stackerstack Dec 06 '20 at 00:35
  • [getting-the-last-modified-date-of-a-file-in-java](https://stackoverflow.com/questions/4363197/getting-the-last-modified-date-of-a-file-in-java) and [how-to-get-the-size-of-a-file-in-megabytes-in-java](https://stackoverflow.com/questions/8989746/how-to-get-the-size-of-a-file-in-megabytes) might help otherwise looks for similar questions using the search engine of your choice. I.e., Google, DuckDuckGo, Bing etc. – Sash Sinha Dec 06 '20 at 00:41
  • Is there a way we could continue this in conversation? I have a couple more questions to ask you if thats okay, you've just been so helpful so far and i'm really appreciating it! – stackerstack Dec 06 '20 at 20:28