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



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

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

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

我想出了这个解决方案:

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

最新更新