0

我有一些由 xml 提要返回的运动时间结果。

第一次到达的结果时间被返回并转换如下:

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

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

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

鉴于与第一次的差距,我如何计算其他参与者的时间?

我尝试过使用 javaDate对象,但它也使用日历日,结果很奇怪:

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
4

1 回答 1

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
于 2013-10-28T11:20:55.467 回答