这是我解决这个问题的方法。首先,我创建了两个帮助函数,一个从文件名中检索结束日期,另一个检索文件系统modified 时间。我认为没有必要检查created 时间,因为创建时间总是小于或等于修改日期时间。
获取文件名结束日期的函数:
private LocalDate getEndDate(File file) {
String fileName = file.getName();
String fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf("."));
String[] fileNameChunks = fileNameWithoutExtension.split("_");
String endDateAsString = fileNameChunks[fileNameChunks.length - 1];
return LocalDate.parse(endDateAsString, DateTimeFormatter.ofPattern("yyyyMMdd"));
}
接下来,检索文件系统modified 日期的函数。为此,我使用Files#getLastModifiedTime 来检索modified 日期:
private LocalDate getLastModifiedDate(File file, ZoneId zoneId) {
try {
return ZonedDateTime
.ofInstant(Files.getLastModifiedTime(file.toPath()).toInstant(), zoneId)
.toLocalDate();
} catch (IOException e) {
throw new RuntimeException("Could not read file attributes: " + file.getAbsolutePath());
}
}
最后,它只是使用调用这些函数并执行验证:
boolean isTodaysFile(File file) {
Clock systemUTCClock = Clock.systemUTC();
LocalDate localDateNow = LocalDate.now(systemUTCClock);
LocalDate fileEndDate = getEndDate(file);
// first check - validate that the file name's end date is today
if (!fileEndDate.isEqual(localDateNow)) {
return false;
}
LocalDate lastModifiedDate = getLastModifiedDate(file, systemUTCClock.getZone());
// second check - validate that the modified that is today
// no need to check the creation date, since creation date is always less or equal to the last modified date
return lastModifiedDate.equals(localDateNow);
}
我正在使用 Clock.systemUTC() 并基于此实例化所有日期,以确保我们始终使用 UTC。
LocalDate.now(systemUTCClock)
systemUTCClock.getZone()
如果目录中的输入文件是:
ABC_STOCK_List_20200220_20200220.csv
ABC_STOCK_List_20200219_20200219.csv
ABC_STOCK_List_20200218_20200218.csv
ABC_STOCK_List_20200217_20200217.csv
ABC_STOCK_List_20200305_20200305.csv
在撰写本文时,当前日期为 03-05-2020。调用findLatestFile时的输出文件为:
ABC_STOCK_List_20200217_20200305.csv
注意事项:
- 我没有对文件名格式进行任何验证。如果格式有问题,您在检索结束日期时可能会遇到一些错误。