⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datedemomidlet.java

📁 里面的工程文件是本人做的一些小程序
💻 JAVA
字号:
package formdatedemo;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class DateDemoMIDlet extends MIDlet implements ItemStateListener{
  static DateDemoMIDlet instance;
   DateField dtfield; //时间日期输入
   ChoiceGroup cgrpTimeZone;//用于时区选择
   Form mainForm;
   String[] timeZoneID;//存储时区
   Calendar clndTemp;//获取日期数据的日历对象
   String nowZoneID="GMT";//记录当前时区
  public DateDemoMIDlet() {
    instance = this;
    timeZoneID = TimeZone.getAvailableIDs();//获取所有支持时区
    cgrpTimeZone = new ChoiceGroup("请选择时区",
                                   Choice.POPUP,timeZoneID,null);
    dtfield = new DateField("请选择日期",
                            DateField.DATE,TimeZone.getTimeZone("GMT"));
    mainForm = new Form("日期编辑—"+nowZoneID);
    clndTemp = getNowCalendar(TimeZone.getTimeZone(nowZoneID),new Date());
    mainForm.append(cgrpTimeZone);
    mainForm.append(dtfield);
    mainForm.append("当前时区:"+nowZoneID+
                    "\n 选取的日期:\n"+FormatToChineseDate(clndTemp));
    mainForm.setItemStateListener(this);
  }

  private void createNewDtField(TimeZone tz){
    for(int i=mainForm.size()-1;i>0;i--){
      mainForm.delete(i);
    }
    dtfield=new DateField("请选择日期",DateField.DATE,tz);
    mainForm.append(dtfield);
    mainForm.append("当前时区:"+nowZoneID+
                    "\n 选取的日期:\n"+FormatToChineseDate(clndTemp));
  }
  private Calendar getNowCalendar(TimeZone tz,Date dt){
    Calendar clndNow;
    clndNow=Calendar.getInstance(tz);
    clndNow.setTime(dt);
    return clndNow;
  }

  public void startApp() {
    Display.getDisplay(this).setCurrent(mainForm);
  }

  public void pauseApp() { }

  public void destroyApp(boolean unconditional) { }

  //根据日立对象格式化日期字符串
  private String FormatToChineseDate(Calendar cld){
    String[] WeekDays={"日","一","二","三","四","五","六"};
    StringBuffer strBuffer = new StringBuffer();
    strBuffer.append(cld.get(Calendar.YEAR));
    strBuffer.append("年");
    strBuffer.append(cld.get(Calendar.MONTH)+1);
    strBuffer.append("月");
    strBuffer.append(cld.get(Calendar.DAY_OF_MONTH));
    strBuffer.append("日");
    strBuffer.append("星期");
    int index=cld.get(Calendar.DAY_OF_WEEK)-Calendar.SUNDAY;
    strBuffer.append(WeekDays[index]);
    return strBuffer.toString();
  }

  public void itemStateChanged(Item item){
    if(item==cgrpTimeZone){
      int index=cgrpTimeZone.getSelectedIndex();
      nowZoneID=cgrpTimeZone.getString(index);
      TimeZone tzone=TimeZone.getTimeZone(nowZoneID);
      mainForm.setTitle("日期编辑—"+nowZoneID);
      createNewDtField(tzone);
    }
    if(item==dtfield){
      clndTemp=getNowCalendar(TimeZone.getTimeZone(nowZoneID),
                              dtfield.getDate());
      mainForm.delete(mainForm.size()-1);
      mainForm.append("当前时区:"+nowZoneID+
                    "\n 选取的日期:\n"+FormatToChineseDate(clndTemp));

    }
  }
  public static void quitApp() {
    instance.destroyApp(true);
    instance.notifyDestroyed();
    instance = null;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -