1

I have files that end with unix timestamp such:

Product.txt.1500256801
Product.txt.1500260400
Product.txt.1500264001
Product.txt.1500267601
...

In 5 days the files will be to 120 files For exm: I want to search any word in the files but not all 5 days only 3 days From 2017-07-05 To 2017-07-08 These two date and the word i search for entered by jsf page The problem not jsf page or how to find word in files The real problem when take that fromDate and toDate how only i limited the search on that date (for acknowledgment: in these 3 days i will have 72 files, i want to search 72 files not all 124 files)

I tried this idea: Read all timestamp to list of string

for(int i=0; i<listOfExt; i++)
{
stringList = listOfExt.get(i).replaceAll(regex,"$1");
}
lista1.add(stringList);//where. I get only from files name the suffix 1500256801,1500260400, and so on ...

I then create two new lists (lista2 and lista3) And read the two dates entered by jsf page to two string then i compare

if(lista1.contains(fromDate)&&lista1.contains(toDate))
{
lista2 = lista1.subList(lista1.indexOf(startDate),lista1.indexOf(endDate)+1);
lista3.add(lista2.toString);
}
else
{
//some thing such: the user entered date not created its file to now
}
Wasfy
  • 73
  • 8
  • I don't see any jsf here... – Kukeltje Jul 20 '17 at 14:55
  • If you know the issue is with the JSF page and its controller, why don't you show us the code of those? Try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) of the problem. – Robin Topper Jul 20 '17 at 14:55
  • Note that the problem not from jsf page as i successfully implemented that . – Wasfy Jul 20 '17 at 15:00
  • In my managed bean i receive the two date without problem and until i can convert them to timestamp – Wasfy Jul 20 '17 at 15:01
  • I want to solve the problem even without jsf page. – Wasfy Jul 20 '17 at 15:02
  • How are these timestamps created? When you search from 2017-07-05 to 2017-07-08, are you considering that the dates are in what timezone (or in UTC)? –  Jul 20 '17 at 16:02
  • First i convert them from date to time stamp so i can compare them with suffix timestamp of files but not success. – Wasfy Jul 20 '17 at 18:42
  • Hi @Hugo, second i tried to read the fromDate and toDate and the suffix of file as date and convert them to string to compare. – Wasfy Jul 20 '17 at 19:00
  • Time zone that files written is GMT +03:00 is Asia/Baghdad (AST) – Wasfy Jul 20 '17 at 19:31

1 Answers1

1

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().