将自定义日期设置为 Android 日历

发布于 2024-12-07 18:07:31 字数 1140 浏览 1 评论 0原文

我正在创建一个应用程序,并试图让日历功能正常工作。到目前为止,事件已添加,但我无法获得正确的自定义日期。我相信这可能是格式,但我正在努力使其正确。

我得到的代码如下:

@Override
    public void onItemClick(AdapterView<?> adp, View v, int position, long id) 
    {
        FixtureSupport fixture = (FixtureSupport) adapter.getItem(position);

        try
        {
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            Date date = (Date)formatter.parse("10/05/2012");

            Intent event = new Intent(Intent.ACTION_EDIT);
            event.setType("vnd.android.cursor.item/event");
            event.putExtra("title", "Rugby Match");
            event.putExtra("description", fixture.getHome() + " V " + fixture.getAway());
            event.putExtra("eventLocation", fixture.getVenue());
            event.putExtra("hasAlarm", 1);
            event.putExtra("startTime", date);
            startActivity(event);
        }
        catch (Exception e)
        {
            System.err.println("Error");
        }
    }

当我单击屏幕上的项目时,标题/描述/位置都有效。但是当我尝试设置 startTime (事件日期)时,它不起作用。目前,当我创建一个明天开始的活动时,它说该活动将在一分钟后开始!

帮助将不胜感激,谢谢大家!

I am creating an app and I am trying to get the calendar functionality working properly. So far the event gets added but I cannot get the custom date correct. I believe it may be the format but I am struggling to get that correct.

The code I got is the following:

@Override
    public void onItemClick(AdapterView<?> adp, View v, int position, long id) 
    {
        FixtureSupport fixture = (FixtureSupport) adapter.getItem(position);

        try
        {
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            Date date = (Date)formatter.parse("10/05/2012");

            Intent event = new Intent(Intent.ACTION_EDIT);
            event.setType("vnd.android.cursor.item/event");
            event.putExtra("title", "Rugby Match");
            event.putExtra("description", fixture.getHome() + " V " + fixture.getAway());
            event.putExtra("eventLocation", fixture.getVenue());
            event.putExtra("hasAlarm", 1);
            event.putExtra("startTime", date);
            startActivity(event);
        }
        catch (Exception e)
        {
            System.err.println("Error");
        }
    }

When I click the item on the screen, the title / description / location all work. But when I try and set the startTime (date of the event) it will not work. Currently when I create a event thats meant to be starting tomorrow, it says the event will begin in 1minute!

Help will be appreciated, thanks guys!

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

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

发布评论

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

评论(1

池予 2024-12-14 18:07:31

您是否尝试过:

event.putExtra("startTime", "10/05/2012");

当您访问传递的变量时:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(bundle.getString("startTime"));

Did you try:

event.putExtra("startTime", "10/05/2012");

And when you are accessing passed variables:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(bundle.getString("startTime"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文