Java 从Calendar.getTime()设置日期值时年份不正确

Java 从Calendar.getTime()设置日期值时年份不正确,java,date,calendar,gettime,Java,Date,Calendar,Gettime,我正在为Android构建一个日历显示应用程序。除此之外,该应用程序允许用户选择要添加到历元的天数(历元是1899年的一个日期),结果将以图形方式显示在显示屏上。然而,当我从日历值设置日期值时,我得到了意想不到的结果:年份似乎偏离了1900。虽然我可以简单地在结果上加上1900,但这似乎是错误的答案,因为我所做的(我相信)是按照书上所说的。或者我可以继续我所拥有的;这也行。但这与本书的发行情况相同。以下是代码,其中包含用于调试的备用变量: public void gotoCal(int days

我正在为Android构建一个日历显示应用程序。除此之外,该应用程序允许用户选择要添加到历元的天数(历元是1899年的一个日期),结果将以图形方式显示在显示屏上。然而,当我从日历值设置日期值时,我得到了意想不到的结果:年份似乎偏离了1900。虽然我可以简单地在结果上加上1900,但这似乎是错误的答案,因为我所做的(我相信)是按照书上所说的。或者我可以继续我所拥有的;这也行。但这与本书的发行情况相同。以下是代码,其中包含用于调试的备用变量:

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);
}
现在,当我以gotoCal(40000)的身份在调试模式下运行此代码时,在gotoDate()行上有一个断点,我在字段中看到以下值: 年份=2009年, 第2年=109, 第3年=2009年

Calendar.getTime()函数是否应该返回1900年前的日期(我没有看到这样的文档)?或者我可能正在做一些事情来影响它去做那件事?或者我可能做错了什么?

您误读(或未阅读)了以下文档:

返回一个值,该值是从包含该日期对象表示的时间瞬间或以该日期对象表示的时间瞬间开始的年份中减去1900的结果,如在本地时区中解释的那样

此外:

  • Date.getYear
    不推荐使用,这是有充分理由的。它使用默认时区,基本上不应该使用

  • 是更好的日期和时间API。我完全建议使用它而不是
    日期
    /
    日历

您误读(或未阅读)以下文档:

返回一个值,该值是从包含该日期对象表示的时间瞬间或以该日期对象表示的时间瞬间开始的年份中减去1900的结果,如在本地时区中解释的那样

此外:

  • Date.getYear
    不推荐使用,这是有充分理由的。它使用默认时区,基本上不应该使用

  • 是更好的日期和时间API。我完全建议使用它而不是
    日期
    /
    日历


我在android emulator上做了以下操作来修复此问题

private Date insuranceExpiryDate = new Date(Calendar.getInstance().getTime().getTime());
private int mYear, mMonth, mDay ;
在我的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);
在我的onCreate对话框中

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);

为了解决android emulator上的这个问题,我做了以下操作

private Date insuranceExpiryDate = new Date(Calendar.getInstance().getTime().getTime());
private int mYear, mMonth, mDay ;
在我的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);
在我的onCreate对话框中

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);

哦,你说得对。非常感谢您的快速响应!如果系统允许,我会接受这个答案。哦,你说得对。非常感谢您的快速响应!如果系统允许,我会接受这个答案。