Formatter Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
An interpreter for printf-style format strings.
[Android.Runtime.Register("java/util/Formatter", DoNotGenerateAcw=true)]
public sealed class Formatter : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ICloseable, Java.IO.IFlushable
[<Android.Runtime.Register("java/util/Formatter", DoNotGenerateAcw=true)>]
type Formatter = class
inherit Object
interface ICloseable
interface IJavaObject
interface IDisposable
interface IJavaPeerable
interface IFlushable
- Inheritance
- Attributes
- Implements
Remarks
An interpreter for printf-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output. Common Java types such as byte
, java.math.BigDecimal BigDecimal
, and Calendar
are supported. Limited formatting customization for arbitrary user types is provided through the Formattable
interface.
Formatters are not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.
Formatted printing for the Java language is heavily inspired by C's printf
. Although the format strings are similar to C, some customizations have been made to accommodate the Java language and exploit some of its features. Also, Java formatting is more strict than C's; for example, if a conversion is incompatible with a flag, an exception will be thrown. In C inapplicable flags are silently ignored. The format strings are thus intended to be recognizable to C programmers but not necessarily completely compatible with those in C.
Examples of expected usage:
<blockquote>
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)"
</blockquote>
Convenience methods for common formatting requests exist as illustrated by the following invocations:
<blockquote>
// 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"
</blockquote>
Like C's sprintf(3)
, Strings may be formatted using the static method String#format(String,Object...) String.format
:
<blockquote>
// 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"
</blockquote>
<h3>"org">Organization</h3>
This specification is divided into two sections. The first section, Summary, covers the basic formatting concepts. This section is intended for users who want to get started quickly and are familiar with formatted printing in other programming languages. The second section, Details, covers the specific implementation details. It is intended for users who want more precise specification of formatting behavior.
<h3>"summary">Summary</h3>
This section is intended to provide a brief overview of formatting concepts. For precise behavioral details, refer to the Details section.
<h4>"syntax">Format String Syntax</h4>
Every method which produces formatted output requires a format string and an argument list. The format string is a String
which may contain fixed text and one or more embedded format specifiers. Consider the following example:
<blockquote>
Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
</blockquote>
This format string is the first argument to the format
method. It contains three format specifiers "%1$tm
", "%1$te
", and "%1$tY
" which indicate how the arguments should be processed and where they should be inserted in the text. The remaining portions of the format string are fixed text including "Dukes Birthday: "
and any other spaces or punctuation.
The argument list consists of all arguments passed to the method after the format string. In the above example, the argument list is of size one and consists of the java.util.Calendar Calendar
object c
.
<ul>
<li> The format specifiers for general, character, and numeric types have the following syntax:
<blockquote>
%[argument_index$][flags][width][.precision]conversion
</blockquote>
The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$
", the second by "2$
", etc.
The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.
The optional width is a positive decimal integer indicating the minimum number of characters to be written to the output.
The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.
The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.
<li> The format specifiers for types which are used to represents dates and times have the following syntax:
<blockquote>
%[argument_index$][flags][width]conversion
</blockquote>
The optional argument_index, flags and width are defined as above.
The required conversion is a two character sequence. The first character is 't'
or 'T'
. The second character indicates the format to be used. These characters are similar to but not completely identical to those defined by GNU date
and POSIX strftime(3c)
.
<li> The format specifiers which do not correspond to arguments have the following syntax:
<blockquote>
%[flags][width]conversion
</blockquote>
The optional flags and width is defined as above.
The required conversion is a character indicating content to be inserted in the output.
</ul>
<h4> Conversions </h4>
Conversions are divided into the following categories:
<ol>
<li> <b>General</b> - may be applied to any argument type
<li> <b>Character</b> - may be applied to basic types which represent Unicode characters: char
, Character
, byte
, Byte
, short
, and Short
. This conversion may also be applied to the types int
and Integer
when Character#isValidCodePoint
returns true
<li> <b>Numeric</b>
<ol>
<li> <b>Integral</b> - may be applied to Java integral types: byte
, Byte
, short
, Short
, int
and Integer
, long
, Long
, and java.math.BigInteger BigInteger
(but not char
or Character
)
<li><b>Floating Point</b> - may be applied to Java floating-point types: float
, Float
, double
, Double
, and java.math.BigDecimal BigDecimal
</ol>
<li> <b>Date/Time</b> - may be applied to Java types which are capable of encoding a date or time: long
, Long
, Calendar
, Date
and TemporalAccessor TemporalAccessor
<li> <b>Percent</b> - produces a literal '%'
('\u0025'
)
<li> <b>Line Separator</b> - produces the platform-specific line separator
</ol>
For category General, Character, Numberic, Integral and Date/Time conversion, unless otherwise specified, if the argument arg is null
, then the result is "null
".
The following table summarizes the supported conversions. Conversions denoted by an upper-case character (i.e. 'B'
, 'H'
, 'S'
, 'C'
, 'X'
, 'E'
, 'G'
, 'A'
, and 'T'
) are the same as those for the corresponding lower-case conversion characters except that the result is converted to upper case according to the rules of the prevailing java.util.Locale Locale
. If there is no explicit locale specified, either at the construction of the instance or as a parameter to its method invocation, then the java.util.Locale.Category#FORMAT default locale
is used.
<table class="striped"> <caption style="display:none">genConv</caption> <thead> <tr><th scope="col" style="vertical-align:bottom"> Conversion <th scope="col" style="vertical-align:bottom"> Argument Category <th scope="col" style="vertical-align:bottom"> Description </thead> <tbody> <tr><th scope="row" style="vertical-align:top"> 'b'
, 'B'
<td style="vertical-align:top"> general <td> 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(boolean) String.valueOf(arg)
. Otherwise, the result is "true".
<tr><th scope="row" style="vertical-align:top"> 'h'
, 'H'
<td style="vertical-align:top"> general <td> The result is obtained by invoking Integer.toHexString(arg.hashCode())
.
<tr><th scope="row" style="vertical-align:top"> 's'
, 'S'
<td style="vertical-align:top"> general <td> If arg implements Formattable
, then Formattable#formatTo arg.formatTo
is invoked. Otherwise, the result is obtained by invoking arg.toString()
.
<tr><th scope="row" style="vertical-align:top">'c'
, 'C'
<td style="vertical-align:top"> character <td> The result is a Unicode character
<tr><th scope="row" style="vertical-align:top">'d'
<td style="vertical-align:top"> integral <td> The result is formatted as a decimal integer
<tr><th scope="row" style="vertical-align:top">'o'
<td style="vertical-align:top"> integral <td> The result is formatted as an octal integer
<tr><th scope="row" style="vertical-align:top">'x'
, 'X'
<td style="vertical-align:top"> integral <td> The result is formatted as a hexadecimal integer
<tr><th scope="row" style="vertical-align:top">'e'
, 'E'
<td style="vertical-align:top"> floating point <td> The result is formatted as a decimal number in computerized scientific notation
<tr><th scope="row" style="vertical-align:top">'f'
<td style="vertical-align:top"> floating point <td> The result is formatted as a decimal number
<tr><th scope="row" style="vertical-align:top">'g'
, 'G'
<td style="vertical-align:top"> floating point <td> The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding.
<tr><th scope="row" style="vertical-align:top">'a'
, 'A'
<td style="vertical-align:top"> floating point <td> The result is formatted as a hexadecimal floating-point number with a significand and an exponent. This conversion is <b>not</b> supported for the BigDecimal
type despite the latter's being in the floating point argument category.
<tr><th scope="row" style="vertical-align:top">'t'
, 'T'
<td style="vertical-align:top"> date/time <td> Prefix for date and time conversion characters. See Date/Time Conversions.
<tr><th scope="row" style="vertical-align:top">'%'
<td style="vertical-align:top"> percent <td> The result is a literal '%'
('\u0025'
)
<tr><th scope="row" style="vertical-align:top">'n'
<td style="vertical-align:top"> line separator <td> The result is the platform-specific line separator
</tbody> </table>
Any characters not explicitly defined as conversions are illegal and are reserved for future extensions.
<h4>"dt">Date/Time Conversions</h4>
The following date and time conversion suffix characters are defined for the 't'
and 'T'
conversions. The types are similar to but not completely identical to those defined by GNU date
and POSIX strftime(3c)
. Additional conversion types are provided to access Java-specific functionality (e.g. 'L'
for milliseconds within the second).
The following conversion characters are used for formatting times:
<table class="striped"> <caption style="display:none">time</caption> <tbody> <tr><th scope="row" style="vertical-align:top"> 'H'
<td> Hour of the day for the 24-hour clock, formatted as two digits with a leading zero as necessary i.e. 00 - 23
.
<tr><th scope="row" style="vertical-align:top">'I'
<td> Hour for the 12-hour clock, formatted as two digits with a leading zero as necessary, i.e. 01 - 12
.
<tr><th scope="row" style="vertical-align:top">'k'
<td> Hour of the day for the 24-hour clock, i.e. 0 - 23
.
<tr><th scope="row" style="vertical-align:top">'l'
<td> Hour for the 12-hour clock, i.e. 1 - 12
.
<tr><th scope="row" style="vertical-align:top">'M'
<td> Minute within the hour formatted as two digits with a leading zero as necessary, i.e. 00 - 59
.
<tr><th scope="row" style="vertical-align:top">'S'
<td> 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).
<tr><th scope="row" style="vertical-align:top">'L'
<td> Millisecond within the second formatted as three digits with leading zeros as necessary, i.e. 000 - 999
.
<tr><th scope="row" style="vertical-align:top">'N'
<td> Nanosecond within the second, formatted as nine digits with leading zeros as necessary, i.e. 000000000 - 999999999
.
<tr><th scope="row" style="vertical-align:top">'p'
<td> Locale-specific java.text.DateFormatSymbols#getAmPmStrings morning or afternoon marker in lower case, e.g."am
" or "pm
". Use of the conversion prefix 'T'
forces this output to upper case.
<tr><th scope="row" style="vertical-align:top">'z'
<td> 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 TimeZone#getDefault() default time zone for this instance of the Java virtual machine.
<tr><th scope="row" style="vertical-align:top">'Z'
<td> 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 TimeZone#getDefault() default time zone for this instance of the Java virtual machine. The Formatter's locale will supersede the locale of the argument (if any).
<tr><th scope="row" style="vertical-align:top">'s'
<td> 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
.
<tr><th scope="row" style="vertical-align:top">'Q'
<td> 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
.
</tbody> </table>
The following conversion characters are used for formatting dates:
<table class="striped"> <caption style="display:none">date</caption> <tbody>
<tr><th scope="row" style="vertical-align:top">'B'
<td> Locale-specific java.text.DateFormatSymbols#getMonths full month name, e.g. "January"
, "February"
.
<tr><th scope="row" style="vertical-align:top">'b'
<td> Locale-specific java.text.DateFormatSymbols#getShortMonths abbreviated month name, e.g. "Jan"
, "Feb"
.
<tr><th scope="row" style="vertical-align:top">'h'
<td> Same as 'b'
.
<tr><th scope="row" style="vertical-align:top">'A'
<td> Locale-specific full name of the java.text.DateFormatSymbols#getWeekdays day of the week, e.g. "Sunday"
, "Monday"
<tr><th scope="row" style="vertical-align:top">'a'
<td> Locale-specific short name of the java.text.DateFormatSymbols#getShortWeekdays day of the week, e.g. "Sun"
, "Mon"
<tr><th scope="row" style="vertical-align:top">'C'
<td> Four-digit year divided by 100
, formatted as two digits with leading zero as necessary, i.e. 00 - 99
<tr><th scope="row" style="vertical-align:top">'Y'
<td> Year, formatted as at least four digits with leading zeros as necessary, e.g. 0092
equals 92
CE for the Gregorian calendar.
<tr><th scope="row" style="vertical-align:top">'y'
<td> Last two digits of the year, formatted with leading zeros as necessary, i.e. 00 - 99
.
<tr><th scope="row" style="vertical-align:top">'j'
<td> Day of year, formatted as three digits with leading zeros as necessary, e.g. 001 - 366
for the Gregorian calendar.
<tr><th scope="row" style="vertical-align:top">'m'
<td> Month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13
.
<tr><th scope="row" style="vertical-align:top">'d'
<td> Day of month, formatted as two digits with leading zeros as necessary, i.e. 01 - 31
<tr><th scope="row" style="vertical-align:top">'e'
<td> Day of month, formatted as two digits, i.e. 1 - 31
.
</tbody> </table>
The following conversion characters are used for formatting common date/time compositions.
<table class="striped"> <caption style="display:none">composites</caption> <tbody>
<tr><th scope="row" style="vertical-align:top">'R'
<td> Time formatted for the 24-hour clock as "%tH:%tM"
<tr><th scope="row" style="vertical-align:top">'T'
<td> Time formatted for the 24-hour clock as "%tH:%tM:%tS"
.
<tr><th scope="row" style="vertical-align:top">'r'
<td> 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.
<tr><th scope="row" style="vertical-align:top">'D'
<td> Date formatted as "%tm/%td/%ty"
.
<tr><th scope="row" style="vertical-align:top">'F'
<td> ISO 8601 complete date formatted as "%tY-%tm-%td"
.
<tr><th scope="row" style="vertical-align:top">'c'
<td> Date and time formatted as "%ta %tb %td %tT %tZ %tY"
, e.g. "Sun Jul 20 16:17:00 EDT 1969"
.
</tbody> </table>
Any characters not explicitly defined as date/time conversion suffixes are illegal and are reserved for future extensions.
<h4> Flags </h4>
The following table summarizes the supported flags. y means the flag is supported for the indicated argument types.
<table class="striped"> <caption style="display:none">genConv</caption> <thead> <tr><th scope="col" style="vertical-align:bottom"> Flag <th scope="col" style="vertical-align:bottom"> General <th scope="col" style="vertical-align:bottom"> Character <th scope="col" style="vertical-align:bottom"> Integral <th scope="col" style="vertical-align:bottom"> Floating Point <th scope="col" style="vertical-align:bottom"> Date/Time <th scope="col" style="vertical-align:bottom"> Description </thead> <tbody> <tr><th scope="row"> '-' <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> y <td> The result will be left-justified.
<tr><th scope="row"> '#' <td style="text-align:center; vertical-align:top"> y<sup>1</sup> <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> y<sup>3</sup> <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> - <td> The result should use a conversion-dependent alternate form
<tr><th scope="row"> '+' <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> y<sup>4</sup> <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> - <td> The result will always include a sign
<tr><th scope="row"> ' ' <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> y<sup>4</sup> <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> - <td> The result will include a leading space for positive values
<tr><th scope="row"> '0' <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> y <td style="text-align:center; vertical-align:top"> - <td> The result will be zero-padded
<tr><th scope="row"> ',' <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> y<sup>2</sup> <td style="text-align:center; vertical-align:top"> y<sup>5</sup> <td style="text-align:center; vertical-align:top"> - <td> The result will include locale-specific java.text.DecimalFormatSymbols#getGroupingSeparator grouping separators<tr><th scope="row"> '(' <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> - <td style="text-align:center; vertical-align:top"> y<sup>4</sup> <td style="text-align:center; vertical-align:top"> y<sup>5</sup> <td style="text-align:center"> - <td> The result will enclose negative numbers in parentheses
</tbody> </table>
<sup>1</sup> Depends on the definition of Formattable
.
<sup>2</sup> For 'd'
conversion only.
<sup>3</sup> For 'o'
, 'x'
, and 'X'
conversions only.
<sup>4</sup> For 'd'
, 'o'
, 'x'
, and 'X'
conversions applied to java.math.BigInteger BigInteger
or 'd'
applied to byte
, Byte
, short
, Short
, int
and Integer
, long
, and Long
.
<sup>5</sup> For 'e'
, 'E'
, 'f'
, 'g'
, and 'G'
conversions only.
Any characters not explicitly defined as flags are illegal and are reserved for future extensions.
<h4> Width </h4>
The width is the minimum number of characters to be written to the output. For the line separator conversion, width is not applicable; if it is provided, an exception will be thrown.
<h4> Precision </h4>
For general argument types, the precision is the maximum number of characters to be written to the output.
For the floating-point conversions 'a'
, 'A'
, 'e'
, 'E'
, and 'f'
the precision is the number of digits after the radix point. If the conversion is 'g'
or 'G'
, then the precision is the total number of digits in the resulting magnitude after rounding.
For character, integral, and date/time argument types and the percent and line separator conversions, the precision is not applicable; if a precision is provided, an exception will be thrown.
<h4> Argument Index </h4>
The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$
", the second by "2$
", etc.
Another way to reference arguments by position is to use the '<'
('\u003c'
) flag, which causes the argument for the previous format specifier to be re-used. For example, the following two statements would produce identical strings:
<blockquote>
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);
</blockquote>
<hr> <h3>"detail">Details</h3>
This section is intended to provide behavioral details for formatting, including conditions and exceptions, supported data types, localization, and interactions between flags, conversions, and data types. For an overview of formatting concepts, refer to the Summary
Any characters not explicitly defined as conversions, date/time conversion suffixes, or flags are illegal and are reserved for future extensions. Use of such a character in a format string will cause an UnknownFormatConversionException
or UnknownFormatFlagsException
to be thrown.
If the format specifier contains a width or precision with an invalid value or which is otherwise unsupported, then a IllegalFormatWidthException
or IllegalFormatPrecisionException
respectively will be thrown.
If a format specifier contains a conversion character that is not applicable to the corresponding argument, then an IllegalFormatConversionException
will be thrown.
All specified exceptions may be thrown by any of the format
methods of Formatter
as well as by any format
convenience methods such as String#format(String,Object...) String.format
and java.io.PrintStream#printf(String,Object...) PrintStream.printf
.
For category General, Character, Numberic, Integral and Date/Time conversion, unless otherwise specified, if the argument arg is null
, then the result is "null
".
Conversions denoted by an upper-case character (i.e. 'B'
, 'H'
, 'S'
, 'C'
, 'X'
, 'E'
, 'G'
, 'A'
, and 'T'
) are the same as those for the corresponding lower-case conversion characters except that the result is converted to upper case according to the rules of the prevailing java.util.Locale Locale
. If there is no explicit locale specified, either at the construction of the instance or as a parameter to its method invocation, then the java.util.Locale.Category#FORMAT default locale
is used.
<h4>"dgen">General</h4>
The following general conversions may be applied to any argument type:
<table class="striped"> <caption style="display:none">dgConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 'b'
<td style="vertical-align:top"> '\u0062'
<td> Produces either "true
" or "false
" as returned by Boolean#toString(boolean)
.
If the argument is null
, then the result is "false
". If the argument is a boolean
or Boolean
, then the result is the string returned by String#valueOf(boolean) String.valueOf()
. Otherwise, the result is "true
".
If the '#'
flag is given, then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'B'
<td style="vertical-align:top"> '\u0042'
<td> The upper-case variant of 'b'
.
<tr><th scope="row" style="vertical-align:top"> 'h'
<td style="vertical-align:top"> '\u0068'
<td> Produces a string representing the hash code value of the object.
The result is obtained by invoking Integer.toHexString(arg.hashCode())
.
If the '#'
flag is given, then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'H'
<td style="vertical-align:top"> '\u0048'
<td> The upper-case variant of 'h'
.
<tr><th scope="row" style="vertical-align:top"> 's'
<td style="vertical-align:top"> '\u0073'
<td> Produces a string.
If the argument implements Formattable
, then its Formattable#formatTo formatTo
method is invoked. Otherwise, the result is obtained by invoking the argument's toString()
method.
If the '#'
flag is given and the argument is not a Formattable
, then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'S'
<td style="vertical-align:top"> '\u0053'
<td> The upper-case variant of 's'
.
</tbody> </table>
The following "dFlags">flags apply to general conversions:
<table class="striped"> <caption style="display:none">dFlags</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> '-'
<td style="vertical-align:top"> '\u002d'
<td> 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.
<tr><th scope="row" style="vertical-align:top"> '#'
<td style="vertical-align:top"> '\u0023'
<td> Requires the output use an alternate form. The definition of the form is specified by the conversion.
</tbody> </table>
The "genWidth">width is the minimum number of characters to be written to the output. If the length of the converted value is less than the width then the output will be padded by ' '
('\u0020'
) until the total number of characters equals the width. The padding is on the left by default. If the '-'
flag is given, then the padding will be on the right. If the width is not specified then there is no minimum.
The precision is the maximum number of characters to be written to the output. The precision is applied before the width, thus the output will be truncated to precision
characters even if the width is greater than the precision. If the precision is not specified then there is no explicit limit on the number of characters.
<h4>"dchar">Character</h4>
This conversion may be applied to char
and Character
. It may also be applied to the types byte
, Byte
, short
, and Short
, int
and Integer
when Character#isValidCodePoint
returns true
. If it returns false
then an IllegalFormatCodePointException
will be thrown.
<table class="striped"> <caption style="display:none">charConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 'c'
<td style="vertical-align:top"> '\u0063'
<td> 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 '#'
flag is given, then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'C'
<td style="vertical-align:top"> '\u0043'
<td> The upper-case variant of 'c'
.
</tbody> </table>
The '-'
flag defined for General conversions applies. If the '#'
flag is given, then a FormatFlagsConversionMismatchException
will be thrown.
The width is defined as for General conversions.
The precision is not applicable. If the precision is specified then an IllegalFormatPrecisionException
will be thrown.
<h4>"dnum">Numeric</h4>
Numeric conversions are divided into the following categories:
<ol>
<li> <b>Byte, Short, Integer, and Long</b><li> <b>BigInteger</b><li> <b>Float and Double</b><li> <b>BigDecimal</b></ol>
Numeric types will be formatted according to the following algorithm:
<b>"L10nAlgorithm"> Number Localization Algorithm</b>
After digits are obtained for the integer part, fractional part, and exponent (as appropriate for the data type), the following transformation is applied:
<ol>
<li> Each digit character d in the string is replaced by a locale-specific digit computed relative to the current locale's java.text.DecimalFormatSymbols#getZeroDigit() zero digitz; that is d - '0'
+ z.
<li> If a decimal separator is present, a locale-specific java.text.DecimalFormatSymbols#getDecimalSeparator decimal separator is substituted.
<li> If the ','
('\u002c'
) "L10nGroup">flag is given, then the locale-specific java.text.DecimalFormatSymbols#getGroupingSeparator grouping separator is inserted by scanning the integer part of the string from least significant to most significant digits and inserting a separator at intervals defined by the locale's java.text.DecimalFormat#getGroupingSize() grouping size.
<li> If the '0'
flag is given, then the locale-specific java.text.DecimalFormatSymbols#getZeroDigit() zero digits are inserted after the sign character, if any, and before the first non-zero digit, until the length of the string is equal to the requested field width.
<li> If the value is negative and the '('
flag is given, then a '('
('\u0028'
) is prepended and a ')'
('\u0029'
) is appended.
<li> If the value is negative (or floating-point negative zero) and '('
flag is not given, then a '-'
('\u002d'
) is prepended.
<li> If the '+'
flag is given and the value is positive or zero (or floating-point positive zero), then a '+'
('\u002b'
) will be prepended.
</ol>
If the value is NaN or positive infinity the literal strings "NaN" or "Infinity" respectively, will be output. If the value is negative infinity, then the output will be "(Infinity)" if the '('
flag is given otherwise the output will be "-Infinity". These values are not localized.
"dnint"><b> Byte, Short, Integer, and Long </b>
The following conversions may be applied to byte
, Byte
, short
, Short
, int
and Integer
, long
, and Long
.
<table class="striped"> <caption style="display:none">IntConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 'd'
<td style="vertical-align:top"> '\u0064'
<td> Formats the argument as a decimal integer. The localization algorithm is applied.
If the '0'
flag is given and the value is negative, then the zero padding will occur after the sign.
If the '#'
flag is given then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'o'
<td style="vertical-align:top"> '\u006f'
<td> 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 2<sup>n</sup> to the value where n
is the number of bits in the type as returned by the static SIZE
field in the Byte#SIZE Byte, Short#SIZE Short, Integer#SIZE Integer, or Long#SIZE Long classes as appropriate.
If the '#'
flag is given then the output will always begin with the radix indicator '0'
.
If the '0'
flag is given then the output will be padded with leading zeros to the field width following any indication of sign.
If '('
, '+'
, ' ', or ','
flags are given then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'x'
<td style="vertical-align:top"> '\u0078'
<td> 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 2<sup>n</sup> to the value where n
is the number of bits in the type as returned by the static SIZE
field in the Byte#SIZE Byte, Short#SIZE Short, Integer#SIZE Integer, or Long#SIZE Long classes as appropriate.
If the '#'
flag is given then the output will always begin with the radix indicator "0x"
.
If the '0'
flag is given then the output will be padded to the field width with leading zeros after the radix indicator or sign (if present).
If '('
, ' '
, '+'
, or ','
flags are given then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'X'
<td style="vertical-align:top"> '\u0058'
<td> The upper-case variant of 'x'
. The entire string representing the number will be converted to String#toUpperCase upper case including the 'x'
(if any) and all hexadecimal digits 'a'
- 'f'
('\u0061'
- '\u0066'
).
</tbody> </table>
If the conversion is 'o'
, 'x'
, or 'X'
and both the '#'
and the '0'
flags are given, then result will contain the radix indicator ('0'
for octal and "0x"
or "0X"
for hexadecimal), some number of zeros (based on the width), and the value.
If the '-'
flag is not given, then the space padding will occur before the sign.
The following "intFlags">flags apply to numeric integral conversions:
<table class="striped"> <caption style="display:none">intFlags</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> '+'
<td style="vertical-align:top"> '\u002b'
<td> 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 '+'
and ' '
flags are given then an IllegalFormatFlagsException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> ' '
<td style="vertical-align:top"> '\u0020'
<td> Requires the output to include a single extra space ('\u0020'
) for non-negative values.
If both the '+'
and ' '
flags are given then an IllegalFormatFlagsException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> '0'
<td style="vertical-align:top"> '\u0030'
<td> Requires the output to be padded with leading java.text.DecimalFormatSymbols#getZeroDigit 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 '-'
and '0'
flags are given then an IllegalFormatFlagsException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> ','
<td style="vertical-align:top"> '\u002c'
<td> Requires the output to include the locale-specific java.text.DecimalFormatSymbols#getGroupingSeparator group separators as described in the "group" section of the localization algorithm.
<tr><th scope="row" style="vertical-align:top"> '('
<td style="vertical-align:top"> '\u0028'
<td> Requires the output to prepend a '('
('\u0028'
) and append a ')'
('\u0029'
) to negative values.
</tbody> </table>
If no "intdFlags">flags are given the default formatting is as follows:
<ul>
<li> The output is right-justified within the width
<li> Negative numbers begin with a '-'
('\u002d'
)
<li> Positive numbers and zero do not include a sign or extra leading space
<li> No grouping separators are included
</ul>
The "intWidth">width is the minimum number of characters to be written to the output. This includes any signs, digits, grouping separators, radix indicator, and parentheses. If the length of the converted value is less than the width then the output will be padded by spaces ('\u0020'
) until the total number of characters equals width. The padding is on the left by default. If '-'
flag is given then the padding will be on the right. If width is not specified then there is no minimum.
The precision is not applicable. If precision is specified then an IllegalFormatPrecisionException
will be thrown.
"dnbint"><b> BigInteger </b>
The following conversions may be applied to java.math.BigInteger
.
<table class="striped"> <caption style="display:none">bIntConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 'd'
<td style="vertical-align:top"> '\u0064'
<td> Requires the output to be formatted as a decimal integer. The localization algorithm is applied.
If the '#'
flag is given FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'o'
<td style="vertical-align:top"> '\u006f'
<td> 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 '-'
('\u002d'
). Signed output is allowed for this type because unlike the primitive types it is not possible to create an unsigned equivalent without assuming an explicit data-type size.
If x is positive or zero and the '+'
flag is given then the result will begin with '+'
('\u002b'
).
If the '#'
flag is given then the output will always begin with '0'
prefix.
If the '0'
flag is given then the output will be padded with leading zeros to the field width following any indication of sign.
If the ','
flag is given then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'x'
<td style="vertical-align:top"> '\u0078'
<td> 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 '-'
('\u002d'
). Signed output is allowed for this type because unlike the primitive types it is not possible to create an unsigned equivalent without assuming an explicit data-type size.
If x is positive or zero and the '+'
flag is given then the result will begin with '+'
('\u002b'
).
If the '#'
flag is given then the output will always begin with the radix indicator "0x"
.
If the '0'
flag is given then the output will be padded to the field width with leading zeros after the radix indicator or sign (if present).
If the ','
flag is given then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'X'
<td style="vertical-align:top"> '\u0058'
<td> The upper-case variant of 'x'
. The entire string representing the number will be converted to String#toUpperCase upper case including the 'x'
(if any) and all hexadecimal digits 'a'
- 'f'
('\u0061'
- '\u0066'
).
</tbody> </table>
If the conversion is 'o'
, 'x'
, or 'X'
and both the '#'
and the '0'
flags are given, then result will contain the base indicator ('0'
for octal and "0x"
or "0X"
for hexadecimal), some number of zeros (based on the width), and the value.
If the '0'
flag is given and the value is negative, then the zero padding will occur after the sign.
If the '-'
flag is not given, then the space padding will occur before the sign.
All flags defined for Byte, Short, Integer, and Long apply. The default behavior when no flags are given is the same as for Byte, Short, Integer, and Long.
The specification of width is the same as defined for Byte, Short, Integer, and Long.
The precision is not applicable. If precision is specified then an IllegalFormatPrecisionException
will be thrown.
"dndec"><b> Float and Double</b>
The following conversions may be applied to float
, Float
, double
and Double
.
<table class="striped"> <caption style="display:none">floatConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 'e'
<td style="vertical-align:top"> '\u0065'
<td> Requires the output to be formatted using "scientific">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 "+00"
.
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 10<sup>n</sup> <= m < 10<sup>n+1</sup>; then let a be the mathematically exact quotient of m and 10<sup>n</sup> 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 lower-case locale-specific java.text.DecimalFormatSymbols#getExponentSeparator exponent separator (e.g. 'e'
), followed by the sign of the exponent, followed by a representation of n as a decimal integer, as produced by the method Long#toString(long, int)
, and zero-padded to include at least two digits.
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 6
. If the precision is less than the number of digits which would appear after the decimal point in the string returned by Float#toString(float)
or Double#toString(double)
respectively, then the value will be rounded using the java.math.RoundingMode#HALF_UP round half up algorithm. Otherwise, zeros may be appended to reach the precision. For a canonical representation of the value, use Float#toString(float)
or Double#toString(double)
as appropriate.
If the ','
flag is given, then an FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'E'
<td style="vertical-align:top"> '\u0045'
<td> The upper-case variant of 'e'
. The exponent symbol will be the upper-case locale-specific java.text.DecimalFormatSymbols#getExponentSeparator exponent separator (e.g. 'E'
).
<tr><th scope="row" style="vertical-align:top"> 'g'
<td style="vertical-align:top"> '\u0067'
<td> 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<sup>-4</sup> but less than 10<sup>precision</sup> then it is represented in decimal format.
If m is less than 10<sup>-4</sup> or greater than or equal to 10<sup>precision</sup>, 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 6
. If the precision is 0
, then it is taken to be 1
.
If the '#'
flag is given then an FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'G'
<td style="vertical-align:top"> '\u0047'
<td> The upper-case variant of 'g'
.
<tr><th scope="row" style="vertical-align:top"> 'f'
<td style="vertical-align:top"> '\u0066'
<td> Requires the output to be formatted using "decimal">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 6
. If the precision is less than the number of digits which would appear after the decimal point in the string returned by Float#toString(float)
or Double#toString(double)
respectively, then the value will be rounded using the java.math.RoundingMode#HALF_UP round half up algorithm. Otherwise, zeros may be appended to reach the precision. For a canonical representation of the value, use Float#toString(float)
or Double#toString(double)
as appropriate.
<tr><th scope="row" style="vertical-align:top"> 'a'
<td style="vertical-align:top"> '\u0061'
<td> 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 '-'
('\u002d'
).
If x is positive or a positive-zero value and the '+'
flag is given then the result will begin with '+'
('\u002b'
).
The formatting of the magnitude m depends upon its value.
<ul>
<li> If the value is NaN or infinite, the literal strings "NaN" or "Infinity", respectively, will be output.
<li> If m is zero then it is represented by the string "0x0.0p0"
.
<li> If m is a double
value with a normalized representation then substrings are used to represent the significand and exponent fields. The significand is represented by the characters "0x1."
followed by the hexadecimal representation of the rest of the significand as a fraction. The exponent is represented by 'p'
('\u0070'
) followed by a decimal string of the unbiased exponent as if produced by invoking Integer#toString(int) Integer.toString
on the exponent value. If the precision is specified, the value is rounded to the given number of hexadecimal digits.
<li> If m is a double
value with a subnormal representation then, unless the precision is specified to be in the range 1 through 12, inclusive, the significand is represented by the characters '0x0.'
followed by the hexadecimal representation of the rest of the significand as a fraction, and the exponent represented by 'p-1022'
. If the precision is in the interval [1, 12], the subnormal value is normalized such that it begins with the characters '0x1.'
, rounded to the number of hexadecimal digits of precision, and the exponent adjusted accordingly. Note that there must be at least one nonzero digit in a subnormal significand.
</ul>
If the '('
or ','
flags are given, then a FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'A'
<td style="vertical-align:top"> '\u0041'
<td> 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'
).
</tbody> </table>
All flags defined for Byte, Short, Integer, and Long apply.
If the '#'
flag is given, then the decimal separator will always be present.
If no "floatdFlags">flags are given the default formatting is as follows:
<ul>
<li> The output is right-justified within the width
<li> Negative numbers begin with a '-'
<li> Positive numbers and positive zero do not include a sign or extra leading space
<li> No grouping separators are included
<li> The decimal separator will only appear if a digit follows it
</ul>
The "floatDWidth">width is the minimum number of characters to be written to the output. This includes any signs, digits, grouping separators, decimal separators, exponential symbol, radix indicator, parentheses, and strings representing infinity and NaN as applicable. If the length of the converted value is less than the width then the output will be padded by spaces ('\u0020'
) until the total number of characters equals width. The padding is on the left by default. If the '-'
flag is given then the padding will be on the right. If width is not specified then there is no minimum.
If the "floatDPrec">conversion is 'e'
, 'E'
or 'f'
, then the precision is the number of digits after the decimal separator. If the precision is not specified, then it is assumed to be 6
.
If the conversion is 'g'
or 'G'
, then the precision is the total number of significant digits in the resulting magnitude after rounding. If the precision is not specified, then the default value is 6
. If the precision is 0
, then it is taken to be 1
.
If the conversion is 'a'
or 'A'
, then the precision is the number of hexadecimal digits after the radix point. If the precision is not provided, then all of the digits as returned by Double#toHexString(double)
will be output.
"dnbdec"><b> BigDecimal </b>
The following conversions may be applied java.math.BigDecimal BigDecimal
.
<table class="striped"> <caption style="display:none">floatConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 'e'
<td style="vertical-align:top"> '\u0065'
<td> Requires the output to be formatted using "bscientific">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 "+00"
.
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 10<sup>n</sup> <= m < 10<sup>n+1</sup>; then let a be the mathematically exact quotient of m and 10<sup>n</sup> 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 'e'
('\u0065'
), followed by the sign of the exponent, followed by a representation of n as a decimal integer, as produced by the method Long#toString(long, int)
, and zero-padded to include at least two digits.
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 6
. If the precision is less than the number of digits to the right of the decimal point then the value will be rounded using the java.math.RoundingMode#HALF_UP round half up algorithm. Otherwise, zeros may be appended to reach the precision. For a canonical representation of the value, use BigDecimal#toString()
.
If the ','
flag is given, then an FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'E'
<td style="vertical-align:top"> '\u0045'
<td> The upper-case variant of 'e'
. The exponent symbol will be 'E'
('\u0045'
).
<tr><th scope="row" style="vertical-align:top"> 'g'
<td style="vertical-align:top"> '\u0067'
<td> 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<sup>-4</sup> but less than 10<sup>precision</sup> then it is represented in decimal format.
If m is less than 10<sup>-4</sup> or greater than or equal to 10<sup>precision</sup>, 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 6
. If the precision is 0
, then it is taken to be 1
.
If the '#'
flag is given then an FormatFlagsConversionMismatchException
will be thrown.
<tr><th scope="row" style="vertical-align:top"> 'G'
<td style="vertical-align:top"> '\u0047'
<td> The upper-case variant of 'g'
.
<tr><th scope="row" style="vertical-align:top"> 'f'
<td style="vertical-align:top"> '\u0066'
<td> Requires the output to be formatted using "bdecimal">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 6
. If the precision is less than the number of digits to the right of the decimal point then the value will be rounded using the java.math.RoundingMode#HALF_UP round half up algorithm. Otherwise, zeros may be appended to reach the precision. For a canonical representation of the value, use BigDecimal#toString()
.
</tbody> </table>
All flags defined for Byte, Short, Integer, and Long apply.
If the '#'
flag is given, then the decimal separator will always be present.
The default behavior when no flags are given is the same as for Float and Double.
The specification of width and precision is the same as defined for Float and Double.
<h4>"ddt">Date/Time</h4>
This conversion may be applied to long
, Long
, Calendar
, Date
and TemporalAccessor TemporalAccessor
<table class="striped"> <caption style="display:none">DTConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 't'
<td style="vertical-align:top"> '\u0074'
<td> Prefix for date and time conversion characters. <tr><th scope="row" style="vertical-align:top"> 'T'
<td style="vertical-align:top"> '\u0054'
<td> The upper-case variant of 't'
.
</tbody> </table>
The following date and time conversion character suffixes are defined for the 't'
and 'T'
conversions. The types are similar to but not completely identical to those defined by GNU date
and POSIX strftime(3c)
. Additional conversion types are provided to access Java-specific functionality (e.g. 'L'
for milliseconds within the second).
The following conversion characters are used for formatting times:
<table class="striped"> <caption style="display:none">time</caption> <tbody>
<tr><th scope="row" style="vertical-align:top"> 'H'
<td style="vertical-align:top"> '\u0048'
<td> 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.
<tr><th scope="row" style="vertical-align:top">'I'
<td style="vertical-align:top"> '\u0049'
<td> 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).
<tr><th scope="row" style="vertical-align:top">'k'
<td style="vertical-align:top"> '\u006b'
<td> Hour of the day for the 24-hour clock, i.e. 0 - 23
. 0
corresponds to midnight.
<tr><th scope="row" style="vertical-align:top">'l'
<td style="vertical-align:top"> '\u006c'
<td> Hour for the 12-hour clock, i.e. 1 - 12
. 1
corresponds to one o'clock (either morning or afternoon).
<tr><th scope="row" style="vertical-align:top">'M'
<td style="vertical-align:top"> '\u004d'
<td> Minute within the hour formatted as two digits with a leading zero as necessary, i.e. 00 - 59
.
<tr><th scope="row" style="vertical-align:top">'S'
<td style="vertical-align:top"> '\u0053'
<td> 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).
<tr><th scope="row" style="vertical-align:top">'L'
<td style="vertical-align:top"> '\u004c'
<td> Millisecond within the second formatted as three digits with leading zeros as necessary, i.e. 000 - 999
.
<tr><th scope="row" style="vertical-align:top">'N'
<td style="vertical-align:top"> '\u004e'
<td> 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.
<tr><th scope="row" style="vertical-align:top">'p'
<td style="vertical-align:top"> '\u0070'
<td> Locale-specific java.text.DateFormatSymbols#getAmPmStrings 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.)
<tr><th scope="row" style="vertical-align:top">'z'
<td style="vertical-align:top"> '\u007a'
<td> 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 TimeZone#getDefault() default time zone for this instance of the Java virtual machine.
<tr><th scope="row" style="vertical-align:top">'Z'
<td style="vertical-align:top"> '\u005a'
<td> 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 TimeZone#getDefault() default time zone for this instance of the Java virtual machine. The Formatter's locale will supersede the locale of the argument (if any).
<tr><th scope="row" style="vertical-align:top">'s'
<td style="vertical-align:top"> '\u0073'
<td> 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
.
<tr><th scope="row" style="vertical-align:top">'Q'
<td style="vertical-align:top"> '\u004f'
<td> 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.
</tbody> </table>
The following conversion characters are used for formatting dates:
<table class="striped"> <caption style="display:none">date</caption> <tbody>
<tr><th scope="row" style="vertical-align:top">'B'
<td style="vertical-align:top"> '\u0042'
<td> Locale-specific java.text.DateFormatSymbols#getMonths full month name, e.g. "January"
, "February"
.
<tr><th scope="row" style="vertical-align:top">'b'
<td style="vertical-align:top"> '\u0062'
<td> Locale-specific java.text.DateFormatSymbols#getShortMonths abbreviated month name, e.g. "Jan"
, "Feb"
.
<tr><th scope="row" style="vertical-align:top">'h'
<td style="vertical-align:top"> '\u0068'
<td> Same as 'b'
.
<tr><th scope="row" style="vertical-align:top">'A'
<td style="vertical-align:top"> '\u0041'
<td> Locale-specific full name of the java.text.DateFormatSymbols#getWeekdays day of the week, e.g. "Sunday"
, "Monday"
<tr><th scope="row" style="vertical-align:top">'a'
<td style="vertical-align:top"> '\u0061'
<td> Locale-specific short name of the java.text.DateFormatSymbols#getShortWeekdays day of the week, e.g. "Sun"
, "Mon"
<tr><th scope="row" style="vertical-align:top">'C'
<td style="vertical-align:top"> '\u0043'
<td> Four-digit year divided by 100
, formatted as two digits with leading zero as necessary, i.e. 00 - 99
<tr><th scope="row" style="vertical-align:top">'Y'
<td style="vertical-align:top"> '\u0059'
<td> Year, formatted to at least four digits with leading zeros as necessary, e.g. 0092
equals 92
CE for the Gregorian calendar.
<tr><th scope="row" style="vertical-align:top">'y'
<td style="vertical-align:top"> '\u0079'
<td> Last two digits of the year, formatted with leading zeros as necessary, i.e. 00 - 99
.
<tr><th scope="row" style="vertical-align:top">'j'
<td style="vertical-align:top"> '\u006a'
<td> 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.
<tr><th scope="row" style="vertical-align:top">'m'
<td style="vertical-align:top"> '\u006d'
<td> 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).
<tr><th scope="row" style="vertical-align:top">'d'
<td style="vertical-align:top"> '\u0064'
<td> 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.
<tr><th scope="row" style="vertical-align:top">'e'
<td style="vertical-align:top"> '\u0065'
<td> Day of month, formatted as two digits, i.e. 1 - 31
where "1
" is the first day of the month.
</tbody> </table>
The following conversion characters are used for formatting common date/time compositions.
<table class="striped"> <caption style="display:none">composites</caption> <tbody>
<tr><th scope="row" style="vertical-align:top">'R'
<td style="vertical-align:top"> '\u0052'
<td> Time formatted for the 24-hour clock as "%tH:%tM"
<tr><th scope="row" style="vertical-align:top">'T'
<td style="vertical-align:top"> '\u0054'
<td> Time formatted for the 24-hour clock as "%tH:%tM:%tS"
.
<tr><th scope="row" style="vertical-align:top">'r'
<td style="vertical-align:top"> '\u0072'
<td> 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.
<tr><th scope="row" style="vertical-align:top">'D'
<td style="vertical-align:top"> '\u0044'
<td> Date formatted as "%tm/%td/%ty"
.
<tr><th scope="row" style="vertical-align:top">'F'
<td style="vertical-align:top"> '\u0046'
<td> ISO 8601 complete date formatted as "%tY-%tm-%td"
.
<tr><th scope="row" style="vertical-align:top">'c'
<td style="vertical-align:top"> '\u0063'
<td> Date and time formatted as "%ta %tb %td %tT %tZ %tY"
, e.g. "Sun Jul 20 16:17:00 EDT 1969"
.
</tbody> </table>
The '-'
flag defined for General conversions applies. If the '#'
flag is given, then a FormatFlagsConversionMismatchException
will be thrown.
The width is the minimum number of characters to be written to the output. If the length of the converted value is less than the width
then the output will be padded by spaces ('\u0020'
) until the total number of characters equals width. The padding is on the left by default. If the '-'
flag is given then the padding will be on the right. If width is not specified then there is no minimum.
The precision is not applicable. If the precision is specified then an IllegalFormatPrecisionException
will be thrown.
<h4>"dper">Percent</h4>
The conversion does not correspond to any argument.
<table class="striped"> <caption style="display:none">DTConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top">'%'
<td> The result is a literal '%'
('\u0025'
)
The width is the minimum number of characters to be written to the output including the '%'
. If the length of the converted value is less than the width
then the output will be padded by spaces ('\u0020'
) until the total number of characters equals width. The padding is on the left. If width is not specified then just the '%'
is output.
The '-'
flag defined for General conversions applies. If any other flags are provided, then a FormatFlagsConversionMismatchException
will be thrown.
The precision is not applicable. If the precision is specified an IllegalFormatPrecisionException
will be thrown.
</tbody> </table>
<h4>"dls">Line Separator</h4>
The conversion does not correspond to any argument.
<table class="striped"> <caption style="display:none">DTConv</caption> <tbody>
<tr><th scope="row" style="vertical-align:top">'n'
<td> the platform-specific line separator as returned by System#lineSeparator()
.
</tbody> </table>
Flags, width, and precision are not applicable. If any are provided an IllegalFormatFlagsException
, IllegalFormatWidthException
, and IllegalFormatPrecisionException
, respectively will be thrown.
<h4>"dpos">Argument Index</h4>
Format specifiers can reference arguments in three ways:
<ul>
<li> Explicit indexing is used when the format specifier contains an argument index. The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$
", the second by "2$
", etc. An argument may be referenced more than once.
For example:
<blockquote>
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"
</blockquote>
<li> Relative indexing is used when the format specifier contains a '<'
('\u003c'
) flag which causes the argument for the previous format specifier to be re-used. If there is no previous argument, then a MissingFormatArgumentException
is thrown.
<blockquote>
formatter.format("%s %s %<s %<s", "a", "b", "c", "d")
// -> "a b b b"
// "c" and "d" are ignored because they are not referenced
</blockquote>
<li> Ordinary indexing is used when the format specifier contains neither an argument index nor a '<'
flag. Each format specifier which uses ordinary indexing is assigned a sequential implicit index into argument list which is independent of the indices used by explicit or relative indexing.
<blockquote>
formatter.format("%s %s %s %s", "a", "b", "c", "d")
// -> "a b c d"
</blockquote>
</ul>
It is possible to have a format string which uses all forms of indexing, for example:
<blockquote>
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
</blockquote>
The maximum number of arguments is limited by the maximum dimension of a Java array as defined by <cite>The Java™ Virtual Machine Specification</cite>. If the argument index does not correspond to an available argument, then a MissingFormatArgumentException
is thrown.
If there are more arguments than format specifiers, the extra arguments are ignored.
Unless otherwise specified, passing a null
argument to any method or constructor in this class will cause a NullPointerException
to be thrown.
Added in 1.5.
Java documentation for java.util.Formatter
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
Formatter() |
Constructs a new formatter. |
Formatter(File, Charset, Locale) |
Constructs a new formatter with the specified file, charset, and locale. |
Formatter(File, String, Locale) |
Constructs a new formatter with the specified file, charset, and locale. |
Formatter(File, String) |
Constructs a new formatter with the specified file and charset. |
Formatter(File) |
Constructs a new formatter with the specified file. |
Formatter(IAppendable, Locale) |
Constructs a new formatter with the specified destination and locale. |
Formatter(IAppendable) |
Constructs a new formatter with the specified destination. |
Formatter(Locale) |
Constructs a new formatter with the specified locale. |
Formatter(PrintStream) |
Constructs a new formatter with the specified print stream. |
Formatter(Stream, Charset, Locale) |
Constructs a new formatter with the specified output stream, charset, and locale. |
Formatter(Stream, String, Locale) |
Constructs a new formatter with the specified output stream, charset, and locale. |
Formatter(Stream, String) |
Constructs a new formatter with the specified output stream and charset. |
Formatter(Stream) |
Constructs a new formatter with the specified output stream. |
Formatter(String, Charset, Locale) |
Constructs a new formatter with the specified file name, charset, and locale. |
Formatter(String, String, Locale) |
Constructs a new formatter with the specified file name, charset, and locale. |
Formatter(String, String) |
Constructs a new formatter with the specified file name and charset. |
Formatter(String) |
Constructs a new formatter with the specified file name. |
Properties
Class |
Returns the runtime class of this |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
JniIdentityHashCode | (Inherited from Object) |
JniPeerMembers | |
PeerReference | (Inherited from Object) |
ThresholdClass |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. (Inherited from Object) |
ThresholdType |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. (Inherited from Object) |
Methods
Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
Close() |
Closes this formatter. |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
Flush() |
Flushes this formatter. |
Format(Locale, String, Object[]) |
Writes a formatted string to this object's destination using the specified format string and arguments. |
Format(String, Object[]) |
Writes a formatted string to this object's destination using the specified format string and arguments. |
GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
IoException() |
Returns the |
JavaFinalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
Locale() |
Returns the locale set by the construction of this formatter. |
Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
NotifyAll() |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
Out() |
Returns the destination for the output. |
SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
ToArray<T>() | (Inherited from Object) |
ToString() |
Returns a string representation of the object. (Inherited from Object) |
UnregisterFromRuntime() | (Inherited from Object) |
Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Explicit Interface Implementations
IJavaPeerable.Disposed() | (Inherited from Object) |
IJavaPeerable.DisposeUnlessReferenced() | (Inherited from Object) |
IJavaPeerable.Finalized() | (Inherited from Object) |
IJavaPeerable.JniManagedPeerState | (Inherited from Object) |
IJavaPeerable.SetJniIdentityHashCode(Int32) | (Inherited from Object) |
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) | (Inherited from Object) |
IJavaPeerable.SetPeerReference(JniObjectReference) | (Inherited from Object) |
Extension Methods
JavaCast<TResult>(IJavaObject) |
Performs an Android runtime-checked type conversion. |
JavaCast<TResult>(IJavaObject) | |
GetJniTypeName(IJavaPeerable) | |
FlushAsync(IFlushable) |