Android:奇怪的GregorianCalendar行为

时间:2010-09-08 09:54:43

标签: android

我遇到了一段代码,这些代码在99%的安装中运行良好,但在一些HTC Desire手机上导致无限循环。

我要做的是创建一个可用数据的月份列表。我从数据可用的第一个日期开始,然后使用循环直到我到达现在。

这是代码(简化):

    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

我完全不知道为什么会这样。还有其他人遇到过类似的问题吗?

谢谢!

安德烈亚斯

2 个答案:

答案 0 :(得分:0)

Calendar cal = Calendar.getInstance(); Date currentLocalTime = cal.getTime(); DateFormat date = new SimpleDateFormat(“dd-MM-yyy HH:mm:ss z”); String localTime = date.format(currentLocalTime); 的System.out.println(LOCALTIME);

答案 1 :(得分:0)

我知道这是一个老问题,但在我的HTC手机上出现了类似的情况:当明确地将日历日期设置为月份的第一天时,它实际上产生了更早的日期。例如,如果我这样做:date.set(2011,Calendar.MARCH,1,0,0),那么结果日期将是2月28日。我发现它与夏令时有关,并且似乎只影响 HTC ,因为它从未向其他品牌报告,也正确地在仿真器上运行。由于我的应用程序在很大程度上取决于日历,我最终得到了相当难看的解决方法:在设置日期之后,我检查结果日期是否与预期相同,然后根据需要通过添加或减去小时来调整它。