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

发布于 2024-07-16 16:15:16 字数 911 浏览 5 评论 0原文

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

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

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

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

我已缩短了代码。)现在,我希望能够在未来阶段编辑日期。 但是,我需要代码才能做到这一点。 到目前为止,我已经:

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

我必须更改 DateField 框的名称,因为这与其他内容相冲突。

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

任何帮助都会很好

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.

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

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

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

发布评论

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

评论(3

失而复得 2024-07-23 16:15:16

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

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

但对于第二个代码,您不能直接将日期保存到字符串。 您必须将其转换为字符串,如下所示:

            String detailsToAdd =strStartDate.toString();

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 detailsToAdd =strStartDate.toString();
嘿咻 2024-07-23 16:15:16

难道是您正在运行的区域设置不接受以 / 作为分隔符的日期? 我确信有一种独立于语言环境的方法来组成日期,而无需像您的示例中那样使用字符串连接。

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.

美人如玉 2024-07-23 16:15:16

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

在这种情况下,首先使用一些简单的代码对其进行冒烟测试,就像

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

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

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".

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

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