关于 GregorianCalendar:java 中相同的输入但不同的输出
我想在程序中使用毫秒设置新日期,但它不起作用。有人可以告诉我为什么它不起作用吗?
日历 r_1 = new GregorianCalendar(2011,0,1);
r_1.add(日历.DAY_OF_MONTH,2);
System.out.println(r_1.getTime());
长date_1 = r_1.getTimeInMillis() + 2*24*60*60*1000;
r_1.setTimeInMillis(startTime1);
System.out.println(r_1.getTime());
它工作得非常正确,但如果我将日期从 2 更改为 25,那么它就工作了。
----------输出正确,是2011/01/26 ----------
Calendar r_1 = new GregorianCalendar(2011,0,1);
r_1.add(日历.DAY_OF_MONTH,25);
System.out.println(r_1.getTime());
-----------现在输出不正确,是2010/12/07-----
long date_1 = r_1.getTimeInMillis() + 25*24*60*60*1000;/ /i 将 2 更改为 25
r_1.setTimeInMillis(startTime1);
System.out.println(r_1.getTime());
谢谢
I want to use milliseconds to set a new date in my program,but it doesnt work. Is there anybody can tell me why it doesnt work?
Calendar r_1 = new GregorianCalendar(2011,0,1);
r_1.add(Calendar.DAY_OF_MONTH,2);
System.out.println(r_1.getTime());
long date_1 = r_1.getTimeInMillis() + 2*24*60*60*1000;
r_1.setTimeInMillis(startTime1);
System.out.println(r_1.getTime());
It works both very correct , but if i change the day from 2 to 25,then it doenst work .
----------the output is correct ,it is 2011/01/26 ----------
Calendar r_1 = new GregorianCalendar(2011,0,1);
r_1.add(Calendar.DAY_OF_MONTH,25);
System.out.println(r_1.getTime());
-----------the output is incorrect now ,it is 2010/12/07------
long date_1 = r_1.getTimeInMillis() + 25*24*60*60*1000;//i have change 2 to 25
r_1.setTimeInMillis(startTime1);
System.out.println(r_1.getTime());
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
表达式
25*24*60*60*1000
是一个整数,并且您已经溢出了整数的大小,从而创建了一个负数。你的表达式是2,160,000,000毫秒。 int 可以容纳的最大值是 2,147,483,647。
要解决此问题,您必须强制表达式为 long,如下所示
The expression
25*24*60*60*1000
is an integer, and you have overflowed the size of an integer, creating a negative number.Your expression is 2,160,000,000 milliseconds. The largest value an int can hold is 2,147,483,647.
To fix this, you have to force the expression to be a long, as follows
25*24*60*60*1000
太大,无法放入int
中。尝试
25L*24*60*60*1000
这是一个长常量。25*24*60*60*1000
is too large to fit in anint
.Try
25L*24*60*60*1000
which is a long constant.尝试这样的事情:
Try something like that: