Java常用类库----取得当前时间

  开发中经常需要取得日期,而且每次取得日期的时候代码都会重复,所以既然是重复的代码就可以将其定义成一个类,以方便重复调用,但是在操作的时候有点要特别注意:

    如果月份是9月,则应该显示09,但如果是09的话,则数字肯定会忽略到0

 

取得系统时间

  实现一:基于Calendar类

  实现二:基于SimpleDateFormat类

 

基于Calendar类操作

 

import java.util.* ;    // 导入需要的工具包
class DateTime{        // 以后直接通过此类就可以取得日期时间
    private Calendar calendar = null ;        // 声明一个Calendar对象,取得时间
    public DateTime(){                        // 构造方法中直接实例化对象
        this.calendar = new GregorianCalendar() ;    
    }
    public String getDate(){        // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
        // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
        StringBuffer buf = new StringBuffer() ;
        buf.append(calendar.get(Calendar.YEAR)).append("-") ;    // 增加年
        buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("-") ;    // 增加月
        buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append(" ") ;    // 取得日
        buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":") ;    // 取得时
        buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append(":") ;
        buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append(".") ;
        buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ;
        return buf.toString() ;
    }
    public String getDateComplete(){        // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
        // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
        StringBuffer buf = new StringBuffer() ;
        buf.append(calendar.get(Calendar.YEAR)).append("年") ;    // 增加年
        buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("月") ;    // 增加月
        buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append("日") ;    // 取得日
        buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append("时") ;    // 取得时
        buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append("分") ;        // 取得分
        buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append("秒") ;        // 取得秒
        buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)).append("毫秒") ;     // 取得毫秒
        return buf.toString() ;
    }
    public String getTimeStamp(){        // 得到的是一个时间戳
        // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
        StringBuffer buf = new StringBuffer() ;
        buf.append(calendar.get(Calendar.YEAR)) ;    // 增加年
        buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)) ;    // 增加月
        buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)) ;    // 取得日
        buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)) ;    // 取得时
        buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)) ;        // 取得分
        buf.append(this.addZero(calendar.get(Calendar.SECOND),2));        // 取得秒
        buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ;     // 取得毫秒
        return buf.toString() ;
    }
    // 考虑到日期中存在前导0,所以在此处加上补零的方法
    private String addZero(int num,int len){
        StringBuffer s = new StringBuffer() ;
        s.append(num) ;
        while(s.length()<len){    // 如果长度不足,则继续补0
            s.insert(0,"0") ;    // 在第一个位置处补0
        }
        return s.toString() ;
    }
};
public class Test{
    public static void main(String args[]){
        DateTime dt = new DateTime() ;
        System.out.println("系统日期:"+dt.getDate()) ;
        System.out.println("中文日期:"+dt.getDateComplete()) ;
        System.out.println("时间戳:"+dt.getTimeStamp()) ;
    }
};
View Code
系统日期:2015-04-07 11:33:40.778
中文日期:2015年04月07日11时33分40秒778毫秒
时间戳:20150407113340778

  以上的程序已经取得了日期时间,中文的日期时间,时间戳,但是所有的操作都比较麻烦,因为每一个地方还需要进行补零操作,所以,在直接使用Calendar类的时候虽然可以方便的将时间取得精确到毫秒,但是在对于取得完整日期的时候却不这么好使了。

 基于SimpleDateFormat类操作

  java.util.Date已经就是一个完整的日期了,SimpleDateFormat类中存在一个方法,可以针对于Date重新格式化,那么如果现在将一个表示当前日期的Date对象通过SimpleDateFormat类指定好的模板进行相关的格式化操作的话,那么取得时间就非常的方便了。

import java.util.* ;    // 导入需要的工具包
import java.text.* ;    // 导入SimpleDateFormat所在的包
class DateTime{        // 以后直接通过此类就可以取得日期时间
    private SimpleDateFormat sdf = null ;    // 声明SimpleDateFormat对象
    public String getDate(){        // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
        this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
        return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
    }
    public String getDateComplete(){        // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
        this.sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒") ;
        return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
    }
    public String getTimeStamp(){        // 得到的是一个时间戳
        this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;
        return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
    }
};
public class Test{
    public static void main(String args[]){
        DateTime dt = new DateTime() ;
        System.out.println("系统日期:"+dt.getDate()) ;
        System.out.println("中文日期:"+dt.getDateComplete()) ;
        System.out.println("时间戳:"+dt.getTimeStamp()) ;
    }
};
系统日期:2015-04-07 14:18:38.326
中文日期:2015年04月07日14时18分38秒326毫秒
时间戳:20150407141838326

 

总结:

  通过代码可以发现,直接使用SimpleDateFormat类取得时间会比使用Calendar类更加方便,而且不用去增加补零操作,所以在开发中如果需要取得一个日期的话,则基本上都使用SimpleDateFormat类进行操作,以上的操作代码为取得日期的一个简单做法。

posted @ 2015-04-07 11:55  闲来垂钓  阅读(156)  评论(0)    收藏  举报