▼MIDP2.0メモ▼
日付フィールドを使う


日付フィールドを使うプログラムです。

  


プログラム
DateFieldEx.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

//日付フィールドを使う(本体)
public class DateFieldEx extends MIDlet {
    static MIDlet current;

    //コンストラクタ
    public DateFieldEx(){
        current=this;
        (Display.getDisplay(this)).setCurrent(new DateFieldForm());
    }

    //アプリの開始
    public void startApp() {
    }

    //アプリの一時停止
    public void pauseApp() {
    }

    //アプリの終了
    public void destroyApp(boolean unconditional) {
    }
}


DateFieldForm.java
import javax.microedition.lcdui.*;
import java.util.*;

//日付フィールドを使う(フォーム)
class DateFieldForm extends Form
    implements CommandListener {
    DateField dateField0;//日付フィールド0(DATE)
    DateField dateField1;//日付フィールド1(DATE_TIME)
    DateField dateField2;//日付フィールド2(TIME)
    Command   soft1;     //ソフトキー1

    //コンストラクタ
    DateFieldForm(){
        super("DataFieldEx");

        //日付フィールド0(DATE)
        dateField0=new DateField("DATE",DateField.DATE);
        append(dateField0);

        //日付フィールド1(DATE_TIME)
        dateField1=new DateField("DATE_TIME",DateField.DATE_TIME);
        append(dateField1);

        //日付フィールド2(TIME)
        dateField2=new DateField("TIME",DateField.TIME);
        append(dateField2);

        //ソフトキー
        soft1=new Command("OK",Command.SCREEN,0);
        addCommand(soft1);
        setCommandListener(this);
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        if (c==soft1) {
            Date date=dateField0.getDate();
            if (date==null) return;

            //年月日
            Calendar cal=Calendar.getInstance();
            cal.setTime(date);
            int year =cal.get(Calendar.YEAR);
            int month=cal.get(Calendar.MONTH)+1;
            int day  =cal.get(Calendar.DAY_OF_MONTH);

            //アラート表示
            Alert alert=new Alert("DATE",
                year+"/"+month+"/"+day,null,AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            (Display.getDisplay(DateFieldEx.current)).setCurrent(alert);
        }
    }
}



−戻る−