Shifted Java Date已成为过去(Shifted Java Date is in the past)
java
我有一个问题,我不能理解 - 如果我添加超过24天,过去的转移日期。
long start = System.currentTimeMillis(); long shift = 3600000 * 24 * 24; System.out.println(new Date(start)); System.out.println(new Date(start + shift));24天班次:
start - Wed Apr 18 11:37:12 CEST 2012 end - Sat May 12 11:37:12 CEST 201225天班次:
start - Wed Apr 18 11:37:55 CEST 2012 end - Sat Mar 24 17:35:08 CET 2012有人可以告诉我为什么它会这样吗? 谢谢。
I have a problem I can't uderstand - if I add more than 24 days, the shifted date in the past.
long start = System.currentTimeMillis(); long shift = 3600000 * 24 * 24; System.out.println(new Date(start)); System.out.println(new Date(start + shift));For 24 day shift:
start - Wed Apr 18 11:37:12 CEST 2012 end - Sat May 12 11:37:12 CEST 2012For 25 day shift:
start - Wed Apr 18 11:37:55 CEST 2012 end - Sat Mar 24 17:35:08 CET 2012Could someone advise me why it behaves this way? Thank you.
最满意答案
你正在进行整数乘法,然后将结果转换为long。 乘法溢出,因此结果为负。
使用以下代码进行长乘法运算:
long shift = 3600000L * 24 * 24;You're doing Integer multiplication, and then transforming the result into a long. The multiplication overflows, so the result is negative.
Use the following code to do long multiplication:
long shift = 3600000L * 24 * 24;更多推荐
发布评论