Javaフォーマット出力printf例

5857 ワード

import java.util.Date;
 
/**
 *   printf  
 */
/**     
 *   java.io.PrintStream printf    C     
 * printf               ,          ,          
 */
public class Test {
 
       public static void main(String[] args) {
              /***       ***/
              // %s       ,                %s
              System.out.printf("%s", new Integer(1212));
              // %n    
              System.out.printf("%s%n", "end line");
              //          
              System.out.printf("%s = %s%n", "Name", "Zhangsan");
              // %S           
              System.out.printf("%S = %s%n", "Name", "Zhangsan");
              //        ,   %s        ,1$        ,3$   3    
              System.out.printf("%1$s = %3$s %2$s%n", "Name", "san", "Zhang");
             
              /***   boolean   ***/
              System.out.printf("true = %b; false = ", true);
              System.out.printf("%b%n", false);
 
              /***       ***/
              Integer iObj = 342;
              // %d         10    
              System.out.printf("%d; %d; %d%n", -500, 2343L, iObj);
              // %o         8    
              System.out.printf("%o; %o; %o%n", -500, 2343L, iObj);
              // %x         16    
              System.out.printf("%x; %x; %x%n", -500, 2343L, iObj);
              // %X         16    ,          
              System.out.printf("%X; %X; %X%n", -500, 2343L, iObj);
             
              /***       ***/
              Double dObj = 45.6d;
              // %e             
              System.out.printf("%e; %e; %e%n", -756.403f, 7464.232641d, dObj);
              // %E             ,                   
              System.out.printf("%E; %E; %E%n", -756.403f, 7464.232641d, dObj);
              // %f              
              System.out.printf("%f; %f; %f%n", -756.403f, 7464.232641d, dObj);
              //             
              System.out.printf("%.1f; %.3f; %f%n", -756.403f, 7464.232641d, dObj);
             
              /***       ***/
              // %t           ,%T          , %t                 
              Date date = new Date();
              long dataL = date.getTime();
              //       
              // %t   y         (2    , 99)
              // %t   m         ,%t   d         
              System.out.printf("%1$ty-%1$tm-%1$td; %2$ty-%2$tm-%2$td%n", date, dataL);
              // %t   Y         (4    ),
              // %t   B             , %t   b            
              System.out.printf("%1$tY-%1$tB-%1$td; %2$tY-%2$tb-%2$td%n", date, dataL);
             
              //           
              // %t   D    "%tm/%td/%ty"     
              System.out.printf("%1$tD%n", date);
              //%t   F   "%tY-%tm-%td"     
              System.out.printf("%1$tF%n", date);
             
              /***       ***/
              //      
              // %t   H        (24  ),%t   I        (12  ),
              // %t   M        ,%t   S        
              System.out.printf("%1$tH:%1$tM:%1$tS; %2$tI:%2$tM:%2$tS%n", date, dataL);
              // %t   L            
              System.out.printf("%1$tH:%1$tM:%1$tS %1$tL%n", date);
              // %t  p              
              System.out.printf("%1$tH:%1$tM:%1$tS %1$tL %1$tp%n", date);
             
              //           
              // %t   R   "%tH:%tM"     
              System.out.printf("%1$tR%n", date);
              // %t   T   "%tH:%tM:%tS"     
              System.out.printf("%1$tT%n", date);
              // %t   r   "%tI:%tM:%tS %Tp"     
              System.out.printf("%1$tr%n", date);
             
              /***     ***/
              // %t   A          
              System.out.printf("%1$tF %1$tA%n", date);
              // %t   a          
              System.out.printf("%1$tF %1$ta%n", date);
             
              //            
              System.out.printf("%1$tc%n", date);
       }
}
/**
 *printf   ,   "%s"                          ;
 *   "%n"    ;   "%S"             ; "%s"   "n$"  
 *         n    .   "%b"                   
 *       .
 */
/**
 *    "%d"            ;"%o"          ;"%x"       
 *   ;"%X"         ,     (A、B、C、D、E、F)    .   "%e" 
 *            ;   "%E"             ,   e  ;   
 * "%f"           , "%f"    ".n"            n .
 */
/**
 *    "%t"          ."%t"   y             ( 99)、 m
 *          , d         ;"%t"   Y             
 * ( 1999)、 B             , b            ."%t"   D
 *    "%tm/%td/%ty"       、 F   "%tY-%tm-%td"       .
 */
/**
 * "%t"   H        (24  ), I        (12  ), M      
 *  , S        , L             、 P            
 *   ."%t"   R   "%tH:%tM"       、 T   "%tH:%tM:%tS"     
 *   、 r   "%tI:%tM:%tS %Tp"       .
 */
/**
 * "%t"   A         , a           .
 */

以上のプログラムの実行結果は次のとおりです.
1212end line
Name = Zhangsan
NAME = Zhangsan
Name = Zhang san
true = true; false = false
-500; 2343; 342
37777777014; 4447; 526
fffffe0c; 927; 156
FFFFFE0C; 927; 156
-7.564030e+02; 7.464233e+03; 4.560000e+01
-7.564030E+02; 7.464233E+03; 4.560000E+01
-756.403015; 7464.232641; 45.600000
-756.4; 7464.233; 45.600000
18-01-29; 18-01-29
2018-  -29; 2018-  -29
01/29/18
2018-01-29
10:58:52; 10:58:52
10:58:52 380
10:58:52 380   
10:58
10:58:52
10:58:52   
2018-01-29    
2018-01-29    
       29 10:58:52 CST 2018