Java equivalent of microtime
java, math, time
Solution
`(mstime - (seconds * 1000))` is a `long`, which you divide by 1000, resulting in 0 (long).
To get floating point precision, either cast to `double`, or divide by a `double` value:
double decimal = (mstime - (seconds * 1000)) / 1000d; // note the d
By the way, if you need a higher precision for durations (time deltas), use `System.nanoTime()`.
`System.currentTimeMillis()` is not that accurate, and will (depending on the platform) not increase in unit increments (1ms) as one would expect. For example, on my system (Win7), the value of `System.currentTimeMillis()` changes every 15 to 16 ms.
Problem
I did some searching and it didn't really return anything. But I'm trying to replicate PHP's `microtime()` function in Java. I found some articles like this one that discusses how to achieve it in Javascript. Their logic is flawed though and the msec value of microtime would always return 0. This is what I have so far... ``` long mstime = System.currentTimeMillis(); long seconds = mstime / 1000; double decimal = (mstime - (seconds * 1000)) / 1000;` System.out.println(decimal + " " + seconds); ``` Except for no matter what, the decimal portion of the function (msec equivalent of php) will ALWAYS return 0 due to the formula. I'm stuck, confused, and need help. Fixed code: ``` long mstime = System.currentTimeMillis(); long seconds = mstime / 1000; double decimal = (mstime - (seconds * 1000)) / 1000d; return decimal + " " + seconds; ```