当前位置: 代码迷 >> J2ME >> j2me中对时间处置的方法 TextFieldWithItemStateListenerMIDlet.java
  详细解决方案

j2me中对时间处置的方法 TextFieldWithItemStateListenerMIDlet.java

热度:3781   发布时间:2013-02-25 21:32:03.0
j2me中对时间处理的方法 TextFieldWithItemStateListenerMIDlet.java
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.microedition.global.Formatter;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;


public class TextFieldWithItemStateListenerMIDlet extends MIDlet implements ItemStateListener {

private Display display;

private TextField name;
private TextField tel;
private TextField summary;
private DateField date;
private DateField datetime;
private DateField time;
private TextField datedisplay;

public TextFieldWithItemStateListenerMIDlet(){
display = Display.getDisplay(this);
}

protected void destroyApp(boolean arg0) {
}

protected void pauseApp() {
}

protected void startApp() {
Form f = new Form("TextField测试");
name = new TextField("姓名","",8,TextField.ANY);
tel = new TextField("电话","",14,TextField.PHONENUMBER);
summary = new TextField("总结","",30,TextField.UNEDITABLE);
date = new DateField("日期",DateField.DATE,TimeZone.getTimeZone("GMT+8:00"));
datetime = new DateField("日期时间",DateField.DATE_TIME,TimeZone.getTimeZone("GMT+8:00"));
time = new DateField("时间",DateField.TIME,TimeZone.getTimeZone("GMT+8:00"));
datedisplay = new TextField("时间","",50,TextField.UNEDITABLE);

System.out.println(datetime.getDate());

f.append(name);
f.append(tel);
f.append(summary);
f.append(date);
f.append(datetime);
f.append(time);
f.append(datedisplay);
f.setItemStateListener(this);
display.setCurrent(f);
}

public void itemStateChanged(Item item) {
summary.setString("");
datedisplay.setString("");
if(item == name){
summary.setString("输入的姓名为:"+name.getString());
}else if(item == tel){
summary.setString("输入的电话为:"+tel.getString());
}else if(item == date){
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
datedisplay.setString(new Formatter().formatDateTime(cal, Formatter.DATE_SHORT));
}else if(item == datetime){
Date dt = datetime.getDate();

long t = dt.getTime();

long shicha = 8*60*60*1000;

dt.setTime(dt.getTime()+shicha);

Calendar cal = Calendar.getInstance();
cal.setTime(dt);

Calendar da = Calendar.getInstance();

Date d = new Date();

d.setTime(d.getTime()+shicha);

da.setTime(d);

long dat = d.getTime();
System.out.println(t-dat);
System.out.println(TextFieldWithItemStateListenerMIDlet.getSystemDateTime(dt));
System.out.println(new Formatter().formatDateTime(da, Formatter.DATETIME_SHORT));

datedisplay.setString(new Formatter().formatDateTime(da, Formatter.DATETIME_SHORT));
}else if(item == time){
Calendar cal = Calendar.getInstance();
cal.setTime(time.getDate());
datedisplay.setString(new Formatter().formatDateTime(cal, Formatter.TIME_SHORT));
}
}

private static String getSystemDateTime(Date d){
        Calendar  ca  =Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
        ca.setTime(d);
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH)+1;
        int date = ca.get(Calendar.DATE);
        int hour = ca.get(Calendar.HOUR_OF_DAY);
        int minute = ca.get(Calendar.MINUTE);
        int second = ca.get(Calendar.SECOND);
        String yearStr = String.valueOf(year);
        String monthStr = String.valueOf(month);
        String dateStr = String.valueOf(date);
        String hourStr = String.valueOf(hour);
        String minuteStr = String.valueOf(minute);
        String secondStr = String.valueOf(second);
        if(month<10){
            monthStr="0"+monthStr;
        }
        if(date<10){
            dateStr="0"+dateStr;
        }
        if(hour+8<10){
            hourStr="0"+hourStr;
        }
      if(minute<10){
          minuteStr="0"+minuteStr;
      }
      if(second<10){
          secondStr="0"+secondStr;
      }

      return yearStr+"-"+monthStr+"-"+dateStr+" "+hourStr+":"+minuteStr+":"+secondStr;
    }

public static void main(String[] args){
System.out.println(getSystemDateTime(new Date()));
}

}
  相关解决方案