MIDletウィジェットに時間を表示
3365 ワード
j 2 meに表示される時間: 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();
}
}
}
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();
}
}
}