Java/Development Class/Formatter

Материал из Java эксперт
Перейти к: навигация, поиск

Apply a mask to String

   <source lang="java">

import javax.swing.text.MaskFormatter; public class Main {

 public static void main(String args[]) throws Exception {
   MaskFormatter mf = new MaskFormatter("A-AAAA-AAAA-A");
   mf.setValueContainsLiteralCharacters(false);
   System.out.println(mf.valueToString("123123123123"));
 }

} //1-2312-3123-1

 </source>
   
  
 
  



Create a table of squares and cubes

   <source lang="java">
 

/**

*Output:
  1    1    1
  2    4    8
  3    9   27
  4   16   64
  5   25  125
  6   36  216
  7   49  343
  8   64  512
  9   81  729
 10  100 1000 */

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt;
   for (int i = 1; i <= 10; i++) {
     fmt = new Formatter();
     fmt.format("%4d %4d %4d", i, i * i, i * i * i);
     System.out.println(fmt);
   }
 }

}


 </source>
   
  
 
  



Demonstrate a field-width specifier

   <source lang="java">
 

/**

*Output:

|10.123450| | 10.123450| |00010.123450|

*/

import java.util.Formatter; public class MainClass {

   public static void main(String args[]) { 
     Formatter fmt = new Formatter(); 
  
     fmt.format("|%f|%n|%12f|%n|%012f|", 
                10.12345, 10.12345, 10.12345); 
  
     System.out.println(fmt); 
  
   } 
 }



 </source>
   
  
 
  



Demonstrate left justification

   <source lang="java">
 

/**

*Output:

| 123.12| |123.12 |

*/

import java.util.Formatter; public class MainClass {

   public static void main(String args[]) { 
     Formatter fmt = new Formatter(); 
  
     // Right justify by default 
     fmt.format("|%10.2f|", 123.123); 
     System.out.println(fmt); 
  
     // Now, left justify. 
     fmt = new Formatter(); 
     fmt.format("|%-10.2f|", 123.123); 
     System.out.println(fmt); 
   } 

}


 </source>
   
  
 
  



Demonstrate the %n and %% format specifiers

   <source lang="java">
 

/**

*Output:

Copying file Transfer is 88% complete

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   fmt.format("Copying file%nTransfer is %d%% complete", 88);
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



Demonstrate the precision modifier

   <source lang="java">
 

/**

*Output:

123.1235

       1.23e+02

Formatting with */ import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   // Format 4 decimal places.
   fmt.format("%.4f", 123.1234567);
   System.out.println(fmt);
   // Format to 2 decimal places in a 16 character field.
   fmt = new Formatter();
   fmt.format("%16.2e", 123.1234567);
   System.out.println(fmt);
   // Display at most 15 characters in a string.
   fmt = new Formatter();
   fmt.format("%.15s", "Formatting with Java is now easy.");
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



Demonstrate the space format specifiers

   <source lang="java">
 

