日历提醒

时间:2017-11-10 10:54:50

标签: android calendar

我在android中创建了日历提醒但是我正在尝试修复的错误:

  

我想腾出时间提醒:情景1:上午10点和情景2:6PM

每当我创建提醒并设置时间时,它总是设置为AM而不是PM如何配置AM和PM过滤器我尝试设置日历AM PM格式但是似乎不起作用

以下是我的代码段:

private void setReminderToCalendar() {

    try {
        if (format.trim().toLowerCase().equals("am")) {
            calendar.set(Calendar.AM_PM, Calendar.AM);
        } else {
            calendar.set(Calendar.AM_PM, Calendar.PM);
        }
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr));
        calendar.set(Calendar.MINUTE, Integer.parseInt(min));
        calendar.add(Calendar.HOUR, -2); // create reminder 2 hr ago
        insertReminderInCalendar(FindingVendorAndTimelineActivity.this, calendar);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void insertReminderInCalendar(Activity activity, Calendar cal) {

    Uri EVENTS_URI = Uri.parse(getCalendarUriBase(true) + "events");
    ContentResolver cr = activity.getContentResolver();
    TimeZone timeZone = TimeZone.getDefault();

    /** Inserting an event in calendar. */
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.CALENDAR_ID, 1);
    values.put(CalendarContract.Events.TITLE, "  Order Configured");
    values.put(CalendarContract.Events.DESCRIPTION, "You have schedule roti order today");
    values.put(CalendarContract.Events.ALL_DAY, 0);
    // event starts at 0 minutes from now
    values.put(CalendarContract.Events.DTSTART, cal.getTimeInMillis());
    // ends 60 minutes from now
    values.put(CalendarContract.Events.DTEND, cal.getTimeInMillis() + 2 * 60 * 1000);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
    values.put(CalendarContract.Events.HAS_ALARM, 1);
    Uri event = cr.insert(EVENTS_URI, values);

    // Display event id.
    Toast.makeText(RotiApplication.getInstance(), "  scheduled order is been added to you calendar", Toast.LENGTH_SHORT).show();

    /** Adding reminder for event added. */
    Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(true) + "reminders");
    values = new ContentValues();
    values.put(CalendarContract.Reminders.EVENT_ID, Long.parseLong(event.getLastPathSegment()));
    values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
    values.put(CalendarContract.Reminders.MINUTES, 10);
    cr.insert(REMINDERS_URI, values);
}

如何在Android中的日历事件中配置AM / PM过滤器?

1 个答案:

答案 0 :(得分:1)

  

calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hr));

使用HOUR_OF_DAY以24小时格式设置时间(下午6点将是18小时)。使用HOUR_OF_DAY会使am / pm无用。使用24小时格式并手动设置上午/下午。日历会自动将其转换为上午/下午格式。