com.github.born2snipe.cli.ElapsedTimeFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cli-progress Show documentation
Show all versions of cli-progress Show documentation
This is a simple Java API for printing progress for CLI apps
The newest version!
/**
*
* Copyright to the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.github.born2snipe.cli;
public class ElapsedTimeFormatter {
public static String formatMillis(long val) {
StringBuilder buf = new StringBuilder(20);
String sgn = "";
if (val < 0) {
sgn = "-";
val = Math.abs(val);
}
append(buf, sgn, 0, (val / 3600000));
append(buf, ":", 2, ((val % 3600000) / 60000));
append(buf, ":", 2, ((val % 60000) / 1000));
append(buf, ".", 3, (val % 1000));
return buf.toString();
}
private static void append(StringBuilder tgt, String pfx, int dgt, long val) {
tgt.append(pfx);
if (dgt > 1) {
int pad = (dgt - 1);
for (long xa = val; xa > 9 && pad > 0; xa /= 10) {
pad--;
}
for (int xa = 0; xa < pad; xa++) {
tgt.append('0');
}
}
tgt.append(val);
}
}