Use DateField : DateField « J2ME « Java Tutorial






Use DateField
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;

public class DateFieldMIDlet extends MIDlet implements CommandListener {

  private Command exitCommand;

  private DateField dateDateField = new DateField("date:", DateField.DATE);

  private DateField timeDateField = new DateField("time:", DateField.TIME);

  private DateField bothDateField = new DateField("both:", DateField.DATE_TIME);

  private Display display;

  public DateFieldMIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("exit", Command.EXIT, 1);
    java.util.Date now = new java.util.Date();
    dateDateField.setDate(now);
    timeDateField.setDate(now);
    bothDateField.setDate(now);
  }

  public void startApp() {
    Form aForm = new Form("DateField");

    aForm.append(dateDateField);
    aForm.append(timeDateField);
    aForm.append(bothDateField);
    aForm.addCommand(exitCommand);
    aForm.setCommandListener(this);
    display.setCurrent(aForm);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}








31.6.DateField
31.6.1.Add DateField with current time to a formAdd DateField with current time to a form
31.6.2.Create DateField for DateCreate DateField for Date
31.6.3.Create DateField for Date and Time
31.6.4.Use DateFieldUse DateField
31.6.5.Today's date in DateFieldToday's date in DateField