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

发布于 2024-11-29 13:25:46 字数 1067 浏览 2 评论 0原文

我正在为 Android 构建 CalendarDisplay 应用程序。除此之外,该应用程序允许用户选择添加到纪元的天数(纪元是 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 年, 第二年 = 109 , year3 = 2009

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的鱼塘能养鲲 2024-12-06 13:25:46

您误读(或未阅读)Date.getYear

返回一个值,该值是从包含此 Date 对象表示的时间点的年份中减去 1900 所得的结果(按照本地时区的解释)。

另外:

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

  • Joda Time更好的日期和时间 API。我强烈建议使用它来代替 Date/Calendar

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.

滴情不沾 2024-12-06 13:25:46

我执行了以下操作来解决 Android 模拟器上的此问题。

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

在我的 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文