Commit c72029ae authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[enh] Add DateUtils.elapsedTime() , useful for log

parent c8283dcd
Pipeline #15815 failed with stage
in 32 seconds
......@@ -85,4 +85,24 @@ public class DateUtils {
return nextDayAndHour(dayOfTheWeek, hour).getTime() - System.currentTimeMillis();
}
public static String elapsedTime(long timeInMs) {
long elapsedTime = System.currentTimeMillis() - timeInMs;
StringBuilder sb = new StringBuilder();
sb.append("in ");
if (elapsedTime < 1000) {
return sb.append(elapsedTime).append("ms").toString();
}
double seconds = (double) elapsedTime / 1_000;
if (seconds < 60) {
return sb.append(seconds).append("s").toString();
}
int minutesFloor = (int) Math.floor(seconds / 60);
int secondsFloor = (int) Math.floor(seconds - minutesFloor * 60);
int millis = (int) Math.floor((seconds - secondsFloor - minutesFloor * 60) * 1_000);
return sb.append(minutesFloor).append("min ")
.append(secondsFloor).append("s ")
.append(millis).append("ms")
.toString();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment