public final class Formatter extends Object implements Closeable, Flushable
byte
,
BigDecimal
,和
Calendar
支持。有限的格式定制任意用户类型是通过
Formattable
接口提供。
格式化也不一定安全的多线程访问。线程安全是可选的,是在这个类中的方法的用户的责任。
格式化打印为java语言是深受C的printf
。虽然字符串的格式类似于C,一些定制了适应java语言开发的一些特征。另外,java格式比C更严格;例如,如果转换是一个标志不符,将抛出一个异常。C不适用的标志被忽略。的格式字符串,因此,目的是可识别的C程序员,但不一定完全兼容的C。
预期用法的例子:
StringBuilder sb = new StringBuilder(); // Send all output to the Appendable object sb Formatter formatter = new Formatter(sb, Locale.US); // Explicit argument indices may be used to re-order output. formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d") // -> " d c b a" // Optional locale as the first argument can be used to get // locale-specific formatting of numbers. The precision and width can be // given to round and align the value. formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E); // -> "e = +2,7183" // The '(' numeric flag may be used to format negative numbers with // parentheses rather than a minus sign. Group separators are // automatically inserted. formatter.format("Amount gained or lost since last statement: $ %(,.2f", balanceDelta); // -> "Amount gained or lost since last statement: $ (6,217.58)"
常见的格式要求方便的调用方法存在以下说明:
// Writes a formatted string to System.out. System.out.format("Local time: %tT", Calendar.getInstance()); // -> "Local time: 13:34:18" // Writes formatted output to System.err. System.err.printf("Unable to open file '%1$s': %2$s", fileName, exception.getMessage()); // -> "Unable to open file 'food': No such file or directory"
像C的sprintf(3)
,字符串可以使用静态方法String.format
格式化:
// Format a string containing a date. import java.util.Calendar; import java.util.GregorianCalendar; import static java.util.Calendar.*; Calendar c = new GregorianCalendar(1995, MAY, 23); String s = String.format("Duke's Birthday: %1$tb %1$te, %1$tY", c); // -> s == "Duke's Birthday: May 23, 1995"
本规范分为两个部分。第一部分,Summary,涵盖的基本格式的概念。本节的目的是为那些想快速入门的用户,并熟悉其他编程语言中的格式化打印。第二部分,Details,涵盖的具体实现细节。它的目的是为用户谁想要更精确的规范的格式化行为。
本节旨在提供格式化概念的简要概述。精确的行为细节,请参阅Details节。
每一种方法产生格式化的输出需要一个格式字符串和参数列表。格式字符串是一个String
可能包含固定文本和一个或多个嵌入式格式说明符。考虑下面的例子:
这个格式字符串的Calendar c = ...; String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
format
方法的第一个参数。它包含三个格式说明符”
%1$tm
”、“
%1$te
”、和“
%1$tY
”这表明争论如何应该处理,他们应该在文本中插入。该格式字符串的其余部分是固定的文本包括
"Dukes Birthday: "
和其他任何空格或标点符号。参数列表由格式字符串后传递给该方法的所有参数组成。在上面的例子中,参数列表的大小和组成的
Calendar
对象
c
。
%[argument_index$][flags][width][.precision]conversion
可选的argument_index是十进制整数表示的参数在参数列表中的位置。第一个参数是引用的“1$
”,第二“2$
”,等等。
可选的旗帜是一套字符,修改输出格式。有效标志的集合取决于转换。
可选的宽度是正整数表示要写入到输出的最小字符数。
可选的精度是一个非负整数通常用于限制字符数。具体的行为取决于转换。
所需的转换是一个字符的说明应该是格式化。一个给定参数的有效转换集取决于参数的数据类型。
%[argument_index$][flags][width]conversion
可选的argument_index,旗帜和宽度如上定义。
所需的转换一二字符序列。第一个字是't'
或'T'
。第二个字符表示要使用的格式。这些字符是相似但不完全相同的定义由GNU date
和POSIX strftime(3c)
。
%[flags][width]conversion
可选的旗帜和宽度如上述定义。
所需的转换是字符指示内容要插在输出。
转换分为以下几类:
char
,Character
,byte
,Byte
,short
,和Short
。这种转换也可以应用于类型int
和Integer
当Character.isValidCodePoint(int)
返回true
byte
,Byte
,short
,Short
,int
和Integer
,long
,Long
,和BigInteger
(但不char
或Character
)float
,Float
,double
,Double
,和BigDecimal
long
,Long
,Calendar
,Date
和TemporalAccessor
'%'
('\u0025')下表总结了支持的转换。转换由一个大写字母表示(即'B'
,'H'
,'S'
,'C'
,'X'
,'E'
,'G'
,'A'
,和'T'
)是相同的那些相应的小写字符转换除了结果转换为大写根据当时的Locale
规则。结果等效于下面的调用String.toUpperCase()
touppercase()出来。
Conversion | Argument Category | 描述 |
---|---|---|
'b' , 'B' |
general | If the argument arg is null , then the result is "false ". If arg is a boolean or Boolean , then the result is the string returned by String.valueOf(arg) . Otherwise, the result is "true". |
'h' , 'H' |
general | If the argument arg is null , then the result is "null ". Otherwise, the result is obtained by invoking Integer.toHexString(arg.hashCode()) . |
's' , 'S' |
general | If the argument arg is null , then the result is "null ". If arg implements Formattable , then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString() . |
'c' , 'C' |
character | The result is a Unicode character |
'd' |
integral | The result is formatted as a decimal integer |
'o' |
integral | The result is formatted as an octal integer |
'x' , 'X' |
integral | The result is formatted as a hexadecimal integer |
'e' , 'E' |
floating point | The result is formatted as a decimal number in computerized scientific notation |
'f' |
floating point | The result is formatted as a decimal number |
'g' , 'G' |
floating point | The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding. |
'a' , 'A' |
floating point | The result is formatted as a hexadecimal floating-point number with a significand and an exponent. This conversion is not supported for the BigDecimal type despite the latter's being in the floating point argument category. |
't' , 'T' |
date/time | Prefix for date and time conversion characters. See Date/Time Conversions. |
'%' |
percent | The result is a literal '%' ('\u0025') |
'n' |
line separator | The result is the platform-specific line separator |
未显式定义转换的任何字符都是非法的,并为未来的扩展保留。
以下日期和时间转换后缀字符被定义为't'
和'T'
转换。类型相似但不完全相同的定义由GNU date
和POSIX strftime(3c)
。额外的转换类型提供访问java的特定功能(例如'L'
毫秒内第二)。
下面的转换字符用于格式化时间:
'H' |
Hour of the day for the 24-hour clock, formatted as two digits with a leading zero as necessary i.e. 00 - 23 . |
'I' |
Hour for the 12-hour clock, formatted as two digits with a leading zero as necessary, i.e. 01 - 12 . |
'k' |
Hour of the day for the 24-hour clock, i.e. 0 - 23 . |
'l' |
Hour for the 12-hour clock, i.e. 1 - 12 . |
'M' |
Minute within the hour formatted as two digits with a leading zero as necessary, i.e. 00 - 59 . |
'S' |
Seconds within the minute, formatted as two digits with a leading zero as necessary, i.e. 00 - 60 ("60 " is a special value required to support leap seconds). |
'L' |
Millisecond within the second formatted as three digits with leading zeros as necessary, i.e. 000 - 999 . |
'N' |
Nanosecond within the second, formatted as nine digits with leading zeros as necessary, i.e. 000000000 - 999999999 . |
'p' |
Locale-specific morning or afternoon marker in lower case, e.g."am " or "pm ". Use of the conversion prefix 'T' forces this output to upper case. |
'z' |
RFC 822 style numeric time zone offset from GMT, e.g. -0800 . This value will be adjusted as necessary for Daylight Saving Time. For long , Long , and Date the time zone used is the default time zone for this instance of the Java virtual machine. |
'Z' |
A string representing the abbreviation for the time zone. This value will be adjusted as necessary for Daylight Saving Time. For long , Long , and Date the time zone used is the default time zone for this instance of the Java virtual machine. The Formatter's locale will supersede the locale of the argument (if any). |
's' |
Seconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000 . |
'Q' |
Milliseconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE . |
下面的转换字符用于格式化日期:
'B' |
Locale-specific full month name, e.g. "January" , "February" . |
'b' |
Locale-specific abbreviated month name, e.g. "Jan" , "Feb" . |
'h' |
Same as 'b' . |
'A' |
Locale-specific full name of the day of the week, e.g. "Sunday" , "Monday" |
'a' |
Locale-specific short name of the day of the week, e.g. "Sun" , "Mon" |
'C' |
Four-digit year divided by 100 , formatted as two digits with leading zero as necessary, i.e. 00 - 99 |
'Y' |
Year, formatted as at least four digits with leading zeros as necessary, e.g. 0092 equals 92 CE for the Gregorian calendar. |
'y' |
Last two digits of the year, formatted with leading zeros as necessary, i.e. 00 - 99 . |
'j' |
Day of year, formatted as three digits with leading zeros as necessary, e.g. 001 - 366 for the Gregorian calendar. |
'm' |
Month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13 . |
'd' |
Day of month, formatted as two digits with leading zeros as necessary, i.e. 01 - 31 |
'e' |
Day of month, formatted as two digits, i.e. 1 - 31 . |
下面的转换字符用于格式化普通日期/时间组合。
'R' |
Time formatted for the 24-hour clock as "%tH:%tM" |
'T' |
Time formatted for the 24-hour clock as "%tH:%tM:%tS" . |
'r' |
Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp" . The location of the morning or afternoon marker ('%Tp' ) may be locale-dependent. |
'D' |
Date formatted as "%tm/%td/%ty" . |
'F' |
ISO 8601 complete date formatted as "%tY-%tm-%td" . |
'c' |
Date and time formatted as "%ta %tb %td %tT %tZ %tY" , e.g. "Sun Jul 20 16:17:00 EDT 1969" . |
任何字符不明确定义为日期/时间转换后缀是非法的,是预留给未来的扩展。
下表总结了支持的标志。Y意味着该标志被支持为表示的参数类型。
Flag | General | Character | Integral | Floating Point | Date/Time | 描述 |
---|---|---|---|---|---|---|
'-' | y | y | y | y | y | The result will be left-justified. |
'#' | y1 | - | y3 | y | - | The result should use a conversion-dependent alternate form |
'+' | - | - | y4 | y | - | The result will always include a sign |
' ' | - | - | y4 | y | - | The result will include a leading space for positive values |
'0' | - | - | y | y | - | The result will be zero-padded |
',' | - | - | y2 | y5 | - | The result will include locale-specific grouping separators |
'(' | - | - | y4 | y5 | - | The result will enclose negative numbers in parentheses |
一取决于Formattable
定义。
只有'd'
转换二。
三为'o'
,'x'
,和'X'
转换只。
四为'd'
,'o'
,'x'
,和'X'
转换应用到BigInteger
或'd'
应用于byte
,Byte
,short
,Short
,int
和Integer
,long
,和Long
。
五为'e'
,'E'
,'f'
,'g'
,和'G'
转换只。
没有明确定义为标志的任何字符都是非法的,并保留为未来的扩展。
宽度是将要写入输出的字符的最小数目。对于行分隔符转换,宽度不适用;如果提供的话,将抛出一个异常。
对于一般的参数类型,精度是将要写入输出的字符的最大数量。
对于浮点数转换'a'
,'A'
,'e'
,'E'
,和'f'
精度是小数点后的位数。如果转换'g'
或'G'
,然后精度取整后的产生的幅度总位数。
对于字符、整数和日期/时间参数类型和百分比和行分隔符转换,精度不适用;如果提供了一个精度,将抛出一个异常。
参数索引是一个十进制整数,表示在参数列表中的参数的位置。第一个参数是引用的“1$
”,第二“2$
”,等等。
另一种方式的参考位置参数是使用'<'
('\u003c')的旗帜,使论点为以前的格式说明符被重新使用。例如,下面的两个语句将产生相同的字符串:
Calendar c = ...; String s1 = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c); String s2 = String.format("Duke's Birthday: %1$tm %<te,%<tY", c);
没有明确定义的任何字符转换,日期/时间转换后缀,或标志是非法的,是留给未来的扩展。使用一个格式字符串这样的性格会造成UnknownFormatConversionException
或UnknownFormatFlagsException
被。
如果格式说明符包含宽度或精度值无效或以其他方式不支持,那么IllegalFormatWidthException
或IllegalFormatPrecisionException
分别将抛出。
如果一个格式说明符包含转换的特性,不适用于对应的参数,然后IllegalFormatConversionException
将抛出。
所有指定的异常可以被任何的Formatter
的format
方法以及任何format
便利方法如String.format
和PrintStream.printf
。
转换由一个大写字母表示(即'B'
,'H'
,'S'
,'C'
,'X'
,'E'
,'G'
,'A'
,和'T'
)是相同的那些相应的小写字符转换除了结果转换为大写根据当时的Locale
规则。结果等效于下面的调用String.toUpperCase()
touppercase()出来。
下列一般转换可应用于任何参数类型:
'b' |
'\u0062' | Produces either "true " or "false " as returned by Boolean.toString(boolean) . If the argument is If the |
'B' |
'\u0042' | The upper-case variant of 'b' . |
'h' |
'\u0068' | Produces a string representing the hash code value of the object. If the argument, arg is If the |
'H' |
'\u0048' | The upper-case variant of 'h' . |
's' |
'\u0073' | Produces a string. If the argument is If the |
'S' |
'\u0053' | The upper-case variant of 's' . |
以下flags适用于一般的转换:
'-' |
'\u002d' | Left justifies the output. Spaces ('\u0020') will be added at the end of the converted value as required to fill the minimum width of the field. If the width is not provided, then a MissingFormatWidthException will be thrown. If this flag is not given then the output will be right-justified. |
'#' |
'\u0023' | Requires the output use an alternate form. The definition of the form is specified by the conversion. |
是的width字符被写入到输出的最小数量。如果转换后的值的长度小于宽度,然后输出将被填充的' '('\u0020')直到总字符数等于宽度。在默认情况下,填充在左边。如果'-'
旗了,然后填充将右边。如果没有指定宽度,那么就没有最小值。
精度是将要写入输出的字符的最大数量。精度应用前的宽度,从而输出将被截断precision
人物即使宽度大于精度。如果没有指定精度,则字符的数量没有显式限制。
char
和
Character
。它也可以应用于类型
byte
,
Byte
,
short
,和
Short
,
int
和
Integer
当
Character.isValidCodePoint(int)
返回
true
。如果它返回一个
IllegalFormatCodePointException
false
然后将抛出。
'c' |
'\u0063' | Formats the argument as a Unicode character as described in Unicode Character Representation. This may be more than one 16-bit char in the case where the argument represents a supplementary character. If the |
'C' |
'\u0043' | The upper-case variant of 'c' . |
'-'
国旗的定义General conversions适用。如果'#'
旗了,然后FormatFlagsConversionMismatchException
将抛出。
宽度定义为General conversions。
精度不适用。如果指定精度然后IllegalFormatPrecisionException
将抛出。
数字转换分为以下几类:
数字类型将根据下面的算法格式化:
整数部分,小数部分和指数(适当的数据类型)后获得数字后,下面的变换应用:
'0'
+ Z.','
('\u002c')flag给出,然后现场具体grouping separator是通过扫描从最重要的到最重要的数字字符串的整数部分和在现场的grouping size定义间隔插入隔板插入。'0'
旗了,然后现场具体zero digits插入符号字符,之后如果任何,和之前的第一个非零数字,直到该字符串的长度等于所要求的字段宽度。'('
旗了,然后'('
('\u0028')前面和')'
('\u0029')追加。'('
旗没有给出,然后'-'
('\u002d')前面。'+'
标志和价值是正的或零(或浮点正零),然后'+'
('\u002b')将被添加。如果值是楠或正无穷大,则分别为“南”或“无限”,将输出。如果该值为负无穷大,那么输出将”(无限)“如果'('
标志了否则输出将“无限”。这些值不是局部的。
Byte, Short, Integer, and Long
下面的转换可以应用于byte
,Byte
,short
,Short
,int
和Integer
,long
,和Long
。
'd' |
'\u0064' | Formats the argument as a decimal integer. The localization algorithm is applied. If the If the |
'o' |
'\u006f' | Formats the argument as an integer in base eight. No localization is applied. If x is negative then the result will be an unsigned value generated by adding 2n to the value where If the If the If |
'x' |
'\u0078' | Formats the argument as an integer in base sixteen. No localization is applied. If x is negative then the result will be an unsigned value generated by adding 2n to the value where If the If the If |
'X' |
'\u0058' | The upper-case variant of 'x' . The entire string representing the number will be converted to upper case including the 'x' (if any) and all hexadecimal digits 'a' - 'f' ('\u0061' - '\u0066'). |
如果转换'o'
,'x'
,或'X'
两'#'
和'0'
旗帜是给定的,那么结果将包含基数指标('0'
为八进制或十六进制和"0x"
"0X"
),一些零(基于宽度),和价值。
如果不给予'-'
标志,那么空间填充会发生前的征兆。
以下flags适用于数值积分转换:
'+' |
'\u002b' | Requires the output to include a positive sign for all positive numbers. If this flag is not given then only negative values will include a sign. If both the |
' ' | '\u0020' | Requires the output to include a single extra space ('\u0020') for non-negative values. If both the |
'0' |
'\u0030' | Requires the output to be padded with leading zeros to the minimum field width following any sign or radix indicator except when converting NaN or infinity. If the width is not provided, then a MissingFormatWidthException will be thrown. If both the |
',' |
'\u002c' | Requires the output to include the locale-specific group separators as described in the "group" section of the localization algorithm. |
'(' |
'\u0028' | Requires the output to prepend a '(' ('\u0028') and append a ')' ('\u0029') to negative values. |
如果没有flags给出的默认格式如下:
width
'-'
('\u002d')是的width字符被写入到输出的最小数量。这包括任何符号、数字、分组分隔符、基数指示器和圆括号。如果转换后的值的长度小于宽度,然后输出将被填充的空间('\u0020')直到总字符数等于宽度。在默认情况下,填充在左边。如果'-'
旗再给出填充将右边。如果没有指定宽度,那么就没有最小值。
精度不适用。如果指定精度然后IllegalFormatPrecisionException
将抛出。
下面的转换可以应用于BigInteger
。
'd' |
'\u0064' | Requires the output to be formatted as a decimal integer. The localization algorithm is applied. If the |
'o' |
'\u006f' | Requires the output to be formatted as an integer in base eight. No localization is applied. If x is negative then the result will be a signed value beginning with If x is positive or zero and the If the If the If the |
'x' |
'\u0078' | Requires the output to be formatted as an integer in base sixteen. No localization is applied. If x is negative then the result will be a signed value beginning with If x is positive or zero and the If the If the If the |
'X' |
'\u0058' | The upper-case variant of 'x' . The entire string representing the number will be converted to upper case including the 'x' (if any) and all hexadecimal digits 'a' - 'f' ('\u0061' - '\u0066'). |
如果转换'o'
,'x'
,或'X'
两'#'
和'0'
旗帜是给定的,那么结果将包含基础指标('0'
为十六进制八进制和"0x"
或"0X"
),一些零(基于宽度),和价值。
如果'0'
旗给出值为负,则补零之后会发生的迹象。
如果不给予'-'
标志,那么空间填充会发生前的征兆。
字节,短整数定义的所有flags,长期应用。当没有给出的default behavior旗帜为字节,短整数一样,长。
对width规格为字节,短整数定义一样,长。
精度不适用。如果指定精度然后IllegalFormatPrecisionException
将抛出。
下面的转换可以应用于float
,Float
,double
和Double
。
'e' |
'\u0065' | Requires the output to be formatted using computerized scientific notation. The localization algorithm is applied. The formatting of the magnitude m depends upon its value. If m is NaN or infinite, the literal strings "NaN" or "Infinity", respectively, will be output. These values are not localized. If m is positive-zero or negative-zero, then the exponent will be Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. The formatting of the sign is described in the localization algorithm. The formatting of the magnitude m depends upon its value. Let n be the unique integer such that 10n <= m < 10n+1; then let a be the mathematically exact quotient of m and 10n so that 1 <= a < 10. The magnitude is then represented as the integer part of a, as a single decimal digit, followed by the decimal separator followed by decimal digits representing the fractional part of a, followed by the exponent symbol The number of digits in the result for the fractional part of m or a is equal to the precision. If the precision is not specified then the default value is If the |
'E' |
'\u0045' | The upper-case variant of 'e' . The exponent symbol will be 'E' ('\u0045'). |
'g' |
'\u0067' | Requires the output to be formatted in general scientific notation as described below. The localization algorithm is applied. After rounding for the precision, the formatting of the resulting magnitude m depends on its value. If m is greater than or equal to 10-4 but less than 10precision then it is represented in decimal format. If m is less than 10-4 or greater than or equal to 10precision, then it is represented in computerized scientific notation. The total number of significant digits in m is equal to the precision. If the precision is not specified, then the default value is If the |
'G' |
'\u0047' | The upper-case variant of 'g' . |
'f' |
'\u0066' | Requires the output to be formatted using decimal format. The localization algorithm is applied. The result is a string that represents the sign and magnitude (absolute value) of the argument. The formatting of the sign is described in the localization algorithm. The formatting of the magnitude m depends upon its value. If m NaN or infinite, the literal strings "NaN" or "Infinity", respectively, will be output. These values are not localized. The magnitude is formatted as the integer part of m, with no leading zeroes, followed by the decimal separator followed by one or more decimal digits representing the fractional part of m. The number of digits in the result for the fractional part of m or a is equal to the precision. If the precision is not specified then the default value is |
'a' |
'\u0061' | Requires the output to be formatted in hexadecimal exponential form. No localization is applied. The result is a string that represents the sign and magnitude (absolute value) of the argument x. If x is negative or a negative-zero value then the result will begin with If x is positive or a positive-zero value and the The formatting of the magnitude m depends upon its value.
If the |
'A' |
'\u0041' | The upper-case variant of 'a' . The entire string representing the number will be converted to upper case including the 'x' ('\u0078') and 'p' ('\u0070' and all hexadecimal digits 'a' - 'f' ('\u0061' - '\u0066'). |
字节,短整数定义的所有flags,长期应用。
如果'#'
旗是给定的,那么小数点分隔符将永远存在。
如果没有flags给出的默认格式如下:
z-226af78f-1540—4f2f-af6b-123675d71d7c是的width字符被写入到输出的最小数量。这包括任何符号,数字,分组分隔符,十进制分隔符,指数符号,基数指示器,括号,和代表无穷和南的字符串,适用。如果转换后的值的长度小于宽度,然后输出将被填充的空间('\u0020')直到总字符数等于宽度。在默认情况下,填充在左边。如果'-'
旗再给出填充将右边。如果没有指定宽度,那么就没有最小值。
如果conversion是'e'
,'E'
或'f'
,然后精度是小数点后的位数。如果未指定精度,则被认为是6
。
如果转换'g'
或'G'
,然后精度取整后的幅度显著的总位数。如果未指定精度,则默认值为6
。如果精度是0
,然后被1
。
如果转换'a'
或'A'
,然后精度是小数点后的数进制数字。如果没有提供精度,然后所有的数字Double.toHexString(double)
将输出返回的。
下面的转换可以应用BigDecimal
。
'e' |
'\u0065' | Requires the output to be formatted using computerized scientific notation. The localization algorithm is applied. The formatting of the magnitude m depends upon its value. If m is positive-zero or negative-zero, then the exponent will be Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. The formatting of the sign is described in the localization algorithm. The formatting of the magnitude m depends upon its value. Let n be the unique integer such that 10n <= m < 10n+1; then let a be the mathematically exact quotient of m and 10n so that 1 <= a < 10. The magnitude is then represented as the integer part of a, as a single decimal digit, followed by the decimal separator followed by decimal digits representing the fractional part of a, followed by the exponent symbol The number of digits in the result for the fractional part of m or a is equal to the precision. If the precision is not specified then the default value is If the |
'E' |
'\u0045' | The upper-case variant of 'e' . The exponent symbol will be 'E' ('\u0045'). |
'g' |
'\u0067' | Requires the output to be formatted in general scientific notation as described below. The localization algorithm is applied. After rounding for the precision, the formatting of the resulting magnitude m depends on its value. If m is greater than or equal to 10-4 but less than 10precision then it is represented in decimal format. If m is less than 10-4 or greater than or equal to 10precision, then it is represented in computerized scientific notation. The total number of significant digits in m is equal to the precision. If the precision is not specified, then the default value is If the |
'G' |
'\u0047' | The upper-case variant of 'g' . |
'f' |
'\u0066' | Requires the output to be formatted using decimal format. The localization algorithm is applied. The result is a string that represents the sign and magnitude (absolute value) of the argument. The formatting of the sign is described in the localization algorithm. The formatting of the magnitude m depends upon its value. The magnitude is formatted as the integer part of m, with no leading zeroes, followed by the decimal separator followed by one or more decimal digits representing the fractional part of m. The number of digits in the result for the fractional part of m or a is equal to the precision. If the precision is not specified then the default value is |
字节,短整数定义的所有flags,长期应用。
如果'#'
旗是给定的,那么小数点分隔符将永远存在。
当没有给出的default behavior旗帜为float和double一样。
对width和precision规格为float和double定义相同。
这种转换可以应用于long
,Long
,Calendar
,Date
和TemporalAccessor
't' |
'\u0074' | Prefix for date and time conversion characters. |
'T' |
'\u0054' | The upper-case variant of 't' . |
以下日期和时间转换字符后缀定义为't'
和'T'
转换。类型相似但不完全相同的定义由GNU date
和POSIX strftime(3c)
。额外的转换类型提供访问java的特定功能(例如'L'
毫秒内第二)。
下面的转换字符用于格式化时间:
'H' |
'\u0048' | Hour of the day for the 24-hour clock, formatted as two digits with a leading zero as necessary i.e. 00 - 23 . 00 corresponds to midnight. |
'I' |
'\u0049' | Hour for the 12-hour clock, formatted as two digits with a leading zero as necessary, i.e. 01 - 12 . 01 corresponds to one o'clock (either morning or afternoon). |
'k' |
'\u006b' | Hour of the day for the 24-hour clock, i.e. 0 - 23 . 0 corresponds to midnight. |
'l' |
'\u006c' | Hour for the 12-hour clock, i.e. 1 - 12 . 1 corresponds to one o'clock (either morning or afternoon). |
'M' |
'\u004d' | Minute within the hour formatted as two digits with a leading zero as necessary, i.e. 00 - 59 . |
'S' |
'\u0053' | Seconds within the minute, formatted as two digits with a leading zero as necessary, i.e. 00 - 60 ("60 " is a special value required to support leap seconds). |
'L' |
'\u004c' | Millisecond within the second formatted as three digits with leading zeros as necessary, i.e. 000 - 999 . |
'N' |
'\u004e' | Nanosecond within the second, formatted as nine digits with leading zeros as necessary, i.e. 000000000 - 999999999 . The precision of this value is limited by the resolution of the underlying operating system or hardware. |
'p' |
'\u0070' | Locale-specific morning or afternoon marker in lower case, e.g."am " or "pm ". Use of the conversion prefix 'T' forces this output to upper case. (Note that 'p' produces lower-case output. This is different from GNU date and POSIX strftime(3c) which produce upper-case output.) |
'z' |
'\u007a' | RFC 822 style numeric time zone offset from GMT, e.g. -0800 . This value will be adjusted as necessary for Daylight Saving Time. For long , Long , and Date the time zone used is the default time zone for this instance of the Java virtual machine. |
'Z' |
'\u005a' | A string representing the abbreviation for the time zone. This value will be adjusted as necessary for Daylight Saving Time. For long , Long , and Date the time zone used is the default time zone for this instance of the Java virtual machine. The Formatter's locale will supersede the locale of the argument (if any). |
's' |
'\u0073' | Seconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000 . |
'Q' |
'\u004f' | Milliseconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE . The precision of this value is limited by the resolution of the underlying operating system or hardware. |
下面的转换字符用于格式化日期:
'B' |
'\u0042' | Locale-specific full month name, e.g. "January" , "February" . |
'b' |
'\u0062' | Locale-specific abbreviated month name, e.g. "Jan" , "Feb" . |
'h' |
'\u0068' | Same as 'b' . |
'A' |
'\u0041' | Locale-specific full name of the day of the week, e.g. "Sunday" , "Monday" |
'a' |
'\u0061' | Locale-specific short name of the day of the week, e.g. "Sun" , "Mon" |
'C' |
'\u0043' | Four-digit year divided by 100 , formatted as two digits with leading zero as necessary, i.e. 00 - 99 |
'Y' |
'\u0059' | Year, formatted to at least four digits with leading zeros as necessary, e.g. 0092 equals 92 CE for the Gregorian calendar. |
'y' |
'\u0079' | Last two digits of the year, formatted with leading zeros as necessary, i.e. 00 - 99 . |
'j' |
'\u006a' | Day of year, formatted as three digits with leading zeros as necessary, e.g. 001 - 366 for the Gregorian calendar. 001 corresponds to the first day of the year. |
'm' |
'\u006d' | Month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13 , where "01 " is the first month of the year and ("13 " is a special value required to support lunar calendars). |
'd' |
'\u0064' | Day of month, formatted as two digits with leading zeros as necessary, i.e. 01 - 31 , where "01 " is the first day of the month. |
'e' |
'\u0065' | Day of month, formatted as two digits, i.e. 1 - 31 where "1 " is the first day of the month. |
下面的转换字符用于格式化普通日期/时间组合。
'R' |
'\u0052' | Time formatted for the 24-hour clock as "%tH:%tM" |
'T' |
'\u0054' | Time formatted for the 24-hour clock as "%tH:%tM:%tS" . |
'r' |
'\u0072' | Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp" . The location of the morning or afternoon marker ('%Tp' ) may be locale-dependent. |
'D' |
'\u0044' | Date formatted as "%tm/%td/%ty" . |
'F' |
'\u0046' | ISO 8601 complete date formatted as "%tY-%tm-%td" . |
'c' |
'\u0063' | Date and time formatted as "%ta %tb %td %tT %tZ %tY" , e.g. "Sun Jul 20 16:17:00 EDT 1969" . |
'-'
国旗的定义General conversions适用。如果'#'
旗了,然后FormatFlagsConversionMismatchException
将抛出。
宽度是将要写入输出的字符的最小数目。如果转换后的值的长度小于width
然后输出将被填充的空间('\u0020')直到总字符数等于宽度。在默认情况下,填充在左边。如果'-'
旗再给出填充将右边。如果没有指定宽度,那么就没有最小值。
精度不适用。如果指定精度然后IllegalFormatPrecisionException
将抛出。
转换不对应于任何参数。
'%' |
The result is a literal '%' ('\u0025') The width is the minimum number of characters to be written to the output including the The The precision is not applicable. If the precision is specified an |
转换不对应于任何参数。
'n' |
the platform-specific line separator as returned by System.getProperty("line.separator") . |
标志,宽度和精度不适用。是否有提供了一个IllegalFormatFlagsException
,IllegalFormatWidthException
,和IllegalFormatPrecisionException
,分别将抛出。
格式说明符可以通过三种方式引用参数:
1$
”,第二“2$
”等论点可以被引用超过一次。例如:
formatter.format("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", "a", "b", "c", "d") // -> "d c b a d c b a"
'<'
('\u003c')旗使争论以前的格式说明符被重新使用。如果没有以前的说法,然后MissingFormatArgumentException
抛出。
formatter.format("%s %s %<s %<s", "a", "b", "c", "d") // -> "a b b b" // "c" and "d" are ignored because they are not referenced
'<'
旗。每个格式说明符用普通索引分配一个连续的隐性指标为参数列表是独立的明确的或相对索引使用的指标。
formatter.format("%s %s %s %s", "a", "b", "c", "d") // -> "a b c d"
它是可能的,有一个格式字符串,它使用所有形式的索引,例如:
formatter.format("%2$s %s %<s %s", "a", "b", "c", "d") // -> "b a a b" // "c" and "d" are ignored because they are not referenced
参数的最大数量是由一个java数组的定义由The Java™ Virtual Machine Specification最大尺寸限制。如果参数指标是不符合一个可用的参数,然后MissingFormatArgumentException
抛出。
如果有比格式说明符的更多参数,多余的参数会被忽略。
除非另有规定,通过null
争论在这个类中的任何方法或构造函数会导致NullPointerException
被。
Modifier and Type | Class and Description |
---|---|
static class |
Formatter.BigDecimalLayoutForm
对于
BigDecimal 格式化枚举。
|
Constructor and Description |
---|
Formatter()
构建了一种新的格式化程序。
|
Formatter(Appendable a)
构建与指定的目的地的新格式。
|
Formatter(Appendable a, Locale l)
构建与指定的目的地和现场的新格式。
|
Formatter(File file)
构建与指定文件的新格式。
|
Formatter(File file, String csn)
构建与指定的文件和字符集的一个新的格式化程序。
|
Formatter(File file, String csn, Locale l)
构建了一种新的格式化程序与指定的文件,字符集,以及现场。
|
Formatter(Locale l)
构建与指定的区域设置一个新的格式化程序。
|
Formatter(OutputStream os)
构建一个新的格式化程序指定的输出流。
|
Formatter(OutputStream os, String csn)
构建与指定的输出流和字符集的一个新的格式化程序。
|
Formatter(OutputStream os, String csn, Locale l)
构建了一种新的格式化程序使用指定的输出流,字符集,以及现场。
|
Formatter(PrintStream ps)
构建与指定的打印流的一个新的格式化程序。
|
Formatter(String fileName)
构建与指定文件名的新格式。
|
Formatter(String fileName, String csn)
构建与指定的文件名和一个新的格式化字符。
|
Formatter(String fileName, String csn, Locale l)
构建了一种新的格式化程序与指定的文件名的字符集,以及现场。
|
Modifier and Type | Method and Description |
---|---|
void |
close()
关闭此格式化程序。
|
void |
flush()
将此格式化程序。
|
Formatter |
format(Locale l, String format, Object... args)
使用指定的区域设置、格式字符串和参数,将格式化的字符串写入到该对象的目的地。
|
Formatter |
format(String format, Object... args)
使用指定的格式字符串和参数将格式化的字符串写入到该对象的目的地。
|
IOException |
ioException()
返回
IOException 最后通过这种格式化的
Appendable 扔。
|
Locale |
locale()
返回由该格式化程序的施工现场。
|
Appendable |
out()
返回输出的目的地。
|
String |
toString()
返回调用
toString() 对输出目的地的结果。
|
public Formatter()
格式化输出的目的地是一个StringBuilder
可通过调用out()
检索和其目前的内容可以通过调用toString()
转换成字符串。现场使用的是在这个java虚拟机实例的default locale formatting。
public Formatter(Appendable a)
现场使用的是在这个java虚拟机实例的default locale formatting。
a
-格式化输出目的地。如果
a
是
null
然后
StringBuilder
将被创建。
public Formatter(Locale l)
格式化输出的目的地是一个StringBuilder
可通过调用out()
检索和其目前的内容可以通过调用toString()
转换成字符串。
l
-
locale申请过程中的格式。如果
l
是
null
然后不采用定位。
public Formatter(Appendable a, Locale l)
a
-格式化输出目的地。如果
a
是
null
然后
StringBuilder
将被创建。
l
-
locale申请过程中的格式。如果
l
是
null
然后没有本地化的应用。
public Formatter(String fileName) throws FileNotFoundException
字符集使用的是default charset这个java虚拟机实例。
现场使用的是在这个java虚拟机实例的default locale formatting。
fileName
-该文件的名称作为该格式化程序的目的。如果文件存在,则它将被截断为零;否则,将创建一个新的文件。输出将被写入到文件,并缓冲。
SecurityException
-如果一个安全管理是当前和
checkWrite(fileName)
否认文件的写访问
FileNotFoundException
-如果指定文件名不表示一个存在,可写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
public Formatter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException
现场使用的是在这个java虚拟机实例的default locale formatting。
fileName
-该文件的名称作为该格式化程序的目的。如果文件存在,则它将被截断为零;否则,将创建一个新的文件。输出将被写入到文件,并缓冲。
csn
-支持的
charset名称
FileNotFoundException
-如果指定文件名不表示一个存在,可写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
SecurityException
-如果一个安全管理是当前和
checkWrite(fileName)
否认文件的写访问
UnsupportedEncodingException
-如果指定的字符集不支持
public Formatter(String fileName, String csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException
fileName
-该文件的名称作为该格式化程序的目的。如果文件存在,则它将被截断为零;否则,将创建一个新的文件。输出将被写入到文件,并缓冲。
csn
-支持的
charset名称
l
-
locale申请过程中的格式。如果
l
是
null
然后没有本地化的应用。
FileNotFoundException
-如果指定文件名不表示一个存在,可写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
SecurityException
-如果一个安全管理是当前和
checkWrite(fileName)
否认文件的写访问
UnsupportedEncodingException
-如果指定的字符集不支持
public Formatter(File file) throws FileNotFoundException
字符集是在这个java虚拟机实例default charset。
现场使用的是default locale为formatting这个java虚拟机实例。
file
-文件使用这种格式化程序的目的。如果文件存在,则它将被截断为零;否则,将创建一个新的文件。输出将被写入到文件,并缓冲。
SecurityException
-如果一个安全管理是当前和
checkWrite(file.getPath())
否认文件的写访问
FileNotFoundException
-如果指定文件对象不表示一个存在,可写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
public Formatter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
现场使用的是在这个java虚拟机实例的default locale formatting。
file
-文件使用这种格式化程序的目的。如果文件存在,则它将被截断为零;否则,将创建一个新的文件。输出将被写入到文件,并缓冲。
csn
-支持的
charset名称
FileNotFoundException
-如果指定文件对象不表示一个存在,可写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
SecurityException
-如果一个安全管理是当前和
checkWrite(file.getPath())
否认文件的写访问
UnsupportedEncodingException
-如果指定的字符集不支持
public Formatter(File file, String csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException
file
-文件使用这种格式化程序的目的。如果文件存在,则它将被截断为零;否则,将创建一个新的文件。输出将被写入到文件,并缓冲。
csn
-支持的
charset名称
l
-
locale申请过程中的格式。如果
l
是
null
然后没有本地化的应用。
FileNotFoundException
-如果指定文件对象不表示一个存在,可写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
SecurityException
-如果一个安全管理是当前和
checkWrite(file.getPath())
否认文件的写访问
UnsupportedEncodingException
-如果指定的字符集不支持
public Formatter(PrintStream ps)
现场使用的是在这个java虚拟机实例的default locale formatting。
字写的PrintStream
对象,因此,对象的字符集编码的使用。
ps
流-使用这种格式化程序的目的。
public Formatter(OutputStream os)
字符集是在这个java虚拟机实例default charset。
现场使用的是在这个java虚拟机实例的default locale formatting。
os
-输出流使用此格式化程序的目的。输出将被缓冲。
public Formatter(OutputStream os, String csn) throws UnsupportedEncodingException
现场使用的是在这个java虚拟机实例的default locale formatting。
os
-输出流使用此格式化程序的目的。输出将被缓冲。
csn
-支持的
charset名称
UnsupportedEncodingException
-如果指定的字符集不支持
public Formatter(OutputStream os, String csn, Locale l) throws UnsupportedEncodingException
os
-输出流使用此格式化程序的目的。输出将被缓冲。
csn
-支持的
charset名称
l
-
locale申请过程中的格式。如果
l
是
null
然后不采用定位。
UnsupportedEncodingException
-如果指定的字符集不支持
public Locale locale()
此对象具有现场辩论format
方法不改变这个值。
null
如果不采用定位,否则现场
FormatterClosedException
-如果此格式化程序被调用其
close()
方法封闭
public Appendable out()
FormatterClosedException
-如果此格式化程序被调用其
close()
方法封闭
public String toString()
toString()
对输出目的地的结果。例如,下面的代码格式文本为
StringBuilder
然后检索得到的字符串:
Formatter f = new Formatter(); f.format("Last reboot at %tc", lastRebootDate); String s = f.toString(); // -> s == "Last reboot at Sat Jan 01 00:00:00 PST 2000"
此方法的调用与调用的方式完全相同的行为
out()。tostring()
根据对Appendable
的toString
规范,返回的字符串可能包含或不包含的字符写入目标。例如,缓冲区通常返回其内容toString()
,但不能因为数据流被丢弃。
toString
方法重写,继承类
Object
toString()
对输出目的地的结果
FormatterClosedException
-如果此格式化程序被调用其
close()
方法封闭
public void flush()
flush
接口
Flushable
FormatterClosedException
-如果此格式化程序被调用其
close()
方法封闭
public void close()
Closeable
接口,其
close
方法将被调用。
关闭一个格式化程序可以释放资源可持(如打开的文件)。如果格式化程序已经关闭,然后调用该方法没有效果。
试图调用任何方法除此格式化程序ioException()
后已关闭将导致FormatterClosedException
。
close
接口
Closeable
close
接口
AutoCloseable
public IOException ioException()
null
如无例外的存在。
public Formatter format(String format, Object... args)
format
-格式字符串中所描述的
Format string syntax。
args
由格式字符串的格式说明符引用的论据。如果有比格式说明符的更多参数,多余的参数会被忽略。参数的最大数量是由一个java数组的定义由
The Java™ Virtual Machine Specification最大尺寸限制。
IllegalFormatException
如果格式字符串包含一个非法的语法、格式说明符与给定的参数不兼容,论据不足给定的格式字符串,或者其他非法的条件。对于所有可能的格式错误的规范,看到格式化程序类规范的
Details节。
FormatterClosedException
-如果此格式化程序被调用其
close()
方法封闭
public Formatter format(Locale l, String format, Object... args)
l
-
locale申请过程中的格式。如果
l
是
null
然后没有本地化的应用。这不会改变在构建过程中设置的这个对象的区域设置。
format
-格式字符串中所描述的
Format string syntax
args
由格式字符串的格式说明符引用的论据。如果有比格式说明符的更多参数,多余的参数会被忽略。参数的最大数量是由一个java数组的定义由
The Java™ Virtual Machine Specification最大尺寸限制。
IllegalFormatException
如果格式字符串包含一个非法的语法、格式说明符与给定的参数不兼容,论据不足给定的格式字符串,或者其他非法的条件。对于所有可能的格式错误的规范,看到格式化程序类规范的
Details节。
FormatterClosedException
-如果此格式化程序被调用其
close()
方法封闭
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.