您的位置:首页 > 其它

j2me学习笔记【6】——获取日期时间

2011-01-27 15:59 351 查看
获取系统的日期和时间,两种方法,一种利用DateField,另一种利用Calendar.

package mtk;

import java.util.Calendar;
import java.util.Date;

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;
import javax.microedition.midlet.MIDletStateChangeException;

public class DataToday extends MIDlet implements CommandListener {
private Display display;
private Form mainForm;
private final static Command CMD_EXIT=new Command("退出",Command.EXIT,1);
public DataToday() {
display=Display.getDisplay(this);
mainForm=new Form("显示日期时间");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int date = c.get(Calendar.DATE);
String hour=String.valueOf(c.get(Calendar.HOUR));
String minute=String.valueOf(c.get(Calendar.MINUTE));
String second=String.valueOf(c.get(Calendar.SECOND));
Date datetime=c.getTime();
mainForm.append(year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second);
DateField time=new DateField("",DateField.DATE_TIME);
time.setDate(datetime);
mainForm.append(time);
mainForm.addCommand(CMD_EXIT);
mainForm.setCommandListener(this);
}

protected void destroyApp(boolean arg0) {
}

protected void pauseApp() {
}

protected void startApp() throws MIDletStateChangeException {
display.setCurrent(mainForm);
}

public void commandAction(Command c, Displayable d) {
if(c==CMD_EXIT){
destroyApp(false);
notifyDestroyed();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息