Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the java utc time fit inside a double?

Tags:

java

utc

I have a method that expects a double, and I want to store the UTC time and my variable that I am passing to this method is a long.

I am using:

Date now = new Date();
now.getTime() 

to get the UTC time.

Can this value not fit into a double?

like image 650
Blankman Avatar asked Nov 25 '12 23:11

Blankman


1 Answers

It will "fit", in the sense that double can represent any long value, but there would be a loss of precision for dates far in the distant future (100,000's of years from now).

The IEEE 754 spec, which double uses, uses up to only 53 bits for the non-exponent part of the number. Since long is 64 bits, roughly speaking you'll potentially lose precision if the long value exceeds 53 bits.

However, 53 bits can accurately represent the current epoch millisecond time long value, which requires only 41 bits.

A loss of precision will not occur until the epoch time exceeds 253, which won't occur until Oct 12 287396.

Even at the worst case, 11 bits of "inaccuracy" would still translate to a time value with an accuracy of about ±1 second (2 ^ 11 = 2048, which in milliseconds is a range of 2 seconds).


In short, converting an epoch time to double is OK.

like image 159
Bohemian Avatar answered Oct 31 '22 15:10

Bohemian