Java/Development Class/Formatter
Содержание
- 1 Apply a mask to String
- 2 Create a table of squares and cubes
- 3 Demonstrate a field-width specifier
- 4 Demonstrate left justification
- 5 Demonstrate the %n and %% format specifiers
- 6 Demonstrate the precision modifier
- 7 Demonstrate the space format specifiers
- 8 Display 12-hour time format
- 9 Display 24-hour time format.
- 10 Display complete time and date information: using %T rather than %t.
- 11 Display date using full names.
- 12 Display hour and minute, and include AM or PM indicator.
- 13 Display several time and date formats
- 14 Display short date format.
- 15 Formatter: format(String format, Object... args)
- 16 Formatter: %g
- 17 Formatter.ioException()
- 18 Formatting time and date
- 19 Limit the number of decimal digits by specifying the precision
- 20 locale-specific formatting.
- 21 new Formatter(new OutputStream("test.fmt"))
- 22 Show positive values with a leading + and negative values within parentheses.
- 23 The Time and Date Format Suffixes
- 24 Use arguments indexes to simplify the creation of a custom time and date format
- 25 Use Formatter to left-justify strings within a table.
- 26 Use Formatter to vertically align numeric values.
- 27 Using group separators.
- 28 using the %t specifier with Formatter.
- 29 Write formatted output directly to the console and to a file.
Apply a mask to String
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
Create a table of squares and cubes
/**
*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);
}
}
}
Demonstrate a field-width specifier
/**
*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);
}
}
Demonstrate left justification
/**
*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);
}
}
Demonstrate the %n and %% format specifiers
/**
*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);
}
}
Demonstrate the precision modifier
/**
*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);
}
}
Demonstrate the space format specifiers
/**
*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);
}
}
Display 12-hour time format
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
Display 24-hour time format.
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
Display complete time and date information: using %T rather than %t.
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
*/
Display date using full names.
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
Display hour and minute, and include AM or PM indicator.
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);
}
}
Display several time and date formats
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
Display short date format.
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
Formatter: format(String format, Object... args)
/**
*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);
}
}
Formatter: %g
/**
*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);
}
}
}
Formatter.ioException()
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");
}
}
}
Formatting time and date
/**
*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);
}
}
Limit the number of decimal digits by specifying the precision
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);
}
}
locale-specific formatting.
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);
}
}
new Formatter(new OutputStream("test.fmt"))
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");
}
}
}
Show positive values with a leading + and negative values within parentheses.
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);
}
}
The Time and Date Format Suffixes
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
Use arguments indexes to simplify the creation of a custom time and date format
/**
*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);
}
}
Use Formatter to left-justify strings within a table.
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
*/
Use Formatter to vertically align numeric values.
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
*/
Using group separators.
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
*/
using the %t specifier with Formatter.
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
Write formatted output directly to the console and to a file.
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");
}
}
}