I am building a CalendarDisplay application for Android. Among other things, this application allows the user to select the number of days to add to an epoch (epoch is a date in 1899), and that result will be represented graphically on the display. However, when I set Date value from a Calendar value, I get unexpected results: the year seems to be off by a value of 1900. While I could simply add 1900 to the result, that seems like the wrong answer since what I am doing is (I believe) by the book. Or I could continue with what I have; that works too. But that shares the same by-the-book issue. Here is the code, with spare variables for debugging:

public void gotoCal(int days) {
    // epochCal has been initialized as follows:
    // (HYF_BASE_YEAR = 1899,  HYF_BASE_MONTH = 11, HYF_BASE_DAY = 1)
    // epochCal = (Calendar) Calendar.getInstance();
    // epochCal.set(HYF_BASE_YEAR, HYF_BASE_MONTH, HYF_BASE_DAY);

    Calendar cal = (Calendar) epochCal.clone();
    cal.add(Calendar.DATE, days);
    int year = cal.get(Calendar.YEAR);
    Date date = cal.getTime();
    int year2 = date.getYear();
    if (year2 != year) {
        date.setYear(year);
    }
    int year3 = date.getYear();
    gotoDate(date);
}

Now, when I run this code in debug mode as gotoCal(40000), with a breakpoint at the gotoDate() line, I see the following values in the fields: year = 2009 , year2 = 109 , year3 = 2009

Is the Calendar.getTime() function supposed to return a date that's 1900 years in the past (I have seen no such documentation)? Or might I be doing something to influence it to do that? Or what else might I be doing wrong?

有帮助吗?

解决方案

You've misread (or not read) the documentation for Date.getYear:

Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

Also:

  • Date.getYear is deprecated, and for good reason. It uses the default time zone, and basically shouldn't be used.

  • Joda Time is much better date and time API. I'd thoroughly recommend using that instead of Date/Calendar.

其他提示

I did the following to fix this issue on android emulator.

private Date insuranceExpiryDate = new Date(Calendar.getInstance().getTime().getTime());
private int mYear, mMonth, mDay ;

In my onCreate

Calendar c = Calendar.getInstance() ;
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
insuranceExpiryDate.setYear(year) ;
insuranceExpiryDate.setMonth(month) ;
insuranceExpiryDate.setDate(day);

In my onCreateDialog

case INSURANCE_EXPIRY:
changeInsuranceExpiryDialog = new DatePickerDialog(this,
    new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(
        DatePicker changeInsuranceDatePicker, int year,
            int month, int day) {
                god.updateDisplay(changeInsuranceExpiryDate,
                insuranceExpiryDate, year, month, day);
        }
    }, mYear, mMonth, mDay);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top