简体   繁体   English

Java Calendar API输出错误

[英]Wrong output from Java Calendar API

Calendar.setTimeInMillis and Calendar.getTime works weird. Calendar.setTimeInMillisCalendar.getTime工作很奇怪。 It returns incorrect date results. 它返回不正确的日期结果。

Java Code Java代码

public static void main(String[] args)
{
    Calendar cal = Calendar.getInstance();
    final int oneDay = 24 * 60 * 60 * 1000;

    for(int i=0; i < 30; i++) {
        cal.setTimeInMillis(System.currentTimeMillis() - i * oneDay);
        System.out.println(cal.getTime());
    }

}

Output 输出量

Tue Jun 24 17:50:35 IST 2014
Mon Jun 23 17:50:35 IST 2014
Sun Jun 22 17:50:35 IST 2014
Sat Jun 21 17:50:35 IST 2014
Fri Jun 20 17:50:35 IST 2014
Thu Jun 19 17:50:35 IST 2014
Wed Jun 18 17:50:35 IST 2014
Tue Jun 17 17:50:35 IST 2014
Mon Jun 16 17:50:35 IST 2014
Sun Jun 15 17:50:35 IST 2014
Sat Jun 14 17:50:35 IST 2014
Fri Jun 13 17:50:35 IST 2014
Thu Jun 12 17:50:35 IST 2014
Wed Jun 11 17:50:35 IST 2014
Tue Jun 10 17:50:35 IST 2014
Mon Jun 09 17:50:35 IST 2014
Sun Jun 08 17:50:35 IST 2014
Sat Jun 07 17:50:35 IST 2014
Fri Jun 06 17:50:35 IST 2014
Thu Jun 05 17:50:35 IST 2014
Wed Jun 04 17:50:35 IST 2014
Tue Jun 03 17:50:35 IST 2014
Mon Jun 02 17:50:35 IST 2014
Sun Jun 01 17:50:35 IST 2014
Sat May 31 17:50:35 IST 2014
Sat Jul 19 10:53:23 IST 2014
Fri Jul 18 10:53:23 IST 2014
Thu Jul 17 10:53:23 IST 2014
Wed Jul 16 10:53:23 IST 2014
Tue Jul 15 10:53:23 IST 2014

After "May 31" , It would be "May 30 , May 29 , May 28 & May 27" but it returns incorrect date. 在“ 5月31日”之后,它将是“ 5月30日,5月29日,5月28日和5月27日”,但返回的日期不正确。

    Sat May 31 17:50:35 IST 2014
    Sat Jul 19 10:53:23 IST 2014
    Fri Jul 18 10:53:23 IST 2014
    Thu Jul 17 10:53:23 IST 2014
    Wed Jul 16 10:53:23 IST 2014
    Tue Jul 15 10:53:23 IST 2014

Is this bug in Calendar.getTime() ? Calendar.getTime()这个错误吗?

No, it is not a bug in Calendar.getTime() . 不,这不是Calendar.getTime()的错误。

You are using int , and when the numbers become too big to fit in an int , then i * oneDay overflows and you get strange results. 您正在使用int ,并且当数字变得太大而无法容纳int ,则i * oneDay溢出,您会得到奇怪的结果。

Solution: Use long instead: 解决方案:改用long代替:

final long oneDay = 24L * 60 * 60 * 1000;

(Your code also doesn't take daylight savings into account, at some point you'll see a shift of an hour if your timezone has daylight savings). (您的代码也没有考虑夏令时,如果您所在的时区具有夏令时,有时您会看到一个小时的偏移)。

You are performing integer arithmetic and have overflowed an int. 您正在执行整数算术,并且溢出了int。 The max value of a (signed) int is 2,147,483,647 (2^31-1). (有符号的)int的最大值是2,147,483,647(2 ^ 31-1)。 There are 86,400,000 milliseconds in ONE_DAY. ONE_DAY有86,400,000毫秒。 On day 25 you reach 2,160,000,000, which is a larger value than can be represented in an int and you overflow into negative numbers. 在第25天,您达到了2,160,000,000,这是一个比int表示的值还大的值,并且您溢出成负数。

You can solve this by doing your math with longs instead of ints. 您可以通过使用long而不是int进行数学运算来解决此问题。

public static void main(String[] args)
{
    Calendar cal = Calendar.getInstance();
    final long oneDay = 24 * 60 * 60 * 1000;

    for(int i=0; i < 30; i++) {
        cal.setTimeInMillis(System.currentTimeMillis() - i * oneDay);
        System.out.println(cal.getTime());
    }

}

Replace 更换

final int oneDay = 24 * 60 * 60 * 1000;

with

final long oneDay = 24 * 60 * 60 * 1000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM