Wu Yuxiong - born naturally develop common java class library study notes: get the current date

Import Classes in java.util *;.     // kit introducing desired 
class the DateTime {         // later can be achieved directly through those datetime 
    Private Calendar = Calendar null ;         // declare a Calendar object, acquisition time 
    public the DateTime () {                         / / constructor directly instantiate an object 
        the this .calendar = new new the GregorianCalendar ();     
    } 
    public String getDate () {         // get a date: format: the mM-dd-YYYY HH: mm: ss.SSS
         // consider to frequently modify the program to the string, the use of improved performance StringBuffer 
        StringBuffer buf = new 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 ( the this .addZero (Calendar.get (Calendar.MILLISECOND),. 3 ));
         return buf.toString (); 
    } 
    public String getDateComplete () {         // get a date: format: YYYY Year when dd day HH mM mm ss a month SSS milliseconds
         // considered to be frequently modified string to the program, the use of improved performance StringBuffer 
        StringBuffer buf = new new StringBuffer (); 
        buf.append (Calendar.get (Calendar.YEAR)). the append ( "Year");     // increase in 
        buf.append ( the this .addZero (Calendar.get (of the Calendar.MONTH) +1,2)) append ( "May");     // increase month 
        buf.append ( the 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() ;
    }
    publicGetTimestamp String () {         // get a timestamp
         // considered to be frequently modified string to the program, the use of improved performance StringBuffer 
        StringBuffer buf = new new StringBuffer (); 
        buf.append (Calendar.get (Calendar.YEAR)) ;     // increase in 
        buf.append ( the this .addZero (Calendar.get (of the Calendar.MONTH) +1,2));     // increase in May 
        buf.append ( the this .addZero (Calendar.get (Calendar.DAY_OF_MONTH), 2 ));     // get the date 
        buf.append ( the this .addZero (Calendar.get (Calendar.HOUR_OF_DAY), 2));     // when acquiring 
        buf.append ( the this.addZero (Calendar.get (Calendar.MINUTE), 2));         // acquisition points 
        buf.append ( the this .addZero (Calendar.get (Calendar.SECOND), 2));         // acquired second 
        buf.append ( the this .addZero (Calendar.get (Calendar.MILLISECOND),. 3));      // obtain millisecond 
        return buf.toString (); 
    } 
    // taking into account the presence of preamble date 0, so here plus zero padding method 
    Private String addZero ( int NUM, int len) { 
        the StringBuffer S = new new the StringBuffer (); 
        s.append (NUM); 
        the while (s.length () <len) {     // If the length is insufficient, then continue to make 0
            s.insert (0, "0");     // make a first position at 0 
        }
         return s.toString (); 
    } 
}; 
public  class DateDemo06 {
     public  static  void main (String args []) { 
        the DateTime dt = new new the DateTime (); 
        System.out.println ( "system date:" + dt.getDate ()); 
        System.out.println ( "Chinese date:" + dt.getDateComplete ()); 
        System.out.println ( " timestamp: "+ dt.getTimeStamp ()); 
    } 
};
Import Classes in java.util *;.     // import required kit 
Import the java.text *;.     // import package where SimpleDateFormat 
class the DateTime {         // later can be achieved directly through those datetime 
    Private SimpleDateFormat SDF = null ;     / / declared objects SimpleDateFormat 
    public String getDate () {         // get a date: format: the mM-dd-YYYY HH: mm: ss.SSS 
        the this .sdf = new new SimpleDateFormat ( "the mM-dd-YYYY HH: mm: ss.SSS " );
         return  the this .sdf.format ( new new a date ()); // the current date to format 
    }
    public String getDateComplete () {         // get a date: format: dd day when HH yyyy, MM month SSS mm ss a millisecond 
        the this .sdf = new new the SimpleDateFormat ( "dd day when HH yyyy, MM month mm fraction ss SSS milliseconds seconds " );
         return  the this .sdf.format ( new new a date ()); // the current date to format 
    }
     public String getTimestamp () {         // get a timestamp is 
        the this .sdf = new new the SimpleDateFormat ( "yyyyMMddHHmmssSSS" );
         return  the this .sdf.format ( new new a date ()); // the current date formatting operations 
    } 
}; 
public class DateDemo07 {
     public  static  void main (String args []) { 
        the DateTime dt = new new the DateTime (); 
        System.out.println ( "System Date:" + dt.getDate ()); 
        System.out.println ( "Chinese date : "+ dt.getDateComplete ()); 
        System.out.println ( " timestamp: "+ dt.getTimeStamp ()); 
    } 
};

 

Guess you like

Origin www.cnblogs.com/tszr/p/12152918.html