/**

*Output:

-100

100

-200

200
*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   fmt.format("% d", -100);
   System.out.println(fmt);
   fmt = new Formatter();
   fmt.format("% d", 100);
   System.out.println(fmt);
   fmt = new Formatter();
   fmt.format("% d", -200);
   System.out.println(fmt);
   fmt = new Formatter();
   fmt.format("% d", 200);
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



Display 12-hour time format

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   
   fmt.format("Time using 12-hour clock: %tr\n", cal);
   System.out.println(fmt);
 }

} //Time using 12-hour clock: 03:00:51 PM


 </source>
   
  
 
  



Display 24-hour time format.

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt.format("Time using 24-hour clock: %tT\n", cal);
   System.out.println(fmt);
 }

} //Time using 24-hour clock: 15:01:46


 </source>
   
  
 
  



Display complete time and date information: using %T rather than %t.

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt.format("Time and date in lowercase: %tc\n", cal);
   fmt.format("Time and date in uppercase: %Tc\n", cal);
   System.out.println(fmt);
 }

} /* Time and date in lowercase: Mon Mar 09 15:03:26 PDT 2009 Time and date in uppercase: MON MAR 09 15:03:26 PDT 2009

  • /


 </source>
   
  
 
  



Display date using full names.

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt.format("Long date format: ");
   fmt.format("%tA %1$tB %1$td, %1$tY\n", cal);
   System.out.println(fmt);
 }

} //Long date format: Monday March 09, 2009


 </source>
   
  
 
  



Display hour and minute, and include AM or PM indicator.

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt.format("Hour and Minute: %tl:%1$tM %1$Tp\n", cal);
   // Display the formatted times and dates.
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



Display several time and date formats

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   // Display 12-hour time format.
   fmt.format("Time using 12-hour clock: %tr\n", cal);
   System.out.println(fmt);
 }

} //Time using 12-hour clock: 03:00:51 PM


 </source>
   
  
 
  



Display short date format.

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   System.out.println(fmt.format("Short date format: %tD\n", cal));
 }

} //Short date format: 03/09/09


 </source>
   
  
 
  



Formatter: format(String format, Object... args)

   <source lang="java">
 

/**

*Output:

Formatting with Java is easy 10 98.600000

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6);
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



Formatter: %g

   <source lang="java">
 

/**

*Output:

1000.00 1000.00 100000 1000.00 100000 1.00000e+07 1000.00 100000 1.00000e+07 1.00000e+09

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   for (double i = 1000; i < 1.0e+10; i *= 100) {
     fmt.format("%g ", i);
     System.out.println(fmt);
   }
 }

}


 </source>
   
  
 
  



Formatter.ioException()

   <source lang="java">
 

import java.io.FileOutputStream; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmtCon = new Formatter(System.out);
   Formatter fmtFile;
   fmtFile = new Formatter(new FileOutputStream("test.fmt"));
   fmtCon.format("a negative number: %(.2f\n\n", -123.34);
   fmtCon.format("%8s %8s\n", "Value", "Square");
   for (int i = 1; i < 20; i++)
     fmtCon.format("%8d %8d\n", i, i * i);
   // write to the file.
   fmtFile.format("This is a negative number: %(.2f\n\n", -123.34);
   fmtFile.format("%8s %8s\n", "Value", "Square");
   for (int i = 1; i < 20; i++)
     fmtFile.format("%8d %8d\n", i, i * i);
   fmtFile.close();
   if (fmtFile.ioException() != null) {
     System.out.println("File I/O Error Occurred");
   }
 }

}


 </source>
   
  
 
  



Formatting time and date

   <source lang="java">
 

/**

*Output:

10:35:15 AM Wed Apr 26 10:35:15 PDT 2006 10:35 April Apr 04

*/

import java.util.Calendar; import java.util.Formatter; public class MainClass {

   public static void main(String args[]) { 
     Formatter fmt = new Formatter(); 
     Calendar cal = Calendar.getInstance(); 
  
     fmt.format("%tr", cal); 
     System.out.println(fmt); 
  
     fmt = new Formatter(); 
     fmt.format("%tc", cal); 
     System.out.println(fmt); 
  
     fmt = new Formatter(); 
     fmt.format("%tl:%tM", cal, cal); 
     System.out.println(fmt); 
  
     fmt = new Formatter(); 
     fmt.format("%tB %tb %tm", cal, cal, cal); 
     System.out.println(fmt); 
   } 
 }



 </source>
   
  
 
  



Limit the number of decimal digits by specifying the precision

   <source lang="java">
 

import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   fmt.format("Default precision: %f\n", 10.0 / 3.0);
   fmt.format("Two decimal digits: %.2f\n\n", 10.0 / 3.0);
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



locale-specific formatting.

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; import java.util.Locale; public class Main {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt = new Formatter();
   fmt.format("Default locale: %tc\n", cal);
   fmt.format(Locale.GERMAN, "For Locale.GERMAN: %tc\n", cal);
   fmt.format(Locale.ITALY, "For Locale.ITALY: %tc\n", cal);
   fmt.format(Locale.FRANCE, "For Locale.FRANCE: %tc\n", cal);
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



new Formatter(new OutputStream("test.fmt"))

   <source lang="java">
 

import java.io.FileOutputStream; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmtCon = new Formatter(System.out);
   Formatter fmtFile = new Formatter(new FileOutputStream("test.fmt"));
   fmtCon.format("a negative number: %(.2f\n\n", -123.34);
   fmtCon.format("%8s %8s\n", "Value", "Square");
   for (int i = 1; i < 20; i++)
     fmtCon.format("%8d %8d\n", i, i * i);
   // write to the file.
   fmtFile.format("This is a negative number: %(.2f\n\n", -123.34);
   fmtFile.format("%8s %8s\n", "Value", "Square");
   for (int i = 1; i < 20; i++)
     fmtFile.format("%8d %8d\n", i, i * i);
   fmtFile.close();
   if (fmtFile.ioException() != null) {
     System.out.println("File I/O Error Occurred");
   }
 }

}


 </source>
   
  
 
  



