如何格式化毫秒

How to format Milliseconds

在我的代码中,我使用

获得毫秒数
// elaspsed time in milliseconds
    public long getElapsedTime() {
        if (running) {
            return System.currentTimeMillis() - startTime;
        }
        return stopTime - startTime;
    }

long 是一个整数因为我想得到 00.0000 格式的毫秒数 不过到目前为止我已经试过了

return ((System.currentTimeMillis() - startTime) / 1000f);

但我没有达到特定格式

您无法将 return 类型设置为 long 和 return doubleString 值。

但是,如果您需要将毫秒值格式化为提供的格式,您应该能够使用以下代码片段来完成它。

String.format("%d.%d",  TimeUnit.MILLISECONDS.toSeconds(millies),millies%1000);