Chamal
Chamal

Reputation: 1127

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 = dateformat.parse(ti);  
             }

Upvotes: 0

Views: 177

Answers (4)

dogbane
dogbane

Reputation: 274670

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.

Upvotes: 1

Qwerky
Qwerky

Reputation: 18445

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.

Upvotes: 0

Brett McCann
Brett McCann

Reputation: 2519

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.

Upvotes: 0

jzd
jzd

Reputation: 23629

Loop through the array and count the number of matches.

Upvotes: 0

Related Questions