I'd use the new Date/Time API, because the old classes (Date
, Calendar
and SimpleDateFormat
) have lots of problems and design issues, and they're being replaced by the new APIs.
If you're using Java 8, there's the new java.time API, which comes natively within the JDK.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
The code below works for both.
The only difference is the package names (in Java 8 is java.time
and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp
), but the classes and methods names are the same.
As I understood, your files are generated hourly. I took the timestamps and found that they correspond to the following dates (in UTC):
2017-07-17T02:00:01Z
2017-07-17T03:00:00Z
2017-07-17T04:00:01Z
2017-07-17T05:00:01Z
I suppose that it's a scheduled job that does it, and note that sometimes it takes 1 second to generate it (giving hours like 05:00:01
). But as the files are generated hourly, I believe we can discard the seconds.
First, we need to get the start and end dates, and I'm assuming they come as strings in the format 2017-07-05
- that's how you specified in the question.
I'm also assuming that these dates correspond to UTC (but we can change the code if it's not, more on that below):
// start date (set the time to start of day)
ZonedDateTime from = LocalDate.parse("2017-07-05").atStartOfDay(ZoneOffset.UTC);
// end date (set the time to 11 PM)
ZonedDateTime to = LocalDate.parse("2017-07-08").atTime(23, 0).atZone(ZoneOffset.UTC);
If the dates are in another format, you can use a DateTimeFormatter
to parse them:
// parse date in day/month/year format
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
ZonedDateTime from = LocalDate.parse("05/07/2017", fmt).atStartOfDay(ZoneOffset.UTC);
Then you get the respective timestamps from each date. But as we are ignoring the seconds, I also use truncatedTo
method to set the seconds to zero. I also divide by 1000 because your timestamps are in seconds, but the API returns it in milliseconds:
// get start and end timestamps
long start = from.toInstant().truncatedTo(ChronoUnit.MINUTES).toEpochMilli() / 1000;
long end = to.toInstant().truncatedTo(ChronoUnit.MINUTES).toEpochMilli() / 1000;
Now you traverse through your folder and check if the timestamp in the file name is between start
and end
- taking care of getting rid of the extra second (truncate all to zero):
// open directory that contains the files
File dir = new File("/your/folder/name");
// get the files with the timestamps between start and end
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// extract number from file name
if (name.startsWith("Product.txt.")) {
// divide by 10 to eliminate extra second (value is rounded as it's an int)
// then multiply by 10 again to get timestamp of respective hour
int timestamp = (Integer.parseInt(name.replace("Product.txt.", "")) / 10) * 10;
System.out.println(timestamp);
return start <= timestamp && timestamp <= end;
}
return false;
}
});
Now you have a files
array with all the files you need.
If instead of UTC you want the dates in a specific timezone, you can use the ZoneId
class:
// timezone
ZoneId zone = ZoneId.of("Asia/Baghdad");
And replace ZoneOffset.UTC
by the zone
variable above.
The API uses IANA timezones names (always in the format Region/City
, like America/Sao_Paulo
or Europe/Berlin
).
Avoid using the 3-letter abbreviations (like CST
or PST
) because they are ambiguous and not standard.
You can get a list of available timezones (and choose the one that fits best your system) by calling ZoneId.getAvailableZoneIds()
.