Revision 1 -
(download)
(annotate)
Wed Jul 28 10:05:51 2010 UTC
(13 years, 9 months ago)
by dgarcia File size: 1657 byte(s)
First code commit
package eu.smartlm.abs.portal.data.aggregation;
import java.util.Calendar;
import java.util.Date;
/**
* Performs different time calculations with the dates of the Usage Records
* @author David García Pérez - CESGA
*/
public class TimeCalculation {
/**
* This class performs a time difference between two formated dates, if the dates are wrongly formated, it returns -1
* @param formatedDate1 On of the dates to be used in the calculation
* @param formatedDate2 The other date to be used in the calculation
* @return returns the difference in miliseconds between formatedDate1 and formatedDate2, if they are wrongly formated it returns -1
*/
public static long timeDifferenceInMiliSeconds(Calendar calendar1, Calendar calendar2) {
Date date1 = calendar1.getTime();
Date date2 = calendar2.getTime();
//Calculating the difference
if(date1.after(date2))
return date1.getTime() - date2.getTime();
else
return date2.getTime() - date1.getTime();
}
/**
* This method performs the time difference between two UsageRecord dates in hours
* @param formatedDate1 formated usage record date to use in the calculation
* @param formatedDate2 formated usage record date to use in the calculation
* @return the difference in hours between formatedDate1 and formatedDate2, -1 if one or both dates are wrongly formatted
*/
public static double timeDifferenceInHours(Calendar date1, Calendar date2) {
long differenceInMiliSeconds = timeDifferenceInMiliSeconds(date1, date2);
if(differenceInMiliSeconds != -1)
return ((double) differenceInMiliSeconds) / (1000.0 * 60.0 * 60.0);
else
return -1.0;
}
}