我做了一些搜索,并没有真正返回任何东西.
但我试图microtime()在Java中复制PHP的功能.我找到了一些像这样的文章,讨论如何在Javascript中实现它.他们的逻辑是有缺陷的,并且microtime的msec值总是会返回0.这就是我到目前为止所拥有的......
long mstime = System.currentTimeMillis();
long seconds = mstime / 1000;
double decimal = (mstime - (seconds * 1000)) / 1000;`
System.out.println(decimal + " " + seconds);
Run Code Online (Sandbox Code Playgroud)
除了无论如何,由于公式,函数的小数部分(msec相当于php)将始终返回0.我陷入困境,困惑,需要帮助.
固定代码:
long mstime = System.currentTimeMillis();
long seconds = mstime / 1000;
double decimal = (mstime - (seconds * 1000)) / 1000d;
return decimal + " " + seconds;
Run Code Online (Sandbox Code Playgroud)
(mstime - (seconds * 1000))是a long,除以1000,得到0(长).
要获得浮点精度,要么强制转换为double或除以double值:
double decimal = (mstime - (seconds * 1000)) / 1000d; // note the d
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果你需要更高的持续时间精度(时间增量),请使用System.nanoTime().
System.currentTimeMillis()不是那么准确,并且(取决于平台)不会像人们预期的那样以单位增量(1ms)增加.例如,在我的系统(Win7)上,System.currentTimeMillis()每15到16毫秒更改一次值.