在MIDlet小程序里面显示时间

j2me中显示时间:

import javax.microedition.lcdui.*;
import java.util.*;
import javax.microedition.midlet.MIDlet;
public class DateFieldItemStateListenerDemo extends MIDlet implements CommandListener
{
private final static Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
private DateField mDateField;
private Form. mainForm;
private StringItem stringItem;
public DateFieldItemStateListenerDemo() {
mainForm. = new Form("DateField演示");
} 
protected void startApp() {
mDateField = new DateField("Date", DateField.DATE_TIME);
mDateField.setDate(new Date());
mainForm.append(mDateField);
stringItem = new StringItem("ItemStateListener:"," ");
mainForm.append(stringItem);
mainForm.addCommand(CMD_EXIT);
mainForm.setCommandListener(this); 
Display.getDisplay(this).setCurrent(mainForm);
ItemStateListener listener = new ItemStateListener(){
public void itemStateChanged(Item item){
if(item == mDateField){
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(mDateField.getDate());
Date today = calendar.getTime();
String timeString = calendar.get(Calendar.YEAR)+"年"+
getMonth(calendar)+calendar.get(Calendar.DATE)+"日"+
getWeek(calendar)+calendar.get(Calendar.HOUR_OF_DAY)+"时"+
getAM_PM(calendar)+calendar.get(Calendar.HOUR)+"时"+
calendar.get(Calendar.MINUTE)+"分"+
calendar.get(Calendar.SECOND)+"秒"+
calendar.get(Calendar.MILLISECOND)+"毫秒";
stringItem.setText(timeString); 
}
} 
};
mainForm.setItemStateListener(listener);
} 
private String getMonth(Calendar c){
switch(c.get(Calendar.MONTH)){
case Calendar.JANUARY:return"一月";
case Calendar.FEBRUARY:return"二月";
case Calendar.MARCH:return"三月";
case Calendar.APRIL:return"四月";
case Calendar.MAY:return"五月";
case Calendar.JUNE:return"六月";
case Calendar.JULY:return"七月";
case Calendar.AUGUST:return"八月";
case Calendar.SEPTEMBER:return"九月";
case Calendar.OCTOBER:return"十月";
case Calendar.NOVEMBER:return"十一月";
case Calendar.DECEMBER:return"十二月";
}
return null;
} 
private String getWeek(Calendar c){
switch(c.get(Calendar.DAY_OF_WEEK)){
case Calendar.SUNDAY:return"星期日";
case Calendar.MONDAY:return"星期一";
case Calendar.TUESDAY:return"星期二";
case Calendar.WEDNESDAY:return"星期三";
case Calendar.THURSDAY:return"星期四";
case Calendar.FRIDAY:return"星期五";
case Calendar.SATURDAY:return"星期六";
};
return null;
} 
private String getAM_PM(Calendar c){
switch(c.get(Calendar.AM_PM)){
case Calendar.AM:return"上午";
case Calendar.PM:return"下午";
};
return null;
}
protected void destroyApp(boolean unconditional) {
} 
protected void pauseApp() {
} 
public void commandAction(Command c, Displayable d) {
if (c == CMD_EXIT) {
destroyApp(false);
notifyDestroyed();
}
} 
}

 

你可能感兴趣的:(时间)