为什么向 long 变量添加值会使其降低?
我试图将 30 天添加到 System.long a = System.currentTimeMillis() + ((long)30 * 24 * 60 * 60 * 1000);
但它正在减少该值?为什么?这是我尝试过的
int days = 30;
long a = System.currentTimeMillis();
long b = a + (days * 24 * 60 * 60 * 1000);
根据我运行代码的时间,结果是:
a = 1646737213282
, b = 1645034245986
为什么b
<一个?
I am trying to add 30 days to System.long a = System.currentTimeMillis() + ((long)30 * 24 * 60 * 60 * 1000);
but it is deceasing the value? Why? This is what i have tried
int days = 30;
long a = System.currentTimeMillis();
long b = a + (days * 24 * 60 * 60 * 1000);
According to the time I run the code, it was the result:
a = 1646737213282
, b = 1645034245986
Why b
< a
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是超出了 int 范围。最大 int 范围为
-2,147,483,648 (-231) 到 2,147,483,647 (231-1)
当您相乘时,它存储为
int
。但是,这不是限制。因此,解决方案是将其解析为 long。你可以看看我下面的代码:但是如果你想优化代码,这个代码会更好:
它节省了你的两行代码
The issue is that the int range is being exceeded. The max int range is from
-2,147,483,648 (-231) to 2,147,483,647 (231-1)
When you multiply, it is stored as a
int
. But, that's not the limit. So, the solution will be to parse it to a long. You can check out my code below:But if you want to optimise the code, this code will be better:
It saves your 2 lines of code ????
But, I'd also prefer you to use a method
and then