为什么我的J2ME DateField没有显示正确的日期?

时间:2022-01-20 16:49:05

I am storing values and date values in a record store. I have my date field set up like this:

我将值和日期值存储在记录存储中。我的日期字段设置如下:

StartDate = new DateField("Start Date ", DateField.DATE);
                cal1 = Calendar.getInstance();
                cal1.set(Calendar.YEAR, 2009);
                cal1.set(Calendar.MONTH, 0);
                cal1.set(Calendar.DAY_OF_MONTH, 1);
                StartDate.setDate(cal1.getTime());

and I save the date as a string as follows:

并将日期保存为字符串,如下所示:

 strStartDate = cal1.get(cal1.DAY_OF_MONTH) + "/" +
                (cal1.get(cal1.MONTH) + 1) + "/" +
                cal1.get(cal1.YEAR);
 String detailsToAdd = strStartDate

(I have shortened the code.) Now, I want to be able to edit the date at a future stage. However, I need the code to be able to do this. So far I have:

(我缩短了代码。)现在,我希望能够在未来阶段编辑日期。但是,我需要代码才能执行此操作。到目前为止我有:

EStartDate = new DateField("Start Date ", DateField.DATE);

I had to change the name of the DateField box as this was conflicting with other things.

我不得不更改DateField框的名称,因为这与其他事情相冲突。

I basically need to be able to show the selected record's date attribute. I currently have the other information displayed. I just need to be able to show the correct date. When I run the program the date field says <date>.

我基本上需要能够显示所选记录的日期属性。我目前显示其他信息。我只需要能够显示正确的日期。当我运行程序时,日期字段显示

Any help will be nice

任何帮助都会很好

3 个解决方案

#1


Your first code as followed is correct:

您的第一个代码是正确的:

            StartDate = new DateField("Start Date ", DateField.DATE);
            cal1 = Calendar.getInstance();
            cal1.set(Calendar.YEAR, 2009);
            cal1.set(Calendar.MONTH, 0);
            cal1.set(Calendar.DAY_OF_MONTH, 1);
            StartDate.setDate(cal1.getTime());

But for the second one, you can't save date to String directly. You must convert it to String like this:

但对于第二个,您无法直接将日期保存到String。您必须将其转换为String,如下所示:

            String detailsToAdd =strStartDate.toString();

#2


Could it be that the locale you are running in does not accept dates with / as a separator? I am sure there is a locale independent way to compose a date without using string concatenation as in your example.

可能是您运行的区域设置不接受带/作为分隔符的日期?我确信有一种独立于语言环境的方法来组成日期而不使用字符串连接,如您的示例中所示。

#3


Code snippet you provided doesn't use setDate(Date). If your actual code doesn't use it too, then <date> in the field is correct behavior - it tells you that date is "not initialized".

您提供的代码段不使用setDate(Date)。如果您的实际代码也没有使用它,那么字段中的 是正确的行为 - 它告诉您日期是“未初始化”。

In that case, first smoke-test it with some simple code, like

在这种情况下,首先用一些简单的代码对其进行抽烟测试,例如

    eStartDate = new DateField("Start Date ", DateField.DATE);
    eStartDate.setDate(new Date());

then, if that shows the date, learn how to recreate needed Date parameter from strStartDate. That is, recreate if you want user to be able to modify it - because if all you need is to display the date, it would be simpler to use StringItem displaying needed string instead

然后,如果显示日期,请学习如何从strStartDate重新创建所需的Date参数。也就是说,如果您希望用户能够修改它,请重新创建 - 因为如果您只需要显示日期,那么使用StringItem显示所需的字符串会更简单

#1


Your first code as followed is correct:

您的第一个代码是正确的:

            StartDate = new DateField("Start Date ", DateField.DATE);
            cal1 = Calendar.getInstance();
            cal1.set(Calendar.YEAR, 2009);
            cal1.set(Calendar.MONTH, 0);
            cal1.set(Calendar.DAY_OF_MONTH, 1);
            StartDate.setDate(cal1.getTime());

But for the second one, you can't save date to String directly. You must convert it to String like this:

但对于第二个,您无法直接将日期保存到String。您必须将其转换为String,如下所示:

            String detailsToAdd =strStartDate.toString();

#2


Could it be that the locale you are running in does not accept dates with / as a separator? I am sure there is a locale independent way to compose a date without using string concatenation as in your example.

可能是您运行的区域设置不接受带/作为分隔符的日期?我确信有一种独立于语言环境的方法来组成日期而不使用字符串连接,如您的示例中所示。

#3


Code snippet you provided doesn't use setDate(Date). If your actual code doesn't use it too, then <date> in the field is correct behavior - it tells you that date is "not initialized".

您提供的代码段不使用setDate(Date)。如果您的实际代码也没有使用它,那么字段中的 是正确的行为 - 它告诉您日期是“未初始化”。

In that case, first smoke-test it with some simple code, like

在这种情况下,首先用一些简单的代码对其进行抽烟测试,例如

    eStartDate = new DateField("Start Date ", DateField.DATE);
    eStartDate.setDate(new Date());

then, if that shows the date, learn how to recreate needed Date parameter from strStartDate. That is, recreate if you want user to be able to modify it - because if all you need is to display the date, it would be simpler to use StringItem displaying needed string instead

然后,如果显示日期,请学习如何从strStartDate重新创建所需的Date参数。也就是说,如果您希望用户能够修改它,请重新创建 - 因为如果您只需要显示日期,那么使用StringItem显示所需的字符串会更简单