Java SE基礎部分——常用類庫之SimpleDateFormat(日期格式化),sesimpledateformat
取得當前日期,並按照不同日期格式化輸入。代碼如下:
1 // 20160618 SimpleDateFomat類的使用 日期格式化 練習 2 package MyPackage; //自己定義的包 3 4 import java.text.SimpleDateFormat; //匯入需要的SimpleDateFormat包 5 import java.util.Date; //匯入需要的Date包 6 7 class MyDateDemo { // 定義的MyDateDemo類 8 private SimpleDateFormat sd = null; // 聲明SimpleDateFormat對象sd 9 10 public String getDate01() { // 定義getDate01方法11 this.sd = new SimpleDateFormat("yyyy-MM-dd HH:mm;ss.sss"); // 得到一個"yyyy-MM-dd12 // HH:mm;ss.sss"格式日期13 return this.sd.format(new Date()); // 將當前日期進行格式化操作14 }15 16 public String getDate02() { // 定義getDate02方法17 this.sd = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒sss毫秒"); // 得到一個"yyyy年MM月dd日18 // HH時mm分ss秒sss毫秒"格式日期19 return this.sd.format(new Date()); // 將當前日期進行格式化操作20 }21 22 public String getDate03() {// 定義getDate03方法23 this.sd = new SimpleDateFormat("yyyyMMddHHmmsssss");// 得到一個"yyyyMMddHHmmsssss"格式日期(也就是時間戳記)24 return this.sd.format(new Date());// 將當前日期進行格式化操作25 }26 }27 28 public class SimpleDateFormatDemo {// 主類29 30 public static void main(String[] args) { // 主方法31 MyDateDemo dd = new MyDateDemo(); // 聲明dd對象,並執行個體化32 System.out.println("預設日期格式: " + new Date());// 分別調用方法輸入不同格式的日期33 System.out.println("英文日期格式: " + dd.getDate01());34 System.out.println("中文日期格式: " + dd.getDate02());35 System.out.println("時間戳記: " + dd.getDate03());36 37 }38 39 }
還有另一個日期格式化的方法,這種方法優先使用,另一種方法代碼如下:
1 import java.util.* ; // 匯入需要的工具包 2 class DateTime{ // 以後直接通過此類就可以取得日期時間 3 private Calendar calendar = null ; // 聲明一個Calendar對象,取得時間 4 public DateTime(){ // 構造方法中直接執行個體化對象 5 this.calendar = new GregorianCalendar() ; 6 } 7 public String getDate(){ // 得到的是一個日期:格式為:yyyy-MM-dd HH:mm:ss.SSS 8 // 考慮到程式要頻繁修改字串,所以使用StringBuffer提升效能 9 StringBuffer buf = new StringBuffer() ;10 buf.append(calendar.get(Calendar.YEAR)).append("-") ; // 增加年11 buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("-") ; // 增加月12 buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append(" ") ; // 取得日13 buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":") ; // 取得時14 buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append(":") ;15 buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append(".") ;16 buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ;17 return buf.toString() ;18 }19 public String getDateComplete(){ // 得到的是一個日期:格式為:yyyy年MM月dd日 HH時mm分ss秒SSS毫秒20 // 考慮到程式要頻繁修改字串,所以使用StringBuffer提升效能21 StringBuffer buf = new StringBuffer() ;22 buf.append(calendar.get(Calendar.YEAR)).append("年") ; // 增加年23 buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("月") ; // 增加月24 buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append("日") ; // 取得日25 buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append("時") ; // 取得時26 buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append("分") ; // 取得分27 buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append("秒") ; // 取得秒28 buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)).append("毫秒") ; // 取得毫秒29 return buf.toString() ;30 }31 public String getTimeStamp(){ // 得到的是一個時間戳記32 // 考慮到程式要頻繁修改字串,所以使用StringBuffer提升效能33 StringBuffer buf = new StringBuffer() ;34 buf.append(calendar.get(Calendar.YEAR)) ; // 增加年35 buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)) ; // 增加月36 buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)) ; // 取得日37 buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)) ; // 取得時38 buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)) ; // 取得分39 buf.append(this.addZero(calendar.get(Calendar.SECOND),2)); // 取得秒40 buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ; // 取得毫秒41 return buf.toString() ;42 }43 // 考慮到日期中存在前置0,所以在此處加上補零的方法44 private String addZero(int num,int len){45 StringBuffer s = new StringBuffer() ;46 s.append(num) ;47 while(s.length()<len){ // 如果長度不足,則繼續補048 s.insert(0,"0") ; // 在第一個位置處補049 }50 return s.toString() ;51 }52 };53 public class DateDemo06{54 public static void main(String args[]){55 DateTime dt = new DateTime() ;56 System.out.println("系統日期:"+dt.getDate()) ;57 System.out.println("中文日期:"+dt.getDateComplete()) ;58 System.out.println("時間戳記:"+dt.getTimeStamp()) ;59 }60 };
經常使用,就可以熟練記住和使用。