tgkprog
tgkprog

Reputation: 4598

Java - math : System.nanoTime() and currentTimeMilli - if we take x chars out - what is the accuracy?

Java, date to long question. If we have :

        String rand = new Long(System.currentTimeMillis()).toString();
        logger.info("full :" + rand);
        rand = rand.substring(rand.length() -9);
        logger.info("Significant 8 :" + rand);

Then is there a way to know for how many days / minutes is the result unique? Sample output :

full :1382519851946

Significant 8 :19851946

I guess the last 3 digits are the milli seconds, the next two 51 here are seconds, then 98 198 is hours and minutes?

Put differently if I take 10 digits will I get unique values for a full day? Assuming of course only one thread and one JVM is generating these.

Upvotes: 0

Views: 379

Answers (4)

Alex Suo
Alex Suo

Reputation: 3119

Apart from your ignorance (not I am mean but you apparently didn't even read the documentation), I hereby have my 2 cents about accuracy:

JVM doesn't have a say on time accuracy. The nano time and millisecond accuracy depends on the OS. E.g. if you are under Windows or normal Linux without special libraries then all digits after the 1ms point would be 0. However, special 3rd party libs supports micro and (occasionally) nano seconds.

Given the quality of your question, I don't see any reason you need to go nano, if micro.

Upvotes: 0

phlogratos
phlogratos

Reputation: 13924

Assuming all 10 digits on the right are 9, then the remaining value changes the next millisecond.

Upvotes: 0

bjedrzejewski
bjedrzejewski

Reputation: 2436

Miliseconds are 1/1000 of second. Form there it is quite easy:

You can take 3 digits and get the same value for a whole second (1000 milis)

You can take 4 digits and get the same value for a whole minute (60000 milis > 10000)

You can take 6 digits and get the same value for a whole hour (3600000 milis > 1000000)

You can take 7 digits and get the same value for a whole day (86400000 milis > 10000000)

If you need more the formula is quite easy from there. I assume that this is what you meant. If you take digits from the front, you will get different value every mili. If you want to create range of unique values you can reverse what I said here:

To have the unique values for a second leave 3 digts. (1000 milis)

To have unique values for a minute leave 5 digits. (60000 milis < 100000)

To have unique values for an hour leave 7 digits. (3600000 milis < 10000000)

To have unique values for a day leave 8 digits. (86400000 milis < 100000000)

The problem is not how many digits you take from the front (as the length varies), but how many digits you leave.

Upvotes: 2

Cl&#233;ment
Cl&#233;ment

Reputation: 12897

currentTimeMillis is actually the number of milliseconds since January 1, 1970 (source).

You can get seconds, minutes, and hours using modulus operations (milliseconds % 1000 = seconds and so on), but the results won't be fully accurate due to leap seconds. See the discussion in http://docs.oracle.com/javase/6/docs/api/java/util/Date.html for more details.

Upvotes: 2

Related Questions