我有两个dateTime字段,我需要获得已经过去的天数和小时数。这是我能做的最好的,但现在我遇到了一些错误。
继承错误
System.TypeException:无效的小数:0。-22
如果第一个日期是22/09/2016 12:30,第二个日期是24/09/2016 14:00我希望结果是2.1
public static Decimal getTimeDifference(Datetime now, Datetime endTime){
if(now != null && endTime != null){
Long dt1Long = endTime.getTime();
Long dt2Long = now.getTime();
Long milliseconds = dt2Long - dt1Long;
Long seconds = milliseconds / 1000;
Long minutes = seconds / 60;
Long hours = minutes / 60;
Long days1 = hours / 24;
Long dt1Long2 = endTime.addDays((Integer)days1).getTime();
Long dt2Long2 = now.getTime();
Long milliseconds2 = dt2Long2 - dt1Long2;
Long seconds2 = milliseconds2 / 1000;
Long minutes2 = seconds2 / 60;
Long hours2 = minutes2 / 60;
Long days2 = hours2 / 24;
return Decimal.valueOf(days1 + '.' + hours2);
} else {
return null;
}
}