时间处理在程序开发中相当常见,本文对于时间处理做一个简单的说明。

    一、时间的表达方式

    时间在J2ME中有两种表达方式:

    1、以和GMT1970年1月1号午夜12点和现在相差的毫秒数来代表,这种方式适合比较两个时间之间的差值。
    2、以对象的形式来表达

    二、时间处理的相关类

    时间处理在J2ME中涉及三个类:
    1、System类
    long time = System. currentTimeMillis();

    使用该方法可以获得当前时间,时间的表达方式为上面提到的第一种。

    2、Date类
    Date date = new Date();
    获得当前时间,使用对象的形式来进行表达。

    3、Calendar类
    Calendar calendar = Calendar. getInstance();

    三、时间处理的具体操作

    1、以上三种表达方式的转换:

    a)将System类获得的时间转换为Date对象
    Date date = new Date(System. currentTimeMillis());

    b)将Date类型的对象转换为Calendar类型的对象
    Calendar calendar = Calendar. getInstance();
    Date date = new Date();
    calendar.setTime(date);

    2、使用Calendar完成一些日期操作

    Calendar是时间处理中最常用也是功能最强大的类,可以用它来获得某个时间的日期、星期几等信息。
    获得日期:
    Calendar calendar = Calendar. getInstance();
    ……
    int day = calendar.get(Calendar. DATE);

    获得日期、年份、星期的操作和这个类似。需要注意的是:Calendar中表示月份的数字和实际相差1,即1月用数字0表示,2月用数字1表示,……12月用数字11表示。

    (责任编辑:铭铭)
原文:J2ME中的时间处理方法
标 签:  J2ME  时间处理