new Date(new Date().getTime()-25 * 24 * 60 * 60 * 1000)获得了意外日期 [英] new Date(new Date().getTime()-25 * 24 * 60 * 60 * 1000) got unexpected date
问题描述
我想生成日期列表,但发现日期从-25 * 24 * 60 * 60 * 1000开始错误
I would like to generate a list of dates, but found out the date is wrong start from - 25 * 24 * 60 * 60 * 1000
我的本地日期是2016年7月17日.我
My local date is 2016-07-17. I got
2016-07-17
2016-07-16
2016-07-15
...
2016-06-23
2016-08-11
我不知道 2016-08-11
的来源.我将25分解为24和-1,如下所示( xxx
和 yyy
),然后我得到了正确的日期 2016-06-22
.
I have no idea where the 2016-08-11
comes from. I broke down the 25 to 24 and -1 as below (xxx
and yyy
), and then I got the correct date 2016-06-22
.
为什么 xxx
可以工作,但是 yyy
不能?
Why does xxx
work but yyy
Doesn't?
Date xxx = new Date(new Date().getTime()-24 * 24 * 60 * 60 * 1000 -1 * 24 * 60 * 60 * 1000);
Date yyy = new Date(new Date().getTime()-25 * 24 * 60 * 60 * 1000);
这是我的代码:从i = 25起日期是错误的
Here is my code: date is wrong from i=25
for (int i=0; i<240;i++) {
Date dt = new Date(new Date().getTime() - i * 24 * 60 * 60 * 1000);
Log.e(TAG,Global.sdfDateTime19.format(dt));
}
推荐答案
您正在溢出 int
的范围. i * 24 * 60 * 60 * 1000
产生一个 int
(然后从 getDate
的 long
中减去它)).
You're overflowing the range of int
. i * 24 * 60 * 60 * 1000
yields an int
(which is then subtracted from the long
from getDate
).
当 i
为24时没关系,因为结果为2,073,600,000,小于 int
的最大正值2,147,483,647.但是当达到 i = 25
时,就会回绕(如果不限制为 int
,则该值为21.6亿,这太大了.)
It's okay when i
is 24, because the result is 2,073,600,000, which is less than the maximum positive value for an int
, 2,147,483,647. But when you reach i = 25
, you wrap around (the value if you weren't constrained to int
would be 2,160,000,000, which is too big).
只要将 i
声明为long或将24设置为 long
,即可将其设置为 long
乘以:
Just make it a long
multiplication, either by declaring i
as a long or by making the 24 a long
:
Date dt = new Date(new Date().getTime() - i * 24L * 60 * 60 * 1000);
// ---------------------------------------------^
为什么xxx有效,但yyy不起作用?
Why xxx works but yyy not work?
因为您已将乘法分解为两部分(为了清楚起见,我在其中添加了一个空格,因为 -1
否则看起来像是负数而不是减法运算符,后跟 1
):
Because you've broken up the multiplication into two parts (I've added a space in there for clarity, since the -1
otherwise looks like a negative number rather than the subtraction operator followed by 1
):
Date xxx = new Date(new Date().getTime()-24 * 24 * 60 * 60 * 1000 - 1 * 24 * 60 * 60 * 1000);
// The first part -----------------------^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
// The second part ------------------------------------------------/
...,并且您将其拆分成两个部分的任何一个都不会溢出 int
的范围.
... and neither of the two parts you've broken it up into overflows the range of int
.
通常,我不建议您以这种方式操作日期,尤其是因为并非所有的日期都恰好有24小时(考虑进入和退出夏令时的日期).我将使用一个库来让您在工作日"级别上工作,例如Java 8 java.time
或JodaTime或类似的库.
In general, I wouldn't suggest manipulating dates this way, not least because not all days have exactly 24 hours in them (consider the days going into and out of daylight saving time). I'd use a library that lets you work at the "day" level, such as the Java 8 java.time
stuff, or JodaTime, or similar.
这篇关于new Date(new Date().getTime()-25 * 24 * 60 * 60 * 1000)获得了意外日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!