Java Tutorial/J2ME/DateField

Материал из Java эксперт
Перейти к: навигация, поиск

Add DateField with current time to a form

import java.util.Calendar;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class DateFieldTIME extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Demo");
    form.append("line");
    DateField timeOnly = new DateField("Time", DateField.TIME);
    timeOnly.setDate(Calendar.getInstance().getTime());
    form.append(timeOnly);
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Create DateField for Date

import java.util.Calendar;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class DateFieldSetDate extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Demo");
    form.append("line");
    DateField timeOnly = new DateField("Time", DateField.DATE);
    timeOnly.setDate(Calendar.getInstance().getTime());
    form.append(timeOnly);
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Create DateField for Date and Time

import java.util.Calendar;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class DateFieldDATE_TIME extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Demo");
    form.append("line");
    DateField timeOnly = new DateField("Time", DateField.DATE_TIME);
    timeOnly.setDate(Calendar.getInstance().getTime());
    form.append(timeOnly);
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Today"s date in DateField

import java.util.Date;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
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 J2MEDateToday extends MIDlet implements CommandListener {
  private Display display;
  private Form form = new Form("Today"s Date");
  private Date today = new Date(System.currentTimeMillis());
  private Command exit = new Command("Exit", Command.EXIT, 1);
  private DateField datefield = new DateField("", DateField.DATE_TIME);
  public J2MEDateToday() {
    display = Display.getDisplay(this);
    datefield.setDate(today);
    form.append(datefield);
    form.addCommand(exit);
    form.setCommandListener(this);
  }
  public void startApp() {
    display.setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}





Use DateField

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
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();
    }
  }
}