Here is a suggestion, using TimeUnit
, to obtain each time part and format them.
private static String formatDuration(long duration) {
long hours = TimeUnit.MILLISECONDS.toHours(duration);
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) % 60;
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) % 60;
long milliseconds = duration % 1000;
return String.format("%02d:%02d:%02d,%03d", hours, minutes, seconds, milliseconds);
}
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss,SSS");
Date startTime = sdf.parse("01:00:22,427");
Date now = sdf.parse("02:06:38,355");
long duration = now.getTime() - startTime.getTime();
System.out.println(formatDuration(duration));
The result is: 01:06:15,928