Okay, so, simply off the top of my head, without trying to perform anything kind of fancy, you could make use of the Java 8 date/time API, which provides the capability to calculate the different between two points in time.
So, taking your input, and running it through the code below, it outputs
2018-03-02T15:44:41.194
2018-03-02T15:44:41.198
0.004
Now, personally, I'd take the concept and simply create a DurationFormatter
which could take a Duration
and spit out your required format, but the idea here is to give you a jumping point to start from.
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
String startTime = "2018-03-02 15:44:41.194";
String endTime = "2018-03-02 15:44:41.198";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime startDateTime = LocalDateTime.parse(startTime, formatter);
LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter);
System.out.println(startDateTime);
System.out.println(endDateTime);
Duration duration = Duration.between(startDateTime, endDateTime);
long hours = duration.toHours();
duration = duration.minusHours(hours);
long mins = duration.toMinutes();
duration = duration.minusMinutes(mins);
long secs = duration.getSeconds();
duration = duration.minusSeconds(secs);
long millis = duration.toMillis();
StringBuilder sb = new StringBuilder(12);
if (hours > 0) {
sb.append(pad(hours, 2));
}
if (mins == 0 && sb.length() > 0) {
sb.append(":00");
} else if (mins > 0) {
if (hours > 0) {
sb.append(":");
}
sb.append(pad(mins, 2));
}
if (secs == 0 & sb.length() > 0) {
sb.append(":00");
} else if (secs > 0) {
if (mins > 0) {
sb.append(":");
}
sb.append(pad(secs, 2));
}
if (millis == 0 & sb.length() > 0) {
sb.append(".00");
} else if (millis > 0) {
if (secs > 0 || sb.length() > 0) {
sb.append(".");
} else if (sb.length() == 0) {
sb.append("0.");
}
sb.append(pad(millis, 3));
}
System.out.println(sb.toString());
}
public static String pad(long value, long length) {
return String.format("%0" + length + "d", value);
}
}
Now, if we change the input to something like...
String startTime = "2018-03-02 15:44:41.194";
String endTime = "2018-03-08 15:44:41.198";
It outputs
144:00:00.004
Or if we use
String startTime = "2018-03-02 15:44:41.194";
String endTime = "2018-03-08 15:15:41.198";
It outputs
143:31:00.004
Or
String startTime = "2018-03-02 15:44:41.194";
String endTime = "2018-03-08 15:15:50.198";
It outputs
143:31:09.004
Or
2018-03-02T15:44:41.194
2018-03-02T15:50:41.194
It outputs
06:00.00
... to me, this is where it gets weird, technically it's correct (6 mins), but from the format, it's hard to deduce exactly what it means
This is where I might be tempted to use something more like String.format("%02d:%02d:%02d.%04d", hours, mins, secs, millis)
which will output 00:06:00.0000
, but that all comes do to you needs. You will need to decide how best to take the raw information and present it based on your needs, but there are a couple of different ideas