I need to display log files in a folder, which has date and time appended to the file name ,from a particular start date to a particular end date .how would I do that please help.
-
can you please provide any clue how should the file name looks like? – Rockstar Nov 19 '14 at 05:06
-
You could use String#compareTo to compare the filenames with the two bounds. – Thilo Nov 19 '14 at 05:06
-
LOG'BLK15_WP8_rev1'625'2014-09-15_17-08-05.130'WP8-BW05_RtHp'CW'3182713.xml – M Jun Nov 20 '14 at 03:42
-
in the above file name 2014-09-15{yyyy-mm-dd) is date and 17-08-05(hh-mm-ss) is time – M Jun Nov 20 '14 at 03:43
-
please help me solve this program – M Jun Nov 20 '14 at 03:44
2 Answers
You need to iterate over the list of files in the folder containing the logs, filtering out any files that are outside of the given date-time range; this sounds exactly like a job for the FileFilter API as another answerer too pointed out:
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles(myFileFilterObject);
Now your problem is to code the accept method of your filter.
If you want a quick and dirty solution, you could just use
String.CompareTo
to exploit simple string comparison (this requires your log files to be formatted as the year followed by the month as a number, then the date and finally the time).If you are familiar with Regular Expressions you could reuse Apache Commons'
RegexFileFilter
, but then you would have two problems.Or, you could do it the 'right' way (which might be too heavy-handed for your particular purpose, though) by extracting the relevant parts of the filename and comparing the dates as mentioned in Comparing date strings in Java:
Date has before and after methods and can be compared to each other.
You could also give Joda-Time a go.
How do I verify if today's date is in between date1 and date 3?
if(todayDate.after(historyDate) && todayDate.before(futureDate)) { // In between }

- 1
- 1

- 4,385
- 2
- 24
- 53
-
@user4268406 Your logs are formatted in the right way for #1 to work. Show us your code. – Yatharth Agarwal Nov 20 '14 at 03:46
You need to get the files from your log directory
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
The above will give you all the files, However to meet your expectation you could use FileFilter. and now the listing code would be :
File[] listOfFiles = folder.listFiles(myFileFilterObject);
in your file filter accept
API you define you logic to filter the files on the basis of date time range.
Or if you are good at Regex then use Apache commons RegexFileFilter