DateField sample program in J2ME

By: Jagan Printer Friendly Format    

First of all, notice that DateFieldDemo extends the Form class. The constructor simply adds a
DateField object to the form and the necessary structure is in place. A DateField is simply a text element that displays a date and time. 

When you run this midlet, the first line in the output, "Date/Time in GMT," is the label and is specified in the first argument of the constructor. The second line is the date, and the third line is the time. The DateFieldDemo no-arg constructor in the program above demonstrates how to set the date of the 
DateField object using a java.util.Calendar object.

This example displays the date and time because the constructor call specifies the display of both quantities. The DateField class defines three constants, which let you control what information is displayed.

import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
Demonstrates the use of the MIDP UI DateField class.
@see javax.microedition.lcdui.DateField
public class DateFieldDemo extends Form
implements CommandListener
private Command back = new Command("Back", Command.BACK, 1);
private static Displayable instance;
private DateField date =
new DateField("Date/Time in GMT",
public DateFieldDemo()
super("DateField Demo");
Calendar cal = Calendar.getInstance();
instance = this;
Returns the single instance of this class. Calling
this method before constructing an object will return
a null pointer.
@return an instance of this class.
public static Displayable getInstance()
return instance;
public void commandAction(Command c, Displayable d)
if (c == back)


The third DateField constructor argument is the time zone specification, which is a java.util.TimeZone object. Beware of the fact that the MIDP specification requires implementations to support only one time zone. You should be aware of what time zones your implementation supports. It's quite possible that most MIDP implementations will only support one time zone.

A DateField constructor call can specify a time zone that is not supported by your MIDP implementation. If the time zone you specify in the constructor is not supported by your MIDP implementation, your program will still execute without error or warning, but the DateField object's time zone will represent whatever time zone is supported by the implementation, not the one you requested. And the time displayed on screen will reflect the time zone used by the DateField object instead of the time zone you specified in the constructor call.

DateField objects are editable. To edit them,

1. First, select the date field.
2. Click the Select button. You'll see the display change.
3. Scroll up and down to highlight the year, month, or day, and change each as desired.

Notice that the implementation has placed a Back soft button and a Save soft button on the display.
This interface presentation is typical of all editable components. When you finish editing and revert to the previous screen, the displayed date and time will have changed.

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)

Comment on this tutorial