-3

我想得到两次之间的差异。我使用以下方法计算此值。

使用这个

但我想计算三个差异。例如,我有 6 次如下。

时间1,时间2,时间3,时间4

我想计算两次之间的差异

long difference1=time2-time1;
long difference2=time4-time3;
long difference3=time6-time5;

然后我想添加这三个差异。比如差1+差2+差3。

4

3 回答 3

2
long difference1 = date2.getTime()-date1.getTime();  
long difference2 = date4.getTime()-date3.getTime();  
long difference3 = date6.getTime()-date5.getTime();

ling totalDifference = difference1 + difference2 + difference3;  

此 totalDifference 以毫秒为单位,您可以将其转换为 Day:Hour:Min:Seconds

days = (int) (totalDifference / (1000 * 60 * 60 * 24));  
hours = (int) ((totalDifference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));  
min = (int) (totalDifference - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))/ (1000 * 60);
于 2012-06-26T08:13:30.253 回答
0

最好使用 Calendar 类和 GregorianClaendar 而不是 Date 类。

Date time1, time2, time3, time4;
[...]
GregorianCalendar difference12 = new GregorianCalendar(0,0,0,0,0);
difference12.set(GregorianCalendar.MILLISECNOD, time1.getTime() - time2.getTime());

GregorianCalendar difference34 = new GregorianCalendar(0,0,0,0,0);
difference12.add(GregorianCalendar.MILLISECNOD, time3.getTime() - time4.getTime());

Date diff12 = difference12.getGregorianChange();
Date diff34 = difference34.getGregorianChange();
GregorianCalendar sum = new GregorianCalendar(0,0,0,0,0);
sum.add(GregorianCalendar.MILLISECOND, diff12.getTime() - time34.getTime());
于 2012-06-26T08:35:44.690 回答
0

这是你想要的吗?

long totalTime = (date2.getTime()-date1.getTime() + date4.getTime()-date3.getTime() + date6.getTime()-date5.getTime());

Date totalDifDate = new Date(totalTimeMillis);
于 2012-06-26T07:52:51.490 回答