문제

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?

도움이 되었습니까?

해결책

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

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top