calculate no of records in selected time period using java
i hav below time records Stored in a array.
8.10.22 AM
8.20.35 AM
8.56.46 AM
8.44.39 AM
So i want how many records in 8.00 AM to 9.00 AM time period. how can i do that using java? Here is my code...
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:/sample.csv")
while (reader.ready()) {
String line = reader.readLine();
String[] values = line.split(",");
String ti=values[5];
DateFormat dateformat = new SimpleDateFormat("hh:mm:ss");
Date date = datefo开发者_如何学Pythonrmat.parse(ti);
}
You can use the before and after Date API methods to test if a Date
is within a certain time range. If it is, increment a counter.
Loop through the array and count the number of matches.
Are they ordered by time? If not I would use a structure where they can be ordered, if possible. Once you find a time that exceeds the window you are searching on, you could halt your search. Depending on the size of the structure, this could save a lot of time.
Looping through the array might not be the best way. If the dates are ordered, you might want to do a binary search to get the position of the first date before the start and after the end and calculate the number of dates in between them.
精彩评论