Pregunta

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?

¿Fue útil?

Solución

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

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

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top