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 に答える