[英]android surprising difference between Date and GregorianCalendar - bug?
I have the following code: 我有以下代码:
public static final long ONE_DAY_IN_MILLISECONDS = 24*60*60*1000;
(...)
Date newNextTestDate = new Date ( (new Date().getTime()) + newInterval*ONE_DAY_IN_MILLISECONDS);
System.out.println("data: " +newNextTestDate.getTime());
GregorianCalendar tmp2Date = new GregorianCalendar ();
tmp2Date.add(GregorianCalendar.DAY_OF_MONTH, newInterval);
System.out.println(" data2: " +tmp2Date.getTimeInMillis());
//where newInterval is number of days (int)
In Android 4.0.4 and 4.2.2 in 2/3 of cases the displayed values are the same (sometimes minor difference). 在Android 4.0.4和4.2.2中,在2/3的情况下,显示的值相同(有时会略有差异)。 However in 1/3 of cases there is the difference of.... 3600000 (or something similar like 3600001) which is the equivalent of one hour.
但是,在1/3的情况下,存在.. 3600000(或类似3600001之类的东西)的差异,相当于一小时。 Moreover if I have this in a loop, it seems to be fully random, on the same device, without touching any system settings, sometimes that difference exist and sometimes not.
而且,如果我将其循环,似乎在同一设备上完全是随机的,没有涉及任何系统设置,有时会存在差异,有时则不会。 Is there any bug in Android implementation of Date?
Android的Date实现中是否存在任何错误?
I don't know what is the reason of problems, but I've found the workaround. 我不知道出现问题的原因是什么,但是我找到了解决方法。
When instead of using new Date()
I use (new GregorianCalendar()).getTime
this strange "effect" doesn't appear. 当我使用
(new GregorianCalendar()).getTime
而不是使用new Date()
,不会出现这种奇怪的“效果”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.