This answer is based on: https://stackoverflow.com/a/4927884/5622596
Imports needed:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
Code that prints out the time : 00:08.194
String first = "1:12.203";
String second = "1:04.009";
SimpleDateFormat formater = new SimpleDateFormat("mm:ss.SSS");
try {
Date dateStart = formater.parse(first);
Date dateEnd = formater.parse(second);
long milliSeconds = dateStart.getTime() - dateEnd.getTime();
Date timeDiff = new Date(milliSeconds);
System.out.println(formater.format(timeDiff));
} catch (ParseException e) {
e.printStackTrace();
}
If you are looking for a method to return a string.Try something like this:
@Test
public void printDiff() {
String first = "1:12.203";
String second = "1:04.009";
try {
System.out.println(getTimeDiffString(first, second));
} catch (ParseException e) {
e.printStackTrace();
}
}
private String getTimeDiffString(String first, String second) throws ParseException {
SimpleDateFormat formater = new SimpleDateFormat("mm:ss.SSS");
//Get number of milliseconds between times:
Date dateStart = formater.parse(first);
Date dateEnd = formater.parse(second);
long milliSeconds = dateStart.getTime() - dateEnd.getTime();
//Convert time difference to mm:ss.SSS string
Date timeDiff = new Date(milliSeconds);
return formater.format(timeDiff).toString();
}