1 *java.util.Formatter* *Formatter* An interpreter for printf-style format strings
3 public final class Formatter
4 extends |java.lang.Object|
5 implements |java.io.Closeable|
8 |java.util.Formatter_Description|
9 |java.util.Formatter_Fields|
10 |java.util.Formatter_Constructors|
11 |java.util.Formatter_Methods|
13 ================================================================================
15 *java.util.Formatter_Constructors*
16 |java.util.Formatter()|Constructs a new formatter.
17 |java.util.Formatter(Appendable)|Constructs a new formatter with the specified
18 |java.util.Formatter(Appendable,Locale)|Constructs a new formatter with the spe
19 |java.util.Formatter(File)|Constructs a new formatter with the specified file.
20 |java.util.Formatter(File,String)|Constructs a new formatter with the specified
21 |java.util.Formatter(File,String,Locale)|Constructs a new formatter with the sp
22 |java.util.Formatter(Locale)|Constructs a new formatter with the specified loca
23 |java.util.Formatter(OutputStream)|Constructs a new formatter with the specifie
24 |java.util.Formatter(OutputStream,String)|Constructs a new formatter with the s
25 |java.util.Formatter(OutputStream,String,Locale)|Constructs a new formatter wit
26 |java.util.Formatter(PrintStream)|Constructs a new formatter with the specified
27 |java.util.Formatter(String)|Constructs a new formatter with the specified file
28 |java.util.Formatter(String,String)|Constructs a new formatter with the specifi
29 |java.util.Formatter(String,String,Locale)|Constructs a new formatter with the
31 *java.util.Formatter_Methods*
32 |java.util.Formatter.close()|Closes this formatter.
33 |java.util.Formatter.flush()|Flushes this formatter.
34 |java.util.Formatter.format(Locale,String,Object[])|Writes a formatted string t
35 |java.util.Formatter.format(String,Object[])|Writes a formatted string to this
36 |java.util.Formatter.ioException()|Returns the IOException last thrown by this
37 |java.util.Formatter.locale()|Returns the locale set by the construction of thi
38 |java.util.Formatter.out()|Returns the destination for the output.
39 |java.util.Formatter.toString()|Returns the result of invoking toString() on th
41 *java.util.Formatter_Description*
43 An interpreter for printf-style format strings. This class provides support for
44 layout justification and alignment, common formats for numeric, string, and
45 date/time data, and locale-specific output. Common Java types such as byte,
46 BigDecimal(|java.math.BigDecimal|) , and (|java.util.Calendar|) are supported.
47 Limited formatting customization for arbitrary user types is provided through
48 the (|java.util.Formattable|) interface.
50 Formatters are not necessarily safe for multithreaded access. Thread safety is
51 optional and is the responsibility of users of methods in this class.
53 Formatted printing for the Java language is heavily inspired by C's printf.
54 Although the format strings are similar to C, some customizations have been
55 made to accommodate the Java language and exploit some of its features. Also,
56 Java formatting is more strict than C's; for example, if a conversion is
57 incompatible with a flag, an exception will be thrown. In C inapplicable flags
58 are silently ignored. The format strings are thus intended to be recognizable
59 to C programmers but not necessarily completely compatible with those in C.
61 Examples of expected usage:
65 StringBuilder sb = new StringBuilder(); // Send all output to the Appendable
66 object sb Formatter formatter = new Formatter(sb, Locale.US);
68 // Explicit argument indices may be used to re-order output.
69 formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d") // -> " d c b
72 // Optional locale as the first argument can be used to get // locale-specific
73 formatting of numbers. The precision and width can be // given to round and
74 align the value. formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E); // ->
77 // The '(' numeric flag may be used to format negative numbers with //
78 parentheses rather than a minus sign. Group separators are // automatically
79 inserted. formatter.format("Amount gained or lost since last statement: $
80 %(,.2f", balanceDelta); // -> "Amount gained or lost since last statement: $
83 Convenience methods for common formatting requests exist as illustrated by the
84 following invocations:
88 // Writes a formatted string to System.out. System.out.format("Local time:
89 %tT", Calendar.getInstance()); // -> "Local time: 13:34:18"
91 // Writes formatted output to System.err. System.err.printf("Unable to open
92 file '%1$s': %2$s", fileName, exception.getMessage()); // -> "Unable to open
93 file 'food': No such file or directory"
95 Like C's sprintf(3), Strings may be formatted using the static method
96 String.format(|java.lang.String|) :
100 // Format a string containing a date. import java.util.Calendar; import
101 java.util.GregorianCalendar; import static java.util.Calendar.*;
103 Calendar c = new GregorianCalendar(1995, MAY, 23); String s =
104 String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c); // -> s == "Duke's
105 Birthday: May 23, 1995"
109 This specification is divided into two sections. The first section, Summary,
110 covers the basic formatting concepts. This section is intended for users who
111 want to get started quickly and are familiar with formatted printing in other
112 programming languages. The second section, Details, covers the specific
113 implementation details. It is intended for users who want more precise
114 specification of formatting behavior.
118 This section is intended to provide a brief overview of formatting concepts.
119 For precise behavioral details, refer to the Details section.
123 Every method which produces formatted output requires a format string and an
124 argument list. The format string is a (|java.lang.String|) which may contain
125 fixed text and one or more embedded format specifiers. Consider the following
130 Calendar c = ...; String s = String.format("Duke's Birthday: %1$tm
133 This format string is the first argument to the format method. It contains
134 three format specifiers "%1$tm", "%1$te", and "%1$tY" which indicate how the
135 arguments should be processed and where they should be inserted in the text.
136 The remaining portions of the format string are fixed text including "Dukes
137 Birthday: " and any other spaces or punctuation.
139 The argument list consists of all arguments passed to the method after the
140 format string. In the above example, the argument list is of size one and
141 consists of the new Calendar(|java.util.Calendar|) object.
145 The format specifiers for general, character, and numeric types have the
150 %[argument_index$][flags][width][.precision]conversion
152 The optional argument_index is a decimal integer indicating the position of the
153 argument in the argument list. The first argument is referenced by "1$", the
156 The optional flags is a set of characters that modify the output format. The
157 set of valid flags depends on the conversion.
159 The optional width is a non-negative decimal integer indicating the minimum
160 number of characters to be written to the output.
162 The optional precision is a non-negative decimal integer usually used to
163 restrict the number of characters. The specific behavior depends on the
166 The required conversion is a character indicating how the argument should be
167 formatted. The set of valid conversions for a given argument depends on the
168 argument's data type.
170 The format specifiers for types which are used to represents dates and times
171 have the following syntax:
175 %[argument_index$][flags][width]conversion
177 The optional argument_index, flags and width are defined as above.
179 The required conversion is a two character sequence. The first character is 't'
180 or 'T'. The second character indicates the format to be used. These characters
181 are similar to but not completely identical to those defined by GNU date and
184 The format specifiers which do not correspond to arguments have the following
189 %[flags][width]conversion
191 The optional flags and width is defined as above.
193 The required conversion is a character indicating content to be inserted in the
200 Conversions are divided into the following categories:
204 General - may be applied to any argument type
206 Character - may be applied to basic types which represent Unicode characters:
207 char, (|java.lang.Character|) , byte, (|java.lang.Byte|) , short, and
208 (|java.lang.Short|) . This conversion may also be applied to the types int and
209 (|java.lang.Integer|) when (|java.lang.Character|) returns true
215 Integral - may be applied to Java integral types: byte, (|java.lang.Byte|) ,
216 short, (|java.lang.Short|) , int and (|java.lang.Integer|) , long,
217 (|java.lang.Long|) , and BigInteger(|java.math.BigInteger|) Floating Point -
218 may be applied to Java floating-point types: float, (|java.lang.Float|) ,
219 double, (|java.lang.Double|) , and BigDecimal(|java.math.BigDecimal|)
221 Date/Time - may be applied to Java types which are capable of encoding a date
222 or time: long, (|java.lang.Long|) , (|java.util.Calendar|) , and
225 Percent - produces a literal '%' ('u0025')
227 Line Separator - produces the platform-specific line separator
231 The following table summarizes the supported conversions. Conversions denoted
232 by an upper-case character (i.e. 'B', 'H', 'S', 'C', 'X', 'E', 'G', 'A', and
233 'T') are the same as those for the corresponding lower-case conversion
234 characters except that the result is converted to upper case according to the
235 rules of the prevailing Locale(|java.util.Locale|) . The result is equivalent
236 to the following invocation of (|java.lang.String|)
242 Conversion Argument Category Description
244 'b', 'B' general If the argument arg is null, then the result is "false". If
245 arg is a boolean or (|java.lang.Boolean|) , then the result is the string
246 returned by String.valueOf()(|java.lang.String|) . Otherwise, the result is
249 'h', 'H' general If the argument arg is null, then the result is "null".
250 Otherwise, the result is obtained by invoking
251 Integer.toHexString(arg.hashCode()).
253 's', 'S' general If the argument arg is null, then the result is "null". If arg
254 implements (|java.util.Formattable|) , then
255 arg.formatTo(|java.util.Formattable|) is invoked. Otherwise, the result is
256 obtained by invoking arg.toString().
258 'c', 'C' character The result is a Unicode character
260 'd' integral The result is formatted as a decimal integer
262 'o' integral The result is formatted as an octal integer
264 'x', 'X' integral The result is formatted as a hexadecimal integer
266 'e', 'E' floating point The result is formatted as a decimal number in
267 computerized scientific notation
269 'f' floating point The result is formatted as a decimal number
271 'g', 'G' floating point The result is formatted using computerized scientific
272 notation or decimal format, depending on the precision and the value after
275 'a', 'A' floating point The result is formatted as a hexadecimal floating-point
276 number with a significand and an exponent
278 't', 'T' date/time Prefix for date and time conversion characters. See
279 Date/Time Conversions.
281 '%' percent The result is a literal '%' ('u0025')
283 'n' line separator The result is the platform-specific line separator
287 Any characters not explicitly defined as conversions are illegal and are
288 reserved for future extensions.
290 Date/Time Conversions
292 The following date and time conversion suffix characters are defined for the
293 't' and 'T' conversions. The types are similar to but not completely identical
294 to those defined by GNU date and POSIX strftime(3c). Additional conversion
295 types are provided to access Java-specific functionality (e.g. 'L' for
296 milliseconds within the second).
298 The following conversion characters are used for formatting times:
302 'H' Hour of the day for the 24-hour clock, formatted as two digits with a
303 leading zero as necessary i.e. 00 - 23.
305 'I' Hour for the 12-hour clock, formatted as two digits with a leading zero as
306 necessary, i.e. 01 - 12.
308 'k' Hour of the day for the 24-hour clock, i.e. 0 - 23.
310 'l' Hour for the 12-hour clock, i.e. 1 - 12.
312 'M' Minute within the hour formatted as two digits with a leading zero as
313 necessary, i.e. 00 - 59.
315 'S' Seconds within the minute, formatted as two digits with a leading zero as
316 necessary, i.e. 00 - 60 ("60" is a special value required to support leap
319 'L' Millisecond within the second formatted as three digits with leading zeros
320 as necessary, i.e. 000 - 999.
322 'N' Nanosecond within the second, formatted as nine digits with leading zeros
323 as necessary, i.e. 000000000 - 999999999.
325 'p' Locale-specific morning or afternoon(|java.text.DateFormatSymbols|) marker
326 in lower case, e.g."am" or "pm". Use of the conversion prefix 'T' forces this
327 output to upper case.
329 'z' RFC822 style numeric time zone offset from GMT, e.g. -0800.
331 'Z' A string representing the abbreviation for the time zone. The Formatter's
332 locale will supersede the locale of the argument (if any).
334 's' Seconds since the beginning of the epoch starting at 1 January 1970
335 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000.
337 'Q' Milliseconds since the beginning of the epoch starting at 1 January 1970
338 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE.
342 The following conversion characters are used for formatting dates:
346 'B' Locale-specific full month name(|java.text.DateFormatSymbols|) , e.g.
347 "January", "February".
349 'b' Locale-specific abbreviated month name(|java.text.DateFormatSymbols|) ,
354 'A' Locale-specific full name of the day of the
355 week(|java.text.DateFormatSymbols|) , e.g. "Sunday", "Monday"
357 'a' Locale-specific short name of the day of the
358 week(|java.text.DateFormatSymbols|) , e.g. "Sun", "Mon"
360 'C' Four-digit year divided by 100, formatted as two digits with leading zero
361 as necessary, i.e. 00 - 99
363 'Y' Year, formatted as at least four digits with leading zeros as necessary,
364 e.g. 0092 equals 92 CE for the Gregorian calendar.
366 'y' Last two digits of the year, formatted with leading zeros as necessary,
369 'j' Day of year, formatted as three digits with leading zeros as necessary,
370 e.g. 001 - 366 for the Gregorian calendar.
372 'm' Month, formatted as two digits with leading zeros as necessary, i.e. 01 -
375 'd' Day of month, formatted as two digits with leading zeros as necessary, i.e.
378 'e' Day of month, formatted as two digits, i.e. 1 - 31.
382 The following conversion characters are used for formatting common date/time
387 'R' Time formatted for the 24-hour clock as "%tH:%tM"
389 'T' Time formatted for the 24-hour clock as "%tH:%tM:%tS".
391 'r' Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp". The location of
392 the morning or afternoon marker ('%Tp') may be locale-dependent.
394 'D' Date formatted as "%tm/%td/%ty".
396 'F' ISO8601 complete date formatted as "%tY-%tm-%td".
398 'c' Date and time formatted as "%ta %tb %td %tT %tZ %tY", e.g. "Sun Jul 20
403 Any characters not explicitly defined as date/time conversion suffixes are
404 illegal and are reserved for future extensions.
408 The following table summarizes the supported flags. y means the flag is
409 supported for the indicated argument types.
413 Flag General Character Integral Floating Point Date/Time Description
415 '-' y y y y y The result will be left-justified.
417 '#' y1 - y3 y - The result should use a conversion-dependent alternate form
419 '+' - - y4 y - The result will always include a sign
421 '' - - y4 y - The result will include a leading space for positive values
423 '0' - - y y - The result will be zero-padded
425 ',' - - y2 y5 - The result will include locale-specific grouping
426 separators(|java.text.DecimalFormatSymbols|) '(' - - y4 y5 - The result will
427 enclose negative numbers in parentheses
431 1 Depends on the definition of (|java.util.Formattable|) .
433 2 For 'd' conversion only.
435 3 For 'o', 'x', and 'X' conversions only.
437 4 For 'd', 'o', 'x', and 'X' conversions applied to
438 BigInteger(|java.math.BigInteger|) or 'd' applied to byte, (|java.lang.Byte|) ,
439 short, (|java.lang.Short|) , int and (|java.lang.Integer|) , long, and
442 5 For 'e', 'E', 'f', 'g', and 'G' conversions only.
444 Any characters not explicitly defined as flags are illegal and are reserved for
449 The width is the minimum number of characters to be written to the output. For
450 the line separator conversion, width is not applicable; if it is provided, an
451 exception will be thrown.
455 For general argument types, the precision is the maximum number of characters
456 to be written to the output.
458 For the floating-point conversions 'e', 'E', and 'f' the precision is the
459 number of digits after the decimal separator. If the conversion is 'g' or 'G',
460 then the precision is the total number of digits in the resulting magnitude
461 after rounding. If the conversion is 'a' or 'A', then the precision must not be
464 For character, integral, and date/time argument types and the percent and line
465 separator conversions, the precision is not applicable; if a precision is
466 provided, an exception will be thrown.
470 The argument index is a decimal integer indicating the position of the argument
471 in the argument list. The first argument is referenced by "1$", the second by
474 Another way to reference arguments by position is to use the ' ('u003c') flag,
475 which causes the argument for the previous format specifier to be re-used. For
476 example, the following two statements would produce identical strings:
480 Calendar c = ...; String s1 = String.format("Duke's Birthday: %1$tm
483 String s2 = String.format("Duke's Birthday: %1$tm %
487 This section is intended to provide behavioral details for formatting,
488 including conditions and exceptions, supported data types, localization, and
489 interactions between flags, conversions, and data types. For an overview of
490 formatting concepts, refer to the Summary
492 Any characters not explicitly defined as conversions, date/time conversion
493 suffixes, or flags are illegal and are reserved for future extensions. Use of
494 such a character in a format string will cause an
495 (|java.util.UnknownFormatConversionException|) or
496 (|java.util.UnknownFormatFlagsException|) to be thrown.
498 If the format specifier contains a width or precision with an invalid value or
499 which is otherwise unsupported, then a
500 (|java.util.IllegalFormatWidthException|) or
501 (|java.util.IllegalFormatPrecisionException|) respectively will be thrown.
503 If a format specifier contains a conversion character that is not applicable to
504 the corresponding argument, then an
505 (|java.util.IllegalFormatConversionException|) will be thrown.
507 All specified exceptions may be thrown by any of the format methods of
508 Formatter as well as by any format convenience methods such as
509 String.format(|java.lang.String|) and PrintStream.printf(|java.io.PrintStream|)
512 Conversions denoted by an upper-case character (i.e. 'B', 'H', 'S', 'C', 'X',
513 'E', 'G', 'A', and 'T') are the same as those for the corresponding lower-case
514 conversion characters except that the result is converted to upper case
515 according to the rules of the prevailing Locale(|java.util.Locale|) . The
516 result is equivalent to the following invocation of (|java.lang.String|)
522 The following general conversions may be applied to any argument type:
526 'b' 'u0062' Produces either "true" or "false" as returned by
527 (|java.lang.Boolean|) .
529 If the argument is null, then the result is "false". If the argument is a
530 boolean or (|java.lang.Boolean|) , then the result is the string returned by
531 String.valueOf()(|java.lang.String|) . Otherwise, the result is "true".
533 If the '#' flag is given, then a
534 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
536 'B' 'u0042' The upper-case variant of 'b'.
538 'h' 'u0068' Produces a string representing the hash code value of the object.
540 If the argument, arg is null, then the result is "null". Otherwise, the result
541 is obtained by invoking Integer.toHexString(arg.hashCode()).
543 If the '#' flag is given, then a
544 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
546 'H' 'u0048' The upper-case variant of 'h'.
548 's' 'u0073' Produces a string.
550 If the argument is null, then the result is "null". If the argument implements
551 (|java.util.Formattable|) , then its formatTo(|java.util.Formattable|) method
552 is invoked. Otherwise, the result is obtained by invoking the argument's
555 If the '#' flag is given and the argument is not a (|java.util.Formattable|) ,
556 then a (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
558 'S' 'u0053' The upper-case variant of 's'.
562 The following flags apply to general conversions:
566 '-' 'u002d' Left justifies the output. Spaces ('u0020') will be added at the
567 end of the converted value as required to fill the minimum width of the field.
568 If the width is not provided, then a (|java.util.MissingFormatWidthException|)
569 will be thrown. If this flag is not given then the output will be
572 '#' 'u0023' Requires the output use an alternate form. The definition of the
573 form is specified by the conversion.
579 The width is the minimum number of characters to be written to the output. If
580 the length of the converted value is less than the width then the output will
581 be padded by '' (u0020') until the total number of characters equals the width.
582 The padding is on the left by default. If the '-' flag is given, then the
583 padding will be on the right. If the width is not specified then there is no
586 The precision is the maximum number of characters to be written to the output.
587 The precision is applied before the width, thus the output will be truncated to
588 precision characters even if the width is greater than the precision. If the
589 precision is not specified then there is no explicit limit on the number of
594 This conversion may be applied to char, (|java.lang.Character|) , byte,
595 (|java.lang.Byte|) , short, and (|java.lang.Short|) . This conversion may also
596 be applied to the types int and (|java.lang.Integer|) when
597 (|java.lang.Character|) returns true. If it returns false then an
598 (|java.util.IllegalFormatCodePointException|) will be thrown.
602 'c' 'u0063' Formats the argument as a Unicode character as described in Unicode
603 Character Representation. This may be more than one 16-bit char in the case
604 where the argument represents a supplementary character.
606 If the '#' flag is given, then a
607 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
609 'C' 'u0043' The upper-case variant of 'c'.
613 The '-' flag defined for General conversions applies. If the '#' flag is given,
614 then a (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
616 The width is defined as for General conversions.
618 The precision is not applicable. If the precision is specified then an
619 (|java.util.IllegalFormatPrecisionException|) will be thrown.
623 Numeric conversions are divided into the following categories:
627 Byte, Short, Integer, and Long
637 Numeric types will be formatted according to the following algorithm:
639 Number Localization Algorithm
641 After digits are obtained for the integer part, fractional part, and exponent
642 (as appropriate for the data type), the following transformation is applied:
646 Each digit character d in the string is replaced by a locale-specific digit
647 computed relative to the current locale's zero
648 digit(|java.text.DecimalFormatSymbols|) z; that is d- '0' +z.
650 If a decimal separator is present, a locale-specific decimal
651 separator(|java.text.DecimalFormatSymbols|) is substituted.
653 If the ',' ('u002c') flag is given, then the locale-specific grouping
654 separator(|java.text.DecimalFormatSymbols|) is inserted by scanning the integer
655 part of the string from least significant to most significant digits and
656 inserting a separator at intervals defined by the locale's grouping
657 size(|java.text.DecimalFormat|) .
659 If the '0' flag is given, then the locale-specific zero
660 digits(|java.text.DecimalFormatSymbols|) are inserted after the sign character,
661 if any, and before the first non-zero digit, until the length of the string is
662 equal to the requested field width.
664 If the value is negative and the '(' flag is given, then a '(' ('u0028') is
665 prepended and a ')' ('u0029') is appended.
667 If the value is negative (or floating-point negative zero) and '(' flag is not
668 given, then a '-' ('u002d') is prepended.
670 If the '+' flag is given and the value is positive or zero (or floating-point
671 positive zero), then a '+' ('u002b') will be prepended.
675 If the value is NaN or positive infinity the literal strings "NaN" or
676 "Infinity" respectively, will be output. If the value is negative infinity,
677 then the output will be "(Infinity)" if the '(' flag is given otherwise the
678 output will be "-Infinity". These values are not localized.
680 Byte, Short, Integer, and Long
682 The following conversions may be applied to byte, (|java.lang.Byte|) , short,
683 (|java.lang.Short|) , int and (|java.lang.Integer|) , long, and
688 'd' 'u0054' Formats the argument as a decimal integer. The localization
689 algorithm is applied.
691 If the '0' flag is given and the value is negative, then the zero padding will
692 occur after the sign.
694 If the '#' flag is given then a
695 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
697 'o' 'u006f' Formats the argument as an integer in base eight. No localization
700 If x is negative then the result will be an unsigned value generated by adding
701 2n to the value where n is the number of bits in the type as returned by the
702 static SIZE field in the Byte(|java.lang.Byte|) , Short(|java.lang.Short|) ,
703 Integer(|java.lang.Integer|) , or Long(|java.lang.Long|) classes as
706 If the '#' flag is given then the output will always begin with the radix
709 If the '0' flag is given then the output will be padded with leading zeros to
710 the field width following any indication of sign.
712 If '(', '+', '', or ',' flags are given then a
713 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
715 'x' 'u0078' Formats the argument as an integer in base sixteen. No localization
718 If x is negative then the result will be an unsigned value generated by adding
719 2n to the value where n is the number of bits in the type as returned by the
720 static SIZE field in the Byte(|java.lang.Byte|) , Short(|java.lang.Short|) ,
721 Integer(|java.lang.Integer|) , or Long(|java.lang.Long|) classes as
724 If the '#' flag is given then the output will always begin with the radix
727 If the '0' flag is given then the output will be padded to the field width with
728 leading zeros after the radix indicator or sign (if present).
730 If '(', '', '+', or ',' flags are given then a
731 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
733 'X' 'u0058' The upper-case variant of 'x'. The entire string representing the
734 number will be converted to upper case(|java.lang.String|) including the 'x'
735 (if any) and all hexadecimal digits 'a' - 'f' ('u0061' - 'u0066').
739 If the conversion is 'o', 'x', or 'X' and both the '#' and the '0' flags are
740 given, then result will contain the radix indicator ('0' for octal and "0x" or
741 "0X" for hexadecimal), some number of zeros (based on the width), and the
744 If the '-' flag is not given, then the space padding will occur before the
747 The following flags apply to numeric integral conversions:
751 '+' 'u002b' Requires the output to include a positive sign for all positive
752 numbers. If this flag is not given then only negative values will include a
755 If both the '+' and '' flags are given then an
756 (|java.util.IllegalFormatFlagsException|) will be thrown.
758 '' 'u0020' Requires the output to include a single extra space ('u0020') for
761 If both the '+' and '' flags are given then an
762 (|java.util.IllegalFormatFlagsException|) will be thrown.
764 '0' 'u0030' Requires the output to be padded with leading
765 zeros(|java.text.DecimalFormatSymbols|) to the minimum field width following
766 any sign or radix indicator except when converting NaN or infinity. If the
767 width is not provided, then a (|java.util.MissingFormatWidthException|) will be
770 If both the '-' and '0' flags are given then an
771 (|java.util.IllegalFormatFlagsException|) will be thrown.
773 ',' 'u002c' Requires the output to include the locale-specific group
774 separators(|java.text.DecimalFormatSymbols|) as described in the "group"
775 section of the localization algorithm.
777 '(' 'u0028' Requires the output to prepend a '(' ('u0028') and append a ')'
778 ('u0029') to negative values.
782 If no flags are given the default formatting is as follows:
786 The output is right-justified within the width
788 Negative numbers begin with a '-' ('u002d')
790 Positive numbers and zero do not include a sign or extra leading space
792 No grouping separators are included
796 The width is the minimum number of characters to be written to the output. This
797 includes any signs, digits, grouping separators, radix indicator, and
798 parentheses. If the length of the converted value is less than the width then
799 the output will be padded by spaces ('u0020') until the total number of
800 characters equals width. The padding is on the left by default. If '-' flag is
801 given then the padding will be on the right. If width is not specified then
804 The precision is not applicable. If precision is specified then an
805 (|java.util.IllegalFormatPrecisionException|) will be thrown.
809 The following conversions may be applied to (|java.math.BigInteger|) .
813 'd' 'u0054' Requires the output to be formatted as a decimal integer. The
814 localization algorithm is applied.
816 If the '#' flag is given (|java.util.FormatFlagsConversionMismatchException|)
819 'o' 'u006f' Requires the output to be formatted as an integer in base eight. No
820 localization is applied.
822 If x is negative then the result will be a signed value beginning with '-'
823 ('u002d'). Signed output is allowed for this type because unlike the primitive
824 types it is not possible to create an unsigned equivalent without assuming an
825 explicit data-type size.
827 If x is positive or zero and the '+' flag is given then the result will begin
830 If the '#' flag is given then the output will always begin with '0' prefix.
832 If the '0' flag is given then the output will be padded with leading zeros to
833 the field width following any indication of sign.
835 If the ',' flag is given then a
836 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
838 'x' 'u0078' Requires the output to be formatted as an integer in base sixteen.
839 No localization is applied.
841 If x is negative then the result will be a signed value beginning with '-'
842 ('u002d'). Signed output is allowed for this type because unlike the primitive
843 types it is not possible to create an unsigned equivalent without assuming an
844 explicit data-type size.
846 If x is positive or zero and the '+' flag is given then the result will begin
849 If the '#' flag is given then the output will always begin with the radix
852 If the '0' flag is given then the output will be padded to the field width with
853 leading zeros after the radix indicator or sign (if present).
855 If the ',' flag is given then a
856 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
858 'X' 'u0058' The upper-case variant of 'x'. The entire string representing the
859 number will be converted to upper case(|java.lang.String|) including the 'x'
860 (if any) and all hexadecimal digits 'a' - 'f' ('u0061' - 'u0066').
864 If the conversion is 'o', 'x', or 'X' and both the '#' and the '0' flags are
865 given, then result will contain the base indicator ('0' for octal and "0x" or
866 "0X" for hexadecimal), some number of zeros (based on the width), and the
869 If the '0' flag is given and the value is negative, then the zero padding will
870 occur after the sign.
872 If the '-' flag is not given, then the space padding will occur before the
875 All flags defined for Byte, Short, Integer, and Long apply. The default
876 behavior when no flags are given is the same as for Byte, Short, Integer, and
879 The specification of width is the same as defined for Byte, Short, Integer, and
882 The precision is not applicable. If precision is specified then an
883 (|java.util.IllegalFormatPrecisionException|) will be thrown.
887 The following conversions may be applied to float, (|java.lang.Float|) , double
888 and (|java.lang.Double|) .
892 'e' 'u0065' Requires the output to be formatted using computerized scientific
893 notation. The localization algorithm is applied.
895 The formatting of the magnitude m depends upon its value.
897 If m is NaN or infinite, the literal strings "NaN" or "Infinity", respectively,
898 will be output. These values are not localized.
900 If m is positive-zero or negative-zero, then the exponent will be "+00".
902 Otherwise, the result is a string that represents the sign and magnitude
903 (absolute value) of the argument. The formatting of the sign is described in
904 the localization algorithm. The formatting of the magnitude m depends upon its
907 Let n be the unique integer such that 10n <= m < 10n+1; then let a be the
908 mathematically exact quotient of m and 10n so that 1 <= a < 10. The magnitude
909 is then represented as the integer part of a, as a single decimal digit,
910 followed by the decimal separator followed by decimal digits representing the
911 fractional part of a, followed by the exponent symbol 'e' ('u0065'), followed
912 by the sign of the exponent, followed by a representation of n as a decimal
913 integer, as produced by the method (|java.lang.Long|) , and zero-padded to
914 include at least two digits.
916 The number of digits in the result for the fractional part of m or a is equal
917 to the precision. If the precision is not specified then the default value is
918 6. If the precision is less than the number of digits which would appear after
919 the decimal point in the string returned by (|java.lang.Float|) or
920 (|java.lang.Double|) respectively, then the value will be rounded using the
921 round half up algorithm(|java.math.BigDecimal|) . Otherwise, zeros may be
922 appended to reach the precision. For a canonical representation of the value,
923 use (|java.lang.Float|) or (|java.lang.Double|) as appropriate.
925 If the ',' flag is given, then an
926 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
928 'E' 'u0045' The upper-case variant of 'e'. The exponent symbol will be 'E'
931 'g' 'u0067' Requires the output to be formatted in general scientific notation
932 as described below. The localization algorithm is applied.
934 After rounding for the precision, the formatting of the resulting magnitude m
935 depends on its value.
937 If m is greater than or equal to 10-4 but less than 10precision then it is
938 represented in decimal format.
940 If m is less than 10-4 or greater than or equal to 10precision, then it is
941 represented in computerized scientific notation.
943 The total number of significant digits in m is equal to the precision. If the
944 precision is not specified, then the default value is 6. If the precision is 0,
945 then it is taken to be 1.
947 If the '#' flag is given then an
948 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
950 'G' 'u0047' The upper-case variant of 'g'.
952 'f' 'u0066' Requires the output to be formatted using decimal format. The
953 localization algorithm is applied.
955 The result is a string that represents the sign and magnitude (absolute value)
956 of the argument. The formatting of the sign is described in the localization
957 algorithm. The formatting of the magnitude m depends upon its value.
959 If m NaN or infinite, the literal strings "NaN" or "Infinity", respectively,
960 will be output. These values are not localized.
962 The magnitude is formatted as the integer part of m, with no leading zeroes,
963 followed by the decimal separator followed by one or more decimal digits
964 representing the fractional part of m.
966 The number of digits in the result for the fractional part of m or a is equal
967 to the precision. If the precision is not specified then the default value is
968 6. If the precision is less than the number of digits which would appear after
969 the decimal point in the string returned by (|java.lang.Float|) or
970 (|java.lang.Double|) respectively, then the value will be rounded using the
971 round half up algorithm(|java.math.BigDecimal|) . Otherwise, zeros may be
972 appended to reach the precision. For a canonical representation of the
973 value,use (|java.lang.Float|) or (|java.lang.Double|) as appropriate.
975 'a' 'u0061' Requires the output to be formatted in hexadecimal exponential
976 form. No localization is applied.
978 The result is a string that represents the sign and magnitude (absolute value)
981 If x is negative or a negative-zero value then the result will begin with '-'
984 If x is positive or a positive-zero value and the '+' flag is given then the
985 result will begin with '+' ('u002b').
987 The formatting of the magnitude m depends upon its value.
991 If the value is NaN or infinite, the literal strings "NaN" or "Infinity",
992 respectively, will be output.
994 If m is zero then it is represented by the string "0x0.0p0".
996 If m is a double value with a normalized representation then substrings are
997 used to represent the significand and exponent fields. The significand is
998 represented by the characters "0x1." followed by the hexadecimal representation
999 of the rest of the significand as a fraction. The exponent is represented by
1000 'p' ('u0070') followed by a decimal string of the unbiased exponent as if
1001 produced by invoking Integer.toString(|java.lang.Integer|) on the exponent
1004 If m is a double value with a subnormal representation then the significand is
1005 represented by the characters '0x0.' followed by the hexadecimal representation
1006 of the rest of the significand as a fraction. The exponent is represented by
1007 'p-1022'. Note that there must be at least one nonzero digit in a subnormal
1012 If the '(' or ',' flags are given, then a
1013 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
1015 'A' 'u0041' The upper-case variant of 'a'. The entire string representing the
1016 number will be converted to upper case including the 'x' ('u0078') and 'p'
1017 ('u0070' and all hexadecimal digits 'a' - 'f' ('u0061' - 'u0066').
1021 All flags defined for Byte, Short, Integer, and Long apply.
1023 If the '#' flag is given, then the decimal separator will always be present.
1025 If no flags are given the default formatting is as follows:
1029 The output is right-justified within the width
1031 Negative numbers begin with a '-'
1033 Positive numbers and positive zero do not include a sign or extra leading space
1035 No grouping separators are included
1037 The decimal separator will only appear if a digit follows it
1041 The width is the minimum number of characters to be written to the output. This
1042 includes any signs, digits, grouping separators, decimal separators,
1043 exponential symbol, radix indicator, parentheses, and strings representing
1044 infinity and NaN as applicable. If the length of the converted value is less
1045 than the width then the output will be padded by spaces ('u0020') until the
1046 total number of characters equals width. The padding is on the left by default.
1047 If the '-' flag is given then the padding will be on the right. If width is not
1048 specified then there is no minimum.
1050 If the conversion is 'e', 'E' or 'f', then the precision is the number of
1051 digits after the decimal separator. If the precision is not specified, then it
1054 If the conversion is 'g' or 'G', then the precision is the total number of
1055 significant digits in the resulting magnitude after rounding. If the precision
1056 is not specified, then the default value is 6. If the precision is 0, then it
1059 If the conversion is 'a' or 'A', then the precision is the number of
1060 hexadecimal digits after the decimal separator. If the precision is not
1061 provided, then all of the digits as returned by (|java.lang.Double|) will be
1066 The following conversions may be applied BigDecimal(|java.math.BigDecimal|) .
1070 'e' 'u0065' Requires the output to be formatted using computerized scientific
1071 notation. The localization algorithm is applied.
1073 The formatting of the magnitude m depends upon its value.
1075 If m is positive-zero or negative-zero, then the exponent will be "+00".
1077 Otherwise, the result is a string that represents the sign and magnitude
1078 (absolute value) of the argument. The formatting of the sign is described in
1079 the localization algorithm. The formatting of the magnitude m depends upon its
1082 Let n be the unique integer such that 10n <= m < 10n+1; then let a be the
1083 mathematically exact quotient of m and 10n so that 1 <= a < 10. The magnitude
1084 is then represented as the integer part of a, as a single decimal digit,
1085 followed by the decimal separator followed by decimal digits representing the
1086 fractional part of a, followed by the exponent symbol 'e' ('u0065'), followed
1087 by the sign of the exponent, followed by a representation of n as a decimal
1088 integer, as produced by the method (|java.lang.Long|) , and zero-padded to
1089 include at least two digits.
1091 The number of digits in the result for the fractional part of m or a is equal
1092 to the precision. If the precision is not specified then the default value is
1093 6. If the precision is less than the number of digits which would appear after
1094 the decimal point in the string returned by (|java.lang.Float|) or
1095 (|java.lang.Double|) respectively, then the value will be rounded using the
1096 round half up algorithm(|java.math.BigDecimal|) . Otherwise, zeros may be
1097 appended to reach the precision. For a canonical representation of the value,
1098 use (|java.math.BigDecimal|) .
1100 If the ',' flag is given, then an
1101 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
1103 'E' 'u0045' The upper-case variant of 'e'. The exponent symbol will be 'E'
1106 'g' 'u0067' Requires the output to be formatted in general scientific notation
1107 as described below. The localization algorithm is applied.
1109 After rounding for the precision, the formatting of the resulting magnitude m
1110 depends on its value.
1112 If m is greater than or equal to 10-4 but less than 10precision then it is
1113 represented in decimal format.
1115 If m is less than 10-4 or greater than or equal to 10precision, then it is
1116 represented in computerized scientific notation.
1118 The total number of significant digits in m is equal to the precision. If the
1119 precision is not specified, then the default value is 6. If the precision is 0,
1120 then it is taken to be 1.
1122 If the '#' flag is given then an
1123 (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
1125 'G' 'u0047' The upper-case variant of 'g'.
1127 'f' 'u0066' Requires the output to be formatted using decimal format. The
1128 localization algorithm is applied.
1130 The result is a string that represents the sign and magnitude (absolute value)
1131 of the argument. The formatting of the sign is described in the localization
1132 algorithm. The formatting of the magnitude m depends upon its value.
1134 The magnitude is formatted as the integer part of m, with no leading zeroes,
1135 followed by the decimal separator followed by one or more decimal digits
1136 representing the fractional part of m.
1138 The number of digits in the result for the fractional part of m or a is equal
1139 to the precision. If the precision is not specified then the default value is
1140 6. If the precision is less than the number of digits which would appear after
1141 the decimal point in the string returned by (|java.lang.Float|) or
1142 (|java.lang.Double|) respectively, then the value will be rounded using the
1143 round half up algorithm(|java.math.BigDecimal|) . Otherwise, zeros may be
1144 appended to reach the precision. For a canonical representation of the value,
1145 use (|java.math.BigDecimal|) .
1149 All flags defined for Byte, Short, Integer, and Long apply.
1151 If the '#' flag is given, then the decimal separator will always be present.
1153 The default behavior when no flags are given is the same as for Float and
1156 The specification of width and precision is the same as defined for Float and
1161 This conversion may be applied to long, (|java.lang.Long|) ,
1162 (|java.util.Calendar|) , and (|java.util.Date|) .
1166 't' 'u0074' Prefix for date and time conversion characters. 'T' 'u0054' The
1167 upper-case variant of 't'.
1171 The following date and time conversion character suffixes are defined for the
1172 't' and 'T' conversions. The types are similar to but not completely identical
1173 to those defined by GNU date and POSIX strftime(3c). Additional conversion
1174 types are provided to access Java-specific functionality (e.g. 'L' for
1175 milliseconds within the second).
1177 The following conversion characters are used for formatting times:
1181 'H' 'u0048' Hour of the day for the 24-hour clock, formatted as two digits with
1182 a leading zero as necessary i.e. 00 - 23. 00 corresponds to midnight.
1184 'I' 'u0049' Hour for the 12-hour clock, formatted as two digits with a leading
1185 zero as necessary, i.e. 01 - 12. 01 corresponds to one o'clock (either morning
1188 'k' 'u006b' Hour of the day for the 24-hour clock, i.e. 0 - 23. 0 corresponds
1191 'l' 'u006c' Hour for the 12-hour clock, i.e. 1 - 12. 1 corresponds to one
1192 o'clock (either morning or afternoon).
1194 'M' 'u004d' Minute within the hour formatted as two digits with a leading zero
1195 as necessary, i.e. 00 - 59.
1197 'S' 'u0053' Seconds within the minute, formatted as two digits with a leading
1198 zero as necessary, i.e. 00 - 60 ("60" is a special value required to support
1201 'L' 'u004c' Millisecond within the second formatted as three digits with
1202 leading zeros as necessary, i.e. 000 - 999.
1204 'N' 'u004e' Nanosecond within the second, formatted as nine digits with leading
1205 zeros as necessary, i.e. 000000000 - 999999999. The precision of this value is
1206 limited by the resolution of the underlying operating system or hardware.
1208 'p' 'u0070' Locale-specific morning or afternoon(|java.text.DateFormatSymbols|)
1209 marker in lower case, e.g."am" or "pm". Use of the conversion prefix 'T' forces
1210 this output to upper case. (Note that 'p' produces lower-case output. This is
1211 different from GNU date and POSIX strftime(3c) which produce upper-case
1214 'z' 'u007a' RFC822 style numeric time zone offset from GMT, e.g. -0800.
1216 'Z' 'u005a' A string representing the abbreviation for the time zone.
1218 's' 'u0073' Seconds since the beginning of the epoch starting at 1 January 1970
1219 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000.
1221 'Q' 'u004f' Milliseconds since the beginning of the epoch starting at 1 January
1222 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE. The precision of this
1223 value is limited by the resolution of the underlying operating system or
1228 The following conversion characters are used for formatting dates:
1232 'B' 'u0042' Locale-specific full month name(|java.text.DateFormatSymbols|) ,
1233 e.g. "January", "February".
1235 'b' 'u0062' Locale-specific abbreviated month
1236 name(|java.text.DateFormatSymbols|) , e.g. "Jan", "Feb".
1238 'h' 'u0068' Same as 'b'.
1240 'A' 'u0041' Locale-specific full name of the day of the
1241 week(|java.text.DateFormatSymbols|) , e.g. "Sunday", "Monday"
1243 'a' 'u0061' Locale-specific short name of the day of the
1244 week(|java.text.DateFormatSymbols|) , e.g. "Sun", "Mon"
1246 'C' 'u0043' Four-digit year divided by 100, formatted as two digits with
1247 leading zero as necessary, i.e. 00 - 99
1249 'Y' 'u0059' Year, formatted to at least four digits with leading zeros as
1250 necessary, e.g. 0092 equals 92 CE for the Gregorian calendar.
1252 'y' 'u0079' Last two digits of the year, formatted with leading zeros as
1253 necessary, i.e. 00 - 99.
1255 'j' 'u006a' Day of year, formatted as three digits with leading zeros as
1256 necessary, e.g. 001 - 366 for the Gregorian calendar. 001 corresponds to the
1257 first day of the year.
1259 'm' 'u006d' Month, formatted as two digits with leading zeros as necessary,
1260 i.e. 01 - 13, where "01" is the first month of the year and ("13" is a special
1261 value required to support lunar calendars).
1263 'd' 'u0064' Day of month, formatted as two digits with leading zeros as
1264 necessary, i.e. 01 - 31, where "01" is the first day of the month.
1266 'e' 'u0065' Day of month, formatted as two digits, i.e. 1 - 31 where "1" is the
1267 first day of the month.
1271 The following conversion characters are used for formatting common date/time
1276 'R' 'u0052' Time formatted for the 24-hour clock as "%tH:%tM"
1278 'T' 'u0054' Time formatted for the 24-hour clock as "%tH:%tM:%tS".
1280 'r' 'u0072' Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp". The
1281 location of the morning or afternoon marker ('%Tp') may be locale-dependent.
1283 'D' 'u0044' Date formatted as "%tm/%td/%ty".
1285 'F' 'u0046' ISO8601 complete date formatted as "%tY-%tm-%td".
1287 'c' 'u0063' Date and time formatted as "%ta %tb %td %tT %tZ %tY", e.g. "Sun Jul
1288 20 16:17:00 EDT 1969".
1292 The '-' flag defined for General conversions applies. If the '#' flag is given,
1293 then a (|java.util.FormatFlagsConversionMismatchException|) will be thrown.
1295 The width is the minimum number of characters to be written to the output. If
1296 the length of the converted value is less than the width then the output will
1297 be padded by spaces ('u0020') until the total number of characters equals
1298 width. The padding is on the left by default. If the '-' flag is given then the
1299 padding will be on the right. If width is not specified then there is no
1302 The precision is not applicable. If the precision is specified then an
1303 (|java.util.IllegalFormatPrecisionException|) will be thrown.
1307 The conversion does not correspond to any argument.
1311 '%' The result is a literal '%' ('u0025')
1313 The width is the minimum number of characters to be written to the output
1314 including the '%'. If the length of the converted value is less than the width
1315 then the output will be padded by spaces ('u0020') until the total number of
1316 characters equals width. The padding is on the left. If width is not specified
1317 then just the '%' is output.
1319 The '-' flag defined for General conversions applies. If any other flags are
1320 provided, then a (|java.util.FormatFlagsConversionMismatchException|) will be
1323 The precision is not applicable. If the precision is specified an
1324 (|java.util.IllegalFormatPrecisionException|) will be thrown.
1330 The conversion does not correspond to any argument.
1334 'n' the platform-specific line separator as returned by
1335 System.getProperty("line.separator")(|java.lang.System|) .
1339 Flags, width, and precision are not applicable. If any are provided an
1340 (|java.util.IllegalFormatFlagsException|) ,
1341 (|java.util.IllegalFormatWidthException|) , and
1342 (|java.util.IllegalFormatPrecisionException|) , respectively will be thrown.
1346 Format specifiers can reference arguments in three ways:
1350 Explicit indexing is used when the format specifier contains an argument index.
1351 The argument index is a decimal integer indicating the position of the argument
1352 in the argument list. The first argument is referenced by "1$", the second by
1353 "2$", etc. An argument may be referenced more than once.
1359 formatter.format("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", "a", "b", "c", "d")
1360 // -> "d c b a d c b a"
1362 Relative indexing is used when the format specifier contains a ' ('u003c') flag
1363 which causes the argument for the previous format specifier to be re-used. If
1364 there is no previous argument, then a
1365 (|java.util.MissingFormatArgumentException|) is thrown.
1369 formatter.format("%s %s %<s %<s", "a", "b", "c", "d") // -> "a b b b" // "c"
1370 and "d" are ignored because they are not referenced
1372 Ordinary indexing is used when the format specifier contains neither an
1373 argument index nor a ' flag. Each format specifier which uses ordinary indexing
1374 is assigned a sequential implicit index into argument list which is independent
1375 of the indices used by explicit or relative indexing.
1379 formatter.format("%s %s %s %s", "a", "b", "c", "d") // -> "a b c d"
1383 It is possible to have a format string which uses all forms of indexing, for
1388 formatter.format("%2$s %s %<s %s", "a", "b", "c", "d") // -> "b a a b" // "c"
1389 and "d" are ignored because they are not referenced
1391 The maximum number of arguments is limited by the maximum dimension of a Java
1392 array as defined by the Java Virtual Machine Specification. If the argument
1393 index is does not correspond to an available argument, then a
1394 (|java.util.MissingFormatArgumentException|) is thrown.
1396 If there are more arguments than format specifiers, the extra arguments are
1399 Unless otherwise specified, passing a null argument to any method or
1400 constructor in this class will cause a (|java.lang.NullPointerException|) to be
1404 *java.util.Formatter()*
1408 Constructs a new formatter.
1410 The destination of the formatted output is a (|java.lang.StringBuilder|) which
1411 may be retrieved by invoking out()(|java.util.Formatter|) and whose current
1412 content may be converted into a string by invoking
1413 toString()(|java.util.Formatter|) . The locale used is the default
1414 locale(|java.util.Locale|) for this instance of the Java virtual machine.
1417 *java.util.Formatter(Appendable)*
1419 public Formatter(java.lang.Appendable a)
1421 Constructs a new formatter with the specified destination.
1423 The locale used is the default locale(|java.util.Locale|) for this instance of
1424 the Java virtual machine.
1426 a - Destination for the formatted output. If a is null then a {@link StringBuilder}
1429 *java.util.Formatter(Appendable,Locale)*
1432 java.lang.Appendable a,
1435 Constructs a new formatter with the specified destination and locale.
1437 a - Destination for the formatted output. If a is null then a {@link StringBuilder}
1439 l - The {@linkplain java.util.Locale locale} to apply during formatting. If l is
1440 null then no localization is applied.
1442 *java.util.Formatter(File)*
1444 public Formatter(java.io.File file)
1445 throws |java.io.FileNotFoundException|
1447 Constructs a new formatter with the specified file.
1449 The charset used is the default charset(|java.nio.charset.Charset|) for this
1450 instance of the Java virtual machine.
1452 The locale used is the default locale(|java.util.Locale|) for this instance of
1453 the Java virtual machine.
1455 file - The file to use as the destination of this formatter. If the file exists then
1456 it will be truncated to zero size; otherwise, a new file will be
1457 created. The output will be written to the file and is buffered.
1459 *java.util.Formatter(File,String)*
1463 java.lang.String csn)
1464 throws |java.io.FileNotFoundException|
1465 |java.io.UnsupportedEncodingException|
1467 Constructs a new formatter with the specified file and charset.
1469 The locale used is the default locale(|java.util.Locale|) for this instance of
1470 the Java virtual machine.
1472 file - The file to use as the destination of this formatter. If the file exists then
1473 it will be truncated to zero size; otherwise, a new file will be
1474 created. The output will be written to the file and is buffered.
1475 csn - The name of a supported {@linkplain java.nio.charset.Charset charset}
1477 *java.util.Formatter(File,String,Locale)*
1481 java.lang.String csn,
1483 throws |java.io.FileNotFoundException|
1484 |java.io.UnsupportedEncodingException|
1486 Constructs a new formatter with the specified file, charset, and locale.
1488 file - The file to use as the destination of this formatter. If the file exists then
1489 it will be truncated to zero size; otherwise, a new file will be
1490 created. The output will be written to the file and is buffered.
1491 csn - The name of a supported {@linkplain java.nio.charset.Charset charset}
1492 l - The {@linkplain java.util.Locale locale} to apply during formatting. If l is
1493 null then no localization is applied.
1495 *java.util.Formatter(Locale)*
1497 public Formatter(java.util.Locale l)
1499 Constructs a new formatter with the specified locale.
1501 The destination of the formatted output is a (|java.lang.StringBuilder|) which
1502 may be retrieved by invoking out()(|java.util.Formatter|) and whose current
1503 content may be converted into a string by invoking
1504 toString()(|java.util.Formatter|) .
1506 l - The {@linkplain java.util.Locale locale} to apply during formatting. If l is
1507 null then no localization is applied.
1509 *java.util.Formatter(OutputStream)*
1511 public Formatter(java.io.OutputStream os)
1513 Constructs a new formatter with the specified output stream.
1515 The charset used is the default charset(|java.nio.charset.Charset|) for this
1516 instance of the Java virtual machine.
1518 The locale used is the default locale(|java.util.Locale|) for this instance of
1519 the Java virtual machine.
1521 os - The output stream to use as the destination of this formatter. The output will
1524 *java.util.Formatter(OutputStream,String)*
1527 java.io.OutputStream os,
1528 java.lang.String csn)
1529 throws |java.io.UnsupportedEncodingException|
1531 Constructs a new formatter with the specified output stream and charset.
1533 The locale used is the default locale(|java.util.Locale|) for this instance of
1534 the Java virtual machine.
1536 os - The output stream to use as the destination of this formatter. The output will
1538 csn - The name of a supported {@linkplain java.nio.charset.Charset charset}
1540 *java.util.Formatter(OutputStream,String,Locale)*
1543 java.io.OutputStream os,
1544 java.lang.String csn,
1546 throws |java.io.UnsupportedEncodingException|
1548 Constructs a new formatter with the specified output stream, charset, and
1551 os - The output stream to use as the destination of this formatter. The output will
1553 csn - The name of a supported {@linkplain java.nio.charset.Charset charset}
1554 l - The {@linkplain java.util.Locale locale} to apply during formatting. If l is
1555 null then no localization is applied.
1557 *java.util.Formatter(PrintStream)*
1559 public Formatter(java.io.PrintStream ps)
1561 Constructs a new formatter with the specified print stream.
1563 The locale used is the default locale(|java.util.Locale|) for this instance of
1564 the Java virtual machine.
1566 Characters are written to the given PrintStream(|java.io.PrintStream|) object
1567 and are therefore encoded using that object's charset.
1569 ps - The stream to use as the destination of this formatter.
1571 *java.util.Formatter(String)*
1573 public Formatter(java.lang.String fileName)
1574 throws |java.io.FileNotFoundException|
1576 Constructs a new formatter with the specified file name.
1578 The charset used is the default charset(|java.nio.charset.Charset|) for this
1579 instance of the Java virtual machine.
1581 The locale used is the default locale(|java.util.Locale|) for this instance of
1582 the Java virtual machine.
1584 fileName - The name of the file to use as the destination of this formatter. If the file
1585 exists then it will be truncated to zero size; otherwise, a new file
1586 will be created. The output will be written to the file and is buffered.
1588 *java.util.Formatter(String,String)*
1591 java.lang.String fileName,
1592 java.lang.String csn)
1593 throws |java.io.FileNotFoundException|
1594 |java.io.UnsupportedEncodingException|
1596 Constructs a new formatter with the specified file name and charset.
1598 The locale used is the default locale(|java.util.Locale|) for this instance of
1599 the Java virtual machine.
1601 fileName - The name of the file to use as the destination of this formatter. If the file
1602 exists then it will be truncated to zero size; otherwise, a new file
1603 will be created. The output will be written to the file and is buffered.
1604 csn - The name of a supported {@linkplain java.nio.charset.Charset charset}
1606 *java.util.Formatter(String,String,Locale)*
1609 java.lang.String fileName,
1610 java.lang.String csn,
1612 throws |java.io.FileNotFoundException|
1613 |java.io.UnsupportedEncodingException|
1615 Constructs a new formatter with the specified file name, charset, and locale.
1617 fileName - The name of the file to use as the destination of this formatter. If the file
1618 exists then it will be truncated to zero size; otherwise, a new file
1619 will be created. The output will be written to the file and is buffered.
1620 csn - The name of a supported {@linkplain java.nio.charset.Charset charset}
1621 l - The {@linkplain java.util.Locale locale} to apply during formatting. If l is
1622 null then no localization is applied.
1624 *java.util.Formatter.close()*
1628 Closes this formatter. If the destination implements the (|java.io.Closeable|)
1629 interface, its close method will be invoked.
1631 Closing a formatter allows it to release resources it may be holding (such as
1632 open files). If the formatter is already closed, then invoking this method has
1635 Attempting to invoke any methods except (|java.util.Formatter|) in this
1636 formatter after it has been closed will result in a
1637 (|java.util.FormatterClosedException|) .
1640 *java.util.Formatter.flush()*
1644 Flushes this formatter. If the destination implements the (|java.io.Flushable|)
1645 interface, its flush method will be invoked.
1647 Flushing a formatter writes any buffered output in the destination to the
1651 *java.util.Formatter.format(Locale,String,Object[])*
1653 public |java.util.Formatter| format(
1655 java.lang.String format,
1656 java.lang.Object[] args)
1658 Writes a formatted string to this object's destination using the specified
1659 locale, format string, and arguments.
1661 l - The {@linkplain java.util.Locale locale} to apply during formatting. If l is
1662 null then no localization is applied. This does not change this object's
1663 locale that was set during construction.
1664 format - A format string as described in Format string syntax
1665 args - Arguments referenced by the format specifiers in the format string. If there
1666 are more arguments than format specifiers, the extra arguments are
1667 ignored. The maximum number of arguments is limited by the maximum
1668 dimension of a Java array as defined by the Java Virtual Machine
1671 Returns: This formatter
1672 *java.util.Formatter.format(String,Object[])*
1674 public |java.util.Formatter| format(
1675 java.lang.String format,
1676 java.lang.Object[] args)
1678 Writes a formatted string to this object's destination using the specified
1679 format string and arguments. The locale used is the one defined during the
1680 construction of this formatter.
1682 format - A format string as described in Format string syntax.
1683 args - Arguments referenced by the format specifiers in the format string. If there
1684 are more arguments than format specifiers, the extra arguments are
1685 ignored. The maximum number of arguments is limited by the maximum
1686 dimension of a Java array as defined by the Java Virtual Machine
1689 Returns: This formatter
1690 *java.util.Formatter.ioException()*
1692 public |java.io.IOException| ioException()
1694 Returns the IOException last thrown by this formatter's
1695 (|java.lang.Appendable|) .
1697 If the destination's append() method never throws IOException, then this method
1698 will always return null.
1701 Returns: The last exception thrown by the Appendable or null if no such exception
1703 *java.util.Formatter.locale()*
1705 public |java.util.Locale| locale()
1707 Returns the locale set by the construction of this formatter.
1709 The format(|java.util.Formatter|) method for this object which has a locale
1710 argument does not change this value.
1713 Returns: null if no localization is applied, otherwise a locale
1714 *java.util.Formatter.out()*
1716 public |java.lang.Appendable| out()
1718 Returns the destination for the output.
1721 Returns: The destination for the output
1722 *java.util.Formatter.toString()*
1724 public |java.lang.String| toString()
1726 Returns the result of invoking toString() on the destination for the output.
1727 For example, the following code formats text into a (|java.lang.StringBuilder|)
1728 then retrieves the resultant string:
1732 Formatter f = new Formatter(); f.format("Last reboot at %tc", lastRebootDate);
1733 String s = f.toString(); // -> s == "Last reboot at Sat Jan 01 00:00:00 PST
1736 An invocation of this method behaves in exactly the same way as the invocation
1742 Depending on the specification of toString for the (|java.lang.Appendable|) ,
1743 the returned string may or may not contain the characters written to the
1744 destination. For instance, buffers typically return their contents in
1745 toString(), but streams cannot since the data is discarded.
1748 Returns: The result of invoking toString() on the destination for the output