J2ME中处理日期相关问题以及收藏一个日期处理类(忽略时间)

在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+'秒';
    }}

///日期处理类//

  1. package util;   
  2.   
  3. /**  
  4.  * --------------------------------------------------  
  5.  * 日期转换对象  
  6.  * --------------------------------------------------  
  7.  * 主要提供日期与1970-01-01后的天数的转换和到字符串的转换  
  8.  * --------------------------------------------------  
  9.  *   
  10.  * @author iwinyeah 李永超  
  11.  * @version 1.0.0  
  12.  * */  
  13.   
  14. import java.util.Calendar;   
  15. import java.util.Date;   
  16. import java.util.TimeZone;   
  17.   
  18. public class DateUtil {   
  19.     private static Calendar _calendar = Calendar.getInstance(); // 用于日期计算   
  20.   
  21.     private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒数   
  22.   
  23.     private static int rawOffset = TimeZone.getDefault().getRawOffset();   
  24.   
  25.     /**  
  26.      * 将日期转换为1970-01-01后的天数  
  27.      *   
  28.      * @param Date  
  29.      *            theDate 要计算天数的日期  
  30.      * @return int 所传入日期与1970-01-01相差的天数  
  31.      */  
  32.     public static int dateToDay(Date theDate) {   
  33.         return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);   
  34.     }   
  35.   
  36.     /**  
  37.      * 将1970-01-01后的天数转换为日期  
  38.      *   
  39.      * @param int  
  40.      *            要取的日期与1970-01-01相差的天数  
  41.      * @return Date theDate 与1970-01-01相差相应天数的日期  
  42.      */  
  43.     public static Date dayToDate(int day) {   
  44.         return new Date(day * MSEC_EVERYDAY);   
  45.     }   
  46.   
  47.     /**  
  48.      * 取今天与1970-01-01相差的天数  
  49.      *   
  50.      * @return int 取今天与1970-01-01相差的天数  
  51.      */  
  52.     public static int toDay() {   
  53.         return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);   
  54.     }   
  55.   
  56.     /**  
  57.      * 将日期转换为年月日字符串  
  58.      *   
  59.      * @param int  
  60.      *            theDay 与1970-01-01相差的天数  
  61.      * @return String 对应日期年月日形式的字符串  
  62.      */  
  63.     public static String getYMD(int theDay) {   
  64.         _calendar.setTime(dayToDate(theDay));   
  65.         return _calendar.get(Calendar.YEAR) % 100 + "/"  
  66.                 + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
  67.                 + (_calendar.get(Calendar.MONTH) + 1) + "/"  
  68.                 + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")   
  69.                 + _calendar.get(Calendar.DATE);   
  70.     }   
  71.   
  72.     /**  
  73.      * 将日期转换为年月字符串  
  74.      *   
  75.      * @param int  
  76.      *            theDay 与1970-01-01相差的天数  
  77.      * @return String 对应日期年月形式的字符串  
  78.      */  
  79.     public static String getYM(int theDay) {   
  80.         _calendar.setTime(dayToDate(theDay));   
  81.         return _calendar.get(Calendar.YEAR) + "/"  
  82.                 + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
  83.                 + (_calendar.get(Calendar.MONTH) + 1);   
  84.     }   
  85.   
  86.     /**  
  87.      * 将日期转换为月日字符串  
  88.      *   
  89.      * @param int  
  90.      *            theDay 与1970-01-01相差的天数  
  91.      * @return String 对应日期月日形式的字符串  
  92.      */  
  93.     public static String getMD(int theDay) {   
  94.         _calendar.setTime(dayToDate(theDay));   
  95.         return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
  96.                 + (_calendar.get(Calendar.MONTH) + 1) + "/"  
  97.                 + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")   
  98.                 + _calendar.get(Calendar.DATE);   
  99.     }   
  100.   
  101.     /**  
  102.      * 将日期转换为当月一号  
  103.      *   
  104.      * @param int  
  105.      *            theDay 与1970-01-01相差的天数  
  106.      * @return int 对应日期所在月份第一天与1970-01-01相差的天数  
  107.      */  
  108.     public static int getMonthFirstDay(int theDay) {   
  109.         _calendar.setTime(dayToDate(theDay));   
  110.         _calendar.set(Calendar.DAY_OF_MONTH, 1);   
  111.         return (int) (dateToDay(_calendar.getTime()));   
  112.     }   
  113.   
  114.     /**  
  115.      * 取日期所在年份  
  116.      *   
  117.      * @param int  
  118.      *            theDay 与1970-01-01相差的天数  
  119.      * @return int 对应日期所在年份  
  120.      */  
  121.     public static int getYear(int theDay) {   
  122.         _calendar.setTime(dayToDate(theDay));   
  123.         return _calendar.get(Calendar.YEAR);   
  124.     }   
  125.   
  126.     /**  
  127.      * 取日期所在月份  
  128.      *   
  129.      * @param int  
  130.      *            theDay 与1970-01-01相差的天数  
  131.      * @return int 对应日期所在月份  
  132.      */  
  133.     public static int getMonth(int theDay) {   
  134.         _calendar.setTime(dayToDate(theDay));   
  135.         return _calendar.get(Calendar.MONTH);   
  136.     }   
  137.   
  138.     /**  
  139.      * 取日期所在周次  
  140.      *   
  141.      * @param int  
  142.      *            theDay 与1970-01-01相差的天数  
  143.      * @return int 对应日期所在周次  
  144.      */  
  145.     public static int getWeek(int theDay) {   
  146.         // 1971-01-03是星期日,从该日开始计算周次   
  147.         _calendar.setTime(dayToDate(theDay));   
  148.         return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L);   
  149.     }   
  150.   
  151. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值