DateFieldItemStateListenerDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import java.util.*;
  3. import javax.microedition.midlet.MIDlet;
  4. public class DateFieldItemStateListenerDemo  extends MIDlet implements CommandListener
  5. {
  6.     private final static Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
  7.     private DateField mDateField;
  8.     private Form mainForm;
  9.     private StringItem stringItem;
  10.     public DateFieldItemStateListenerDemo() {
  11.     mainForm = new Form("DateField演示");
  12.     }                                                        
  13.     protected void startApp() {
  14.             mDateField = new DateField("Date", DateField.DATE_TIME);
  15.             mDateField.setDate(new Date());
  16.             mainForm.append(mDateField);
  17.             stringItem = new StringItem("ItemStateListener:"," ");
  18.             mainForm.append(stringItem);
  19.             mainForm.addCommand(CMD_EXIT);
  20.             mainForm.setCommandListener(this);  
  21.             Display.getDisplay(this).setCurrent(mainForm);
  22.             ItemStateListener listener = new  ItemStateListener(){
  23.             public void itemStateChanged(Item item){
  24.             if(item == mDateField){
  25.             Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
  26.             calendar.setTime(mDateField.getDate());
  27.             Date today = calendar.getTime();
  28.             String timeString = calendar.get(Calendar.YEAR)+"年"+
  29.             getMonth(calendar)+calendar.get(Calendar.DATE)+"日"+
  30.             getWeek(calendar)+calendar.get(Calendar.HOUR_OF_DAY)+"时"+
  31.             getAM_PM(calendar)+calendar.get(Calendar.HOUR)+"时"+
  32.             calendar.get(Calendar.MINUTE)+"分"+
  33.             calendar.get(Calendar.SECOND)+"秒"+
  34.             calendar.get(Calendar.MILLISECOND)+"毫秒";
  35.             stringItem.setText(timeString);          
  36.             }
  37.             }            
  38.             };
  39.             mainForm.setItemStateListener(listener);
  40.     }  
  41.     private String getMonth(Calendar c){
  42.     switch(c.get(Calendar.MONTH)){
  43.     case Calendar.JANUARY:return"一月";
  44.     case Calendar.FEBRUARY:return"二月";
  45.     case Calendar.MARCH:return"三月";
  46.     case Calendar.APRIL:return"四月";
  47.     case Calendar.MAY:return"五月";
  48.     case Calendar.JUNE:return"六月";
  49.     case Calendar.JULY:return"七月";
  50.     case Calendar.AUGUST:return"八月";
  51.     case Calendar.SEPTEMBER:return"九月";
  52.     case Calendar.OCTOBER:return"十月";
  53.     case Calendar.NOVEMBER:return"十一月";
  54.     case Calendar.DECEMBER:return"十二月";
  55.     }
  56.     return null;
  57.     }
  58.     private String getWeek(Calendar c){
  59.     switch(c.get(Calendar.DAY_OF_WEEK)){
  60.     case Calendar.SUNDAY:return"星期日";
  61.     case Calendar.MONDAY:return"星期一";
  62.     case Calendar.TUESDAY:return"星期二";
  63.     case Calendar.WEDNESDAY:return"星期三";
  64.     case Calendar.THURSDAY:return"星期四";
  65.     case Calendar.FRIDAY:return"星期五";
  66.     case Calendar.SATURDAY:return"星期六";
  67.     };
  68.     return null;
  69.     } 
  70.     private String getAM_PM(Calendar c){
  71.     switch(c.get(Calendar.AM_PM)){
  72.     case Calendar.AM:return"上午";
  73.     case Calendar.PM:return"下午";
  74.     };
  75.     return null;
  76.     }
  77.     protected void destroyApp(boolean unconditional) {
  78.     }    
  79.     protected void pauseApp() {
  80.     }   
  81.     public void commandAction(Command c, Displayable d) {
  82.         if (c == CMD_EXIT) {
  83.             destroyApp(false);
  84.             notifyDestroyed();
  85.         }
  86.     }    
  87. }