public static void main(String args[]) throws ParseException { /*Random random = new Random(); System.out.println("{"); for( int i = 0 ; i<300 ; i++ ) { System.out.println("{"+i + ","+ (i+Math.abs(random.nextInt())%20+1)+"},"); } System.out.println("}"); */ /* for( int i=0 ; i<300 ; ++i ) { for( int[] row : ranges ) { if( row[0]<=i && row[1]-1>i ) System.out.print("#"); else System.out.print(" "); } System.out.println(); }*/ Request requests[] = new Request[ranges.length]; GregorianCalendar start = new GregorianCalendar(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); start.setTime(sdf.parse("2000-01-01")); for (int i = 0; i < 300; ++i) { int row[] = ranges[i]; Request req = new Request(); req.setRecievedAt(start.getTimeInMillis() + ((long) row[0]) * 1000 * 60 * 60 * 24); req.setFinishedAt(start.getTimeInMillis() + ((long) row[1]) * 1000 * 60 * 60 * 24); requests[i] = req; } Object[] res = weekly( requests, start.getTimeInMillis(), start.getTimeInMillis() + (long) 300 * 1000 * 60 * 60 * 24); for (Object c : res) { Object[] row = (Object[]) c; for (Object o : row) { System.out.print(o + ", "); } System.out.println(); } }
public static Object[] weekly(Request[] requests, long start, long end) { GregorianCalendar dayStart = new GregorianCalendar(); dayStart.setTimeInMillis(start); dayStart.set(Calendar.HOUR_OF_DAY, 0); dayStart.set(Calendar.MINUTE, 0); dayStart.set(Calendar.SECOND, 0); dayStart.set(Calendar.MILLISECOND, 0); GregorianCalendar dayEnd = new GregorianCalendar(); dayEnd.setTimeInMillis(dayStart.getTimeInMillis() + 1000 * 60 * 60 * 24); Object[] results = new Object[(int) ((end - start) / ((long) 1000 * 60 * 60 * 24))]; for (int i = 0; dayStart.getTimeInMillis() < end; ++i) { long cur = 0; for (Request request : requests) { // seek open requests if (request.getRecievedAt() <= dayStart.getTimeInMillis() && request.getFinishedAt() > dayEnd.getTimeInMillis()) { cur++; } } Object row[] = new Object[3]; row[0] = new Long(dayStart.getTimeInMillis()); row[1] = new Long(dayEnd.getTimeInMillis()); row[2] = new Double(cur); results[i] = row; dayStart.setTimeInMillis(dayStart.getTimeInMillis() + 1000 * 60 * 60 * 24); dayEnd.setTimeInMillis(dayStart.getTimeInMillis() + 1000 * 60 * 60 * 24); } return results; }