1 : |
dgarcia |
1 |
package eu.smartlm.abs.portal.data.aggregation;
|
2 : |
|
|
|
3 : |
|
|
import java.util.Calendar;
|
4 : |
|
|
import java.util.Date;
|
5 : |
|
|
|
6 : |
|
|
/**
|
7 : |
|
|
* Performs different time calculations with the dates of the Usage Records
|
8 : |
|
|
* @author David García Pérez - CESGA
|
9 : |
|
|
*/
|
10 : |
|
|
public class TimeCalculation {
|
11 : |
|
|
|
12 : |
|
|
/**
|
13 : |
|
|
* This class performs a time difference between two formated dates, if the dates are wrongly formated, it returns -1
|
14 : |
|
|
* @param formatedDate1 On of the dates to be used in the calculation
|
15 : |
|
|
* @param formatedDate2 The other date to be used in the calculation
|
16 : |
|
|
* @return returns the difference in miliseconds between formatedDate1 and formatedDate2, if they are wrongly formated it returns -1
|
17 : |
|
|
*/
|
18 : |
|
|
public static long timeDifferenceInMiliSeconds(Calendar calendar1, Calendar calendar2) {
|
19 : |
|
|
|
20 : |
|
|
Date date1 = calendar1.getTime();
|
21 : |
|
|
Date date2 = calendar2.getTime();
|
22 : |
|
|
|
23 : |
|
|
//Calculating the difference
|
24 : |
|
|
if(date1.after(date2))
|
25 : |
|
|
return date1.getTime() - date2.getTime();
|
26 : |
|
|
else
|
27 : |
|
|
return date2.getTime() - date1.getTime();
|
28 : |
|
|
}
|
29 : |
|
|
|
30 : |
|
|
/**
|
31 : |
|
|
* This method performs the time difference between two UsageRecord dates in hours
|
32 : |
|
|
* @param formatedDate1 formated usage record date to use in the calculation
|
33 : |
|
|
* @param formatedDate2 formated usage record date to use in the calculation
|
34 : |
|
|
* @return the difference in hours between formatedDate1 and formatedDate2, -1 if one or both dates are wrongly formatted
|
35 : |
|
|
*/
|
36 : |
|
|
public static double timeDifferenceInHours(Calendar date1, Calendar date2) {
|
37 : |
|
|
|
38 : |
|
|
long differenceInMiliSeconds = timeDifferenceInMiliSeconds(date1, date2);
|
39 : |
|
|
|
40 : |
|
|
if(differenceInMiliSeconds != -1)
|
41 : |
|
|
return ((double) differenceInMiliSeconds) / (1000.0 * 60.0 * 60.0);
|
42 : |
|
|
else
|
43 : |
|
|
return -1.0;
|
44 : |
|
|
}
|
45 : |
|
|
|
46 : |
|
|
}
|