在J2ME开发中不可避免要设计到日期相关的问题,CLDC和MIDP中提供了如下类Date、Calendar和DateField给开发者。其中前两个类位于java.util包中,DateField是javax.microedition.lcdui中的一个UI组件。我们通过使用DateField组件来收集用户输入的时间相关的信息,然后使用Date和Calendar对其进行处理。 在DateField组件中有一个概念是输入模式,包括TIME、DATE_TIME和DATE。其中TIME模式只能处理小时分钟和秒相关的时间问题,而DATE则只能处理年月日相关的问题,DATE_TIME则都可以处理。当然这就看用户的需要了。DateField也是一个Item的子类,因此需要append到Form中使用。他的两个最重要的方法是setDate()和getDate()。通过后者我们可以读取用户设定的时间,方法返回Date类型的对象。 Date在J2ME中是非常简单的,很多在J2SE中的方法和字段都被省略了。得到Date后我们把它设置为Calendar对象的当前时间,借助于Calendar提供的方法我们就可以得到用户输入的时间信息了。首先我们通过静态方法getInstance()得到Calendar的一个实例。
Date date = dateField.getDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);看看Calendar的api doc我们发现他含有很多字段,其中一些是帮助我们获得时间信息的。通常我们使用get()方法,把字段类型传递给这个方法就可以得到相关的数据了。
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR);
int min = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
需要注意的一点是month是从0开始计数的,因此我们应该把它+1然后得到当前的月份。 下面是一个简单的MIDlet测试了J2ME中如何处理时间相关的问题
package com.j2medev.time;import java.util.Calendar;
import java.util.Date;import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;public class TimeMIDlet extends MIDlet implements CommandListener
{
private Display display;
private DateField dateField;
private Form mainForm;
public static final Command okCmd = new Command('OK',Command.ITEM,1); protected void startApp() throws MIDletStateChangeException
{ initMIDlet();
}
private void initMIDlet()
{
display = Display.getDisplay(this);
dateField = new DateField('Slect Date',DateField.DATE_TIME);
mainForm = new Form('Test');
mainForm.append(dateField);
mainForm.addCommand(okCmd);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
protected void pauseApp()
{
} protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
public void commandAction(Command cmd,Displayable disp)
{
if(cmd == okCmd)
{
Date date = dateField.getDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
mainForm.append(getDetailInfo(calendar));
}
}
private String getDetailInfo(Calendar calendar)
{
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR);
int min = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
return ''+year+'年'+month+'月'+day+'日'+hour+'时'+min+'分'+sec+'秒';
}}
///日期处理类//
- package util;
- /**
- * --------------------------------------------------
- * 日期转换对象
- * --------------------------------------------------
- * 主要提供日期与1970-01-01后的天数的转换和到字符串的转换
- * --------------------------------------------------
- *
- * @author iwinyeah 李永超
- * @version 1.0.0
- * */
- import java.util.Calendar;
- import java.util.Date;
- import java.util.TimeZone;
- public class DateUtil {
- private static Calendar _calendar = Calendar.getInstance(); // 用于日期计算
- private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒数
- private static int rawOffset = TimeZone.getDefault().getRawOffset();
- /**
- * 将日期转换为1970-01-01后的天数
- *
- * @param Date
- * theDate 要计算天数的日期
- * @return int 所传入日期与1970-01-01相差的天数
- */
- public static int dateToDay(Date theDate) {
- return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);
- }
- /**
- * 将1970-01-01后的天数转换为日期
- *
- * @param int
- * 要取的日期与1970-01-01相差的天数
- * @return Date theDate 与1970-01-01相差相应天数的日期
- */
- public static Date dayToDate(int day) {
- return new Date(day * MSEC_EVERYDAY);
- }
- /**
- * 取今天与1970-01-01相差的天数
- *
- * @return int 取今天与1970-01-01相差的天数
- */
- public static int toDay() {
- return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);
- }
- /**
- * 将日期转换为年月日字符串
- *
- * @param int
- * theDay 与1970-01-01相差的天数
- * @return String 对应日期年月日形式的字符串
- */
- public static String getYMD(int theDay) {
- _calendar.setTime(dayToDate(theDay));
- return _calendar.get(Calendar.YEAR) % 100 + "/"
- + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
- + (_calendar.get(Calendar.MONTH) + 1) + "/"
- + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
- + _calendar.get(Calendar.DATE);
- }
- /**
- * 将日期转换为年月字符串
- *
- * @param int
- * theDay 与1970-01-01相差的天数
- * @return String 对应日期年月形式的字符串
- */
- public static String getYM(int theDay) {
- _calendar.setTime(dayToDate(theDay));
- return _calendar.get(Calendar.YEAR) + "/"
- + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
- + (_calendar.get(Calendar.MONTH) + 1);
- }
- /**
- * 将日期转换为月日字符串
- *
- * @param int
- * theDay 与1970-01-01相差的天数
- * @return String 对应日期月日形式的字符串
- */
- public static String getMD(int theDay) {
- _calendar.setTime(dayToDate(theDay));
- return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
- + (_calendar.get(Calendar.MONTH) + 1) + "/"
- + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
- + _calendar.get(Calendar.DATE);
- }
- /**
- * 将日期转换为当月一号
- *
- * @param int
- * theDay 与1970-01-01相差的天数
- * @return int 对应日期所在月份第一天与1970-01-01相差的天数
- */
- public static int getMonthFirstDay(int theDay) {
- _calendar.setTime(dayToDate(theDay));
- _calendar.set(Calendar.DAY_OF_MONTH, 1);
- return (int) (dateToDay(_calendar.getTime()));
- }
- /**
- * 取日期所在年份
- *
- * @param int
- * theDay 与1970-01-01相差的天数
- * @return int 对应日期所在年份
- */
- public static int getYear(int theDay) {
- _calendar.setTime(dayToDate(theDay));
- return _calendar.get(Calendar.YEAR);
- }
- /**
- * 取日期所在月份
- *
- * @param int
- * theDay 与1970-01-01相差的天数
- * @return int 对应日期所在月份
- */
- public static int getMonth(int theDay) {
- _calendar.setTime(dayToDate(theDay));
- return _calendar.get(Calendar.MONTH);
- }
- /**
- * 取日期所在周次
- *
- * @param int
- * theDay 与1970-01-01相差的天数
- * @return int 对应日期所在周次
- */
- public static int getWeek(int theDay) {
- // 1971-01-03是星期日,从该日开始计算周次
- _calendar.setTime(dayToDate(theDay));
- return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L);
- }
- }