2010-09-08 99 views
0

我有一段代碼在99%的安裝上運行良好,但導致HTC Desire手機上出現無限循環。Android:奇怪的GregorianCalendar行爲

我想要做的是創建一個數據可用的月份列表。我從數據可用的第一個日期開始,然後使用循環,直到我到達現在。

下面是代碼(簡化):

GregorianCalendar today = new GregorianCalendar(); 
    GregorianCalendar xCal = new GregorianCalendar(); 
    xCal.setTime(startDate); 
    xCal.set(GregorianCalendar.DAY_OF_MONTH, resetDay); // resetDay = end of billing period 
    while(xCal.before(today)) 
    { 
     MonthObject xObj = new MonthObject(); // Create object to hold information 
     xObj.setTimestamp(xCal.getTimeInMillis()); // Save timestamp for start of period 
     xObj.setFromDate(xCal.getTime()); // Save date for start of period 
     xCal.add(GregorianCalendar.MONTH, 1); // Increment by 1 month 
     xCal.set(GregorianCalendar.DAY_OF_MONTH, resetDay); // set to end of billing period 
     xCal.set(Calendar.HOUR_OF_DAY, 0); // make sure it's midnight 
     xCal.set(Calendar.MINUTE, 0); 
     xCal.set(Calendar.SECOND, 0); 
     xCal.set(Calendar.MILLISECOND, 0); 
     xObj.setToDate(xCal.getTime()); // Save timestamp for end of period 
     xObj.setToTimestamp(xCal.getTimeInMillis()); // Save date for end of period 
    Log.v("NetworkCheck", "Added MonthObject - " + xObj.fromDate.toGMTString() + " to " + xObj.toDate.toGMTString()); 
     dates.add(xObj); // Add to collection 
     xObj = null; 
    } 
    xCal = null; 
    today = null; 

因此,在手機的99%,這給期望的結果(個月的列表)。然而,在一些手機上,我得到這個輸出:

D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 
D/NetworkCheck(2311): Added MonthObject - 30 Aug 2010 22:00:00 GMT to 30 Aug 2010 22:00:00 GMT 

我絕對不知道這是爲什麼。有沒有人遇到類似的問題?

謝謝!

安德烈亞斯

回答

0

日曆CAL = Calendar.getInstance(); 日期currentLocalTime = cal.getTime(); DateFormat date = new SimpleDateFormat(「dd-MM-yyy HH:mm:ss z」); String localTime = date.format(currentLocalTime); System.out.println(localTime);

0

我知道這是一個老問題,但類似的情況發生在我的HTC手機上:當明確設置日曆日期到月的第一天時,它實際上會產生更早的日期。例如,如果我這樣做:date.set(2011,Calendar.MARCH,1,0,0),那麼結果日期將是2月28日。我發現它連接到夏令時,似乎影響宏達電只有,因爲它從來沒有從其他品牌的報道,也正確地在仿真器上工作。由於我的應用在很大程度上取決於日曆,我最終採用了相當醜陋的解決方法:在設置日期之後,我檢查結果日期是否與預期相同,然後根據需要添加或減去小時數進行調整。