Shifted Java Date已成为过去(Shifted Java Date is in the past)

编程入门 行业动态 更新时间:2024-10-27 07:17:24
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 2012

25天班次:

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 2012

For 25 day shift:

start - Wed Apr 18 11:37:55 CEST 2012 end - Sat Mar 24 17:35:08 CET 2012

Could 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;

更多推荐

本文发布于:2023-04-27 12:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:已成为   Shifted   Java   Date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!