shuniar
shuniar

Reputation: 2622

Java Mask Formatting for Range of Dates

I am working on a project that pulls files from a third party FTP site where the files are defined by a mask, lets say 'XXX'MMddy.FILE where XXX is the vendor code and y is the last digit of the year.

My application is only concerned with the files matching the mask, but does not care about the actual date on the file. Currently, we are building a list of dates a month back with the Java Calendar object and iterating through it to create each possible mask until one matches or is determined invalid. I feel like there should be a better way to do this...

So to reiterate, in a consise question, is there a way to use the mask without creating an instance of each date to compare to the actual filename?

i.e. XXX04022.FILE conforms to XXXMMddy.FILE

Upvotes: 0

Views: 740

Answers (1)

GavinCattell
GavinCattell

Reputation: 3963

You could implement a FilenameFilter to use on File.listFiles:

public class YourFilenameFilter extends java.io.FilenameFilter
{
    public boolean accept(java.io.File file, String name)
    {
        boolean shouldAccept = false;
        if(name!=null)
        {
            java.util.regex.Pattern p = java.util.regex.Pattern.compile("[A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9].FILE");
            java.util.regex.Matcher m = p.matcher(name);
            if(m.matches()) {
                shouldAccept = true;
            }
        }
        return shouldAccept;
    }
}

Upvotes: 2

Related Questions