-1

我正在做一个倒数计时器,我摆脱了int变量并制作了long。以下方法如何获得小时、分钟和秒。

public static long getHours(long totalSeconds) {
    return (long) Math.floor(totalSeconds / 3600000);
}

public static long getMinutes(long totalSeconds) {
    return (long) Math.floor((totalSeconds - 3600000 * getHours(totalSeconds)) / 60000);
}

public static long getSeconds(long totalSeconds) {
    return (long) Math.floor(totalSeconds - 3600000 * getHours(totalSeconds) - 60000 * getMinutes(totalSeconds));
}

public static long getMilliseconds(long totalSeconds) {
    return (long) Math.floor(getSeconds(totalSeconds) / 1000);
}

对于 ms,我将秒除以 1000。现在我以 12:34:56.000的格式获得时间。代替毫秒总是.000

怎么了?如何获得毫秒?

4

2 回答 2

1

“Milli”来自拉丁语。它的意思是“千分之一”。

也许这会提示您如何将第二个值转换为毫秒值。

于 2016-05-30T06:46:18.917 回答
0

希望对你有帮助:

public static int getHours(int totalSeconds) {
    return  TimeUnit.SECONDS.toMillis(your seconds);
}
于 2016-05-30T06:46:28.643 回答