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?