JAvaフォーマット時間クラス

4493 ワード


  Java    ,             SimpleDateFormat  
public class SimpleDateFormat extends DateFormat
SimpleDateFormat                        。        (date -> text)、     (text -> date)    。

SimpleDateFormat       -                  。   ,    DateFormat    getTimeInstance、 getDateInstance   getDateTimeInstance       -       。                        /       。         applyPattern          。

SimpleDateFormat       :
java.lang.Object
    |
    +----java.text.Format
            |
            +----java.text.DateFormat
                    |
                    +----java.text.SimpleDateFormat
       :
import java.text.*;
import java.util.Date;

/**
   SimpleDateFormat    :

   G      
   y  
   M  
   d  
   h          (1~12)
   H        (0~23)
   m  
   s  
   S   
   E   
   D        
   F          
   w         
   W         
   a    /        
   k        (1~24)
   K          (0~11)
   z   
*/
public class FormatDateTime {

     public static void main(String[] args) {
         SimpleDateFormat myFmt=new SimpleDateFormat("yyyy MM dd  HH mm ss ");
         SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); 
         SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//   now.toLocaleString()
         SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy MM dd  HH mm ss  E ");
         SimpleDateFormat myFmt4=new SimpleDateFormat(
                 "      D       w        W        k  z  ");
         Date now=new Date();
         System.out.println(myFmt.format(now));
         System.out.println(myFmt1.format(now));
         System.out.println(myFmt2.format(now));
         System.out.println(myFmt3.format(now));
         System.out.println(myFmt4.format(now));
         System.out.println(now.toGMTString());
         System.out.println(now.toLocaleString());
         System.out.println(now.toString());
     }    
    
}

  :
2004 12 16  17 24 27 
04/12/16 17:24
2004-12-16 17:24:27
2004 12 16  17 24 27      
      351       51        3        17  CST  
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004

    JavaBean:
public class FormatDateTime {
    
     public static String toLongDateString(Date dt){
         SimpleDateFormat myFmt=new SimpleDateFormat("yyyy MM dd  HH mm ss  E ");        
         return myFmt.format(dt);
     }
    
     public static String toShortDateString(Date dt){
         SimpleDateFormat myFmt=new SimpleDateFormat("yy MM dd  HH mm ");        
         return myFmt.format(dt);
     }    
    
     public static String toLongTimeString(Date dt){
         SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");

return myFmt.format(dt);
     }
     public static String toShortTimeString(Date dt){
         SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");        
         return myFmt.format(dt);
     }
    
     public static void main(String[] args) {

         Date now=new Date();

         System.out.println(FormatDateTime.toLongDateString(now));
         System.out.println(FormatDateTime.toShortDateString(now));
         System.out.println(FormatDateTime.toLongTimeString(now));
         System.out.println(FormatDateTime.toShortTimeString(now));
     }    
    
}
   main     :
2004 12 16  17 38 26      
04 12 16  17 38 
17 38 26 0965
04/12/16 17:38


 java                      

          java.util.Calendar   java.text.SimpleDateFormat     :

        Calendar rightNow = Calendar.getInstance();
         SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddhhmmss");
         String sysDatetime = fmt.format(rightNow.getTime());  

        new SimpleDateFormat("yyyyMMddhhmmss")             ,          ,      
          new    SimpleDateFormat("yyyy/MM/dd    hh:mm:ss    ")    。