J2ME中使用calendar类获得当前时间

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class DateParser extends MIDlet{        private String[] weekDayArray={"日","一","二","三","四","五","六"};
                public void startApp(){                Calendar calendar=Calendar.getInstance();
                System.out.println(calendar.getTime());
                int year=calendar.get(Calendar.YEAR);
                int month=calendar.get(Calendar.MONTH);
                int day=calendar.get(Calendar.DATE);
                int weekDay=calendar.get((Calendar.DAY_OF_WEEK));
                int am_pm=calendar.get(Calendar.AM_PM);
                int hour=calendar.get(Calendar.HOUR_OF_DAY);
                int minute=calendar.get(Calendar.MINUTE);
                int second=calendar.get(Calendar.SECOND);
                String dateStr=year+"年"+(month+1)+"月"+day+"日"                +" 星期"+weekDayArray[weekDay-1];
                if (am_pm==Calendar.AM){                        dateStr+=" 上午";
                }                else{                        dateStr+=" 下午";
                }                dateStr+=hour+"时"+minute+"分"+second+"秒";
                Alert alert=new Alert("当前时间",dateStr,null,AlertType.ALARM);
                alert.setTimeout(Alert.FOREVER);
                Display.getDisplay(this).setCurrent(alert);
                        }                public void destroyApp(boolean force){                        }                public void pauseApp(){                        }}
posted @ 2005-11-28 17:40  林建彧  阅读(69)  评论(0编辑  收藏  举报

欢迎光临个人主站:70apps.com,方便分享代码、截图的小工具 DevShots