问题
I have created a appoitment for particular date and time in Blackberry calendar,i am trying to read date and time using the following code,but its showing the error.
private void getEvents() {
try {
EventList eventList = (EventList)PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.READ_ONLY);
Enumeration events = eventList.items();
while (events.hasMoreElements()) {
Event event = (Event)events.nextElement();
if(eventList.isSupportedField(Event.ALARM) && event.countValues(Event.ALARM) > 0) {
long alarm = event.getDate(Event.ALARM, 0);
System.out.println(alarm);
}
}
}
i am not sure what is wrong in if loop
回答1:
The field Event.ALARM contains:
Field specifying a relative time for an Alarm for this Event. Data for this field is expressed with an INT data type. The alarm is expressed in seconds and derived by subtracting the alarm value from every date/time occurrence of this Event. For example, if this field has a value of 600, then the alarm first occurs 600 seconds before the date/time value specified by Event.START. For re-occurrences of the event, the alarm is calculated by subtracting the stored value from the date/time of the specific event occurrence.
So you need to get the value from the field Event.START for the Date/Time of the Event start. You can then subtract the value of Event.ALARM (as seconds) from the start Date/Time to get the time for any requested reminder.
long start = event.getDate(Event.START);
int alarm = event.getDate(Event.ALARM);
if (alarm > 0) {
long reminderTime = start - (long)alarm * 1000L;
...
}
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm");
String dateString = sdf.formatLocal(start);
来源:https://stackoverflow.com/questions/9584050/not-able-to-read-the-time-from-native-calendar-event