Question

Is it possible to lose precision in this case?

int X =  (some integer number);
Date D = new Date(X * 1000);
int Y  = (int)(D.getTime())/1000;

I would believe that Y would always be the same as X as long as X is some integer value. Is that correct?

Was it helpful?

Solution

No!you're not correct! replace the line Date D = new Date(X * 1000);

with Date D = new Date(X * 1000L);

OTHER TIPS

Use long (64 bit) numbers not integers (32 bit) numbers. If you use integers it may overflow (go from positive to negative). See this link for details https://www.securecoding.cert.org/confluence/display/java/NUM00-J.+Detect+or+prevent+integer+overflow

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top