J2ME Dates & the Blackberry
I wrote a BB based diary application for a conference last year. It allowed delegates to view conference sessions and search for speakers etc. Updating it recently I wanted the users to be able to add calendar entries. This is pretty straightforward. There's a code listing below but before that a step by step in plain english ![]()
- Get an Eventlist instance for reading and writing
- Create a new event and give it a title and location
- Use a Java Calendar instance to create a date/time in a particular timezone (One for the start & reuse for the end)
- Use an AddEvent call to set the start time and another to set the end time
- Create an alarm for the event
- Commit the changes and close the Eventlist
I chose to add the "Add to calendar" function to a menu item. The menutiem is available when the delegate views any session. It takes two clicks to add a session to the calendar. It would probably be nice to check for conflicts too I suppose.
The code with some inline comments follows. If this were a real menu item it ought to create a calendar entry for January 21st 2007 14:00 - 14:35 hours and create an alarm associated with it.
Hope it's of interest,
Jason
private MenuItem _addSession = new MenuItem("Add to Calendar", 120, 10) {
public void run() {
EventList eventList = null;
try {
// the catch clause is at the the end of the new MenuItem
eventList = (EventList)PIM.getInstance().openPIMList(
PIM.EVENT_LIST, PIM.READ_WRITE);
Event event = eventList.createEvent();
if (eventList.isSupportedField(Event.SUMMARY)) {
// I'm getting the title from a field on the screen that's being viewed
event.addString(Event.SUMMARY, Event.ATTR_NONE, _titleField.getText());
}
if (eventList.isSupportedField(Event.LOCATION)) {
// I'm getting the location information from the screen too
event.addString(Event.LOCATION, Event.ATTR_NONE, _locationField.getText());
}
java.util.TimeZone tz = java.util.TimeZone.getTimeZone("America/New_York");
java.util.Calendar calendar = java.util.Calendar.getInstance(tz);
// In this section we are constructing the start time for the event
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,2007);
c.set(Calendar.MONTH,Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH,21);
c.set(Calendar.HOUR,14);
c.set(Calendar.MINUTE,05);
c.set(Calendar.MILLISECOND,0);
if (eventList.isSupportedField(Event.START)) {
event.addDate(Event.START, Event.ATTR_NONE, c.getTime().getTime());
}
// In this section we are constructing the end time for the event
c.set(Calendar.HOUR,14);
c.set(Calendar.MINUTE,35);
c.set(Calendar.MILLISECOND,0);
if (eventList.isSupportedField(Event.END)) {
event.addDate(Event.END, Event.ATTR_NONE, c.getTime().getTime());
}
if (eventList.isSupportedField(Event.ALARM)) {
if (event.countValues(Event.ALARM) > 0) {
event.removeValue(Event.ALARM,0);
event.setInt(Event.ALARM, 0, Event.ATTR_NONE, 396000);
}
}
try{
if(event.isModified()) {
event.commit();
}
}catch (PIMException e) {
// Handle exception.
}
try {
eventList.close();
} catch (PIMException e) {
// Handle exception.
}
}
} catch (Exception e) {
// Handle exception.
}
};


Comments