Show positive values with a leading + and negative values within parentheses.

   <source lang="java">
 

import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   fmt.format("Default positive and negative format: %.2f %.2f\n", 423.78,
       -505.09);
   fmt.format("With + and parentheses: %+.2f %(.2f\n", 423.78, -505.09);
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



The Time and Date Format Suffixes

   <source lang="java">
 

Suffix Replaced By a Abbreviated weekday name A Full weekday name b Abbreviated month name B Full month name c Standard date and time string formatted as day month date hh::mm:ss tzone year C First two digits of year d Day of month as a decimal (01-31) D month/day/year e Day of month as a decimal (1-31) F year-month-day h Abbreviated month name H Hour (00 to 23) I Hour (01 to 12) j Day of year as a decimal (001 to 366) k Hour (0 to 23) l Hour (1 to 12) L Millisecond (000 to 999) m Month as decimal (01 to 13) M Minute as decimal (00 to 59) N Nanosecond (000000000 to 999999999) p Locale"s equivalent of AM or PM in lowercase Q Milliseconds from 1/1/1970 r hh:mm:ss (12-hour format) R hh:mm (24-hour format) S Seconds (00 to 60) s Seconds from 1/1/1970 UTC T hh:mm:ss (24-hour format) y Year in decimal without century (00 to 99) Y Year in decimal including century (0001 to 9999) z Offset from UTC Z Time zone name


 </source>
   
  
 
  



Use arguments indexes to simplify the creation of a custom time and date format

   <source lang="java">
 

/**

*Output:
Today is day 26 of April, 2006
*/

import java.util.Calendar; import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt.format("Today is day %te of %<tB, %<tY", cal);
   System.out.println(fmt);
 }

}


 </source>
   
  
 
  



Use Formatter to left-justify strings within a table.

   <source lang="java">
 

import java.util.Formatter; public class Main {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   fmt.format("%-12s %12s\n\n", "Source", "Loss");
   fmt.format("%-12s %,12d\n", "Retail", 1232675);
   System.out.println(fmt);
 }

} /* Source Loss Retail 1,232,675

  • /


 </source>
   
  
 
  



Use Formatter to vertically align numeric values.

   <source lang="java">
 

import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   double data[] = { 12.3, 45.6, -7.89, -1.0, 1.01 };
   Formatter fmt = new Formatter();
   fmt.format("%12s %12s\n", "Value", "Cube Root");
   for (double v : data) {
     fmt.format("%12.4f %12.4f\n", v, Math.cbrt(v));
   }
   System.out.println(fmt);
 }

} /* Value Cube Root

    12.3000       2.3084
    45.6000       3.5726
    -7.8900      -1.9908
    -1.0000      -1.0000
     1.0100       1.0033
  • /


 </source>
   
  
 
  



Using group separators.

   <source lang="java">
 

import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   fmt.format("No group separators: %d\n", 123456789);
   fmt.format("With group separators: %,d\n\n", 123456789);
   System.out.println(fmt);
 }

} /*No group separators: 123456789 With group separators: 123,456,789

  • /


 </source>
   
  
 
  



using the %t specifier with Formatter.

   <source lang="java">
 

import java.util.Calendar; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   // Display 12-hour time format.
   fmt.format("Time using 12-hour clock: %tr\n", cal);
   System.out.println(fmt);
 }

} //Time using 12-hour clock: 03:00:51 PM


 </source>
   
  
 
  



Write formatted output directly to the console and to a file.

   <source lang="java">
 

import java.io.FileOutputStream; import java.util.Formatter; public class Main {

 public static void main(String[] argv) throws Exception {
   Formatter fmtCon = new Formatter(System.out);
   Formatter fmtFile;
   fmtFile = new Formatter(new FileOutputStream("test.fmt"));
   fmtCon.format("a negative number: %(.2f\n\n", -123.34);
   fmtCon.format("%8s %8s\n", "Value", "Square");
   for (int i = 1; i < 20; i++)
     fmtCon.format("%8d %8d\n", i, i * i);
   // write to the file.
   fmtFile.format("This is a negative number: %(.2f\n\n", -123.34);
   fmtFile.format("%8s %8s\n", "Value", "Square");
   for (int i = 1; i < 20; i++)
     fmtFile.format("%8d %8d\n", i, i * i);
   fmtFile.close();
   if (fmtFile.ioException() != null) {
     System.out.println("File I/O Error Occurred");
   }
 }

}


 </source>