![](/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.