我有一段代碼在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
我絕對不知道這是爲什麼。有沒有人遇到類似的問題?
謝謝!
安德烈亞斯