Java常用類庫--實例操作--取得當前日期
在開發中經常需要取得日期,而且每次取得日期的時候代碼都會重複,所以既然是重複的代碼就可以將其定義成一個類,以方便重複調用,但是在操作的時候有一點土鱉需要注意:如果月份是1月,則應該顯示01,但是如果是01的話,則數字肯定會忽略掉0。
1、基於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 DateDemo06{ 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()) ; } };

2、基於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 DateDemo07{ 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()) ; } };
3、總結

最後更新:2017-04-03 14:53:48