![](/img/trans.png)
[英]Difference between GregorianCalendar class and Calendar class in java?
[英]android surprising difference between Date and GregorianCalendar - bug?
我有以下代码:
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)
在Android 4.0.4和4.2.2中,在2/3的情况下,显示的值相同(有时会略有差异)。 但是,在1/3的情况下,存在.. 3600000(或类似3600001之类的东西)的差异,相当于一小时。 而且,如果我将其循环,似乎在同一设备上完全是随机的,没有涉及任何系统设置,有时会存在差异,有时则不会。 Android的Date实现中是否存在任何错误?
我不知道出现问题的原因是什么,但是我找到了解决方法。
当我使用(new GregorianCalendar()).getTime
而不是使用new Date()
,不会出现这种奇怪的“效果”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.