Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Display dates and times in handheld applications

by Retired ‎02-16-2010 12:54 PM - edited ‎09-16-2010 02:33 PM (2,147 Views)



This article applies to the following:

  • BlackBerry® Java Development Environment (JDE)
  • BlackBerry Wireless Handhelds™ based on Java™



Representing dates and times in BlackBerry handheld applications is often problematic because of the differences between locales and formats. Java 2 Platform Standard Edition (J2SE) provides a number of classes for simplifying the representation of dates and times. These include java.util.Calendar, java.util.Date, java.util.TimeZone, and java.text.DateFormat. In Java 2 Platform Micro Edition (J2ME), the classes that are available for this purpose are not as powerful. For this reason, the RIM application programming interface (API) provides the DateField (net.rim.device.api.ui.component package) and DateFormat (net.rim.device.api.i18n package) classes.


The DateField UI component is used to display dates and times. It also enables the user to edit the displayed dates and times. The DateField component can be set to operate in different modes (for example, DATE, DATE_TIME, TIME, Field.EDITABLE, and Field.READONLY). For more information on these modes, see the API documentation.




The following sample code displays the DateField in three different modes (DATE, TIME, and DATE_TIME):



MainScreen mainScreen = new MainScreen(Field.USE_ALL_HEIGHT|Field.FIELD_LEFT);
Date date = new Date();
SimpleDateFormat sdFormat1 = new SimpleDateFormat("yyyy/MM/dd");
DateField dateField1 = new DateField ("DATE Mode", date.getTime(),sdFormat1);
mainScreen.add (dateField1);
SimpleDateFormat sdFormat2 = new SimpleDateFormat("hh:mm");
DateField dateField2 = new DateField ("TIME Mode", date.getTime(), sdFormat2);
mainScreen.add (dateField2);
SimpleDateFormat sdFormat3 = new SimpleDateFormat("yyyy/MM/dd hh:mm");
DateField dateField3 = new DateField ("DATE_TIME Mode", date.getTime(), sdFormat3);
mainScreen.add (dateField3);


In the code sample above, different DateField modes are set by creating different date display formats using the SimpleDateFormat class. The SimpleDateFormat is a subclass of DateFormat class. In the first DateField, the date format provided specifies only the date ("yyyy/MM/dd"), so a field in DATE mode is created. In the second DateField, the date format specifies only time ("hh:mm"), so the field is built in TIME mode. In the third DateField, the date format specifies both date and time values ("yyyy/MM/dd hh:mm"), so the field is built in DATE_TIME mode.


Additional Information


To see an image of the 3 DateFields on the BlackBerry simulator screen, go to http://www.blackberry.net/developers/javaknowledge/tipsandsamplecode/download/simulator_fields_datet....

Users Online
Currently online: 44 members 2,446 guests
Please welcome our newest community members: