给定第一个计时时间,根据间隙计算下一个结果的值

时间:2013-10-28 11:20:55

标签: java time

我有一些xml Feed返回的运动时间结果。

返回第一个到达的结果时间,并按如下方式转换:

String time = "00:01:00:440";
String gap = "";

对于其他参与者我只回到差距:

String time = "";
String gap = "00:00:00:900";

如何计算出与第一个产生差距的其他参与者的时间?

我尝试过使用java Date对象,但它也使用了日历天,我得到了奇怪的结果:

String firstTime = "00:01:00:440";
String gapOne = "00:00:00:900";

SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss:SSS");

Date d1 = null;
Date d2 = null;
long diff = 0;
String timeResult = "";

try {

    d1 = formatter.parse(firstTime);
    d2 = formatter.parse(gapOne);
    diff = d2.getTime() + d1.getTime();
    timeResult = formatter.format(new Date(diff));

} catch (Exception e) {
    e.printStackTrace();
}

System.out.println(timeResult);

但打印出来:

11:01:01:340

1 个答案:

答案 0 :(得分:0)

我提出了这个解决方案:

String firstTime = "00:01:00:440";
String gapOne = "00:00:00:900";

String firstTimeSplit[] = firstTime.split(":");
String gapSplit[] = gapOne.split(":");

int millisecSum = Integer.parseInt(firstTimeSplit[3]) + Integer.parseInt(gapSplit[3]);
int secsSum = Integer.parseInt(firstTimeSplit[2]) + Integer.parseInt(gapSplit[2]);
int minSum = Integer.parseInt(firstTimeSplit[1]) + Integer.parseInt(gapSplit[1]);
int hrsSum = Integer.parseInt(firstTimeSplit[0]) + Integer.parseInt(gapSplit[0]);

String millisec = String.format("%03d", millisecSum % 1000);

int mathSec = millisecSum / 1000 + secsSum;
String secs = String.format("%02d", mathSec % 60);

int mathMins = mathSec / 60 + minSum;
String mins = String.format("%02d", mathMins % 60);

int mathHrs = mathMins / 60 + hrsSum;
String hrs = String.format("%02d", mathHrs % 60);

String format = "%s:%s:%s:%s";
String result = String.format(format, hrs, mins, secs, millisec);

这样我就会以这种方式返回值:

00:01:01:340