0N/A/*
3377N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/Aimport java.io.BufferedWriter;
0N/Aimport java.io.Closeable;
0N/Aimport java.io.IOException;
0N/Aimport java.io.File;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.Flushable;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.UnsupportedEncodingException;
0N/Aimport java.math.BigDecimal;
0N/Aimport java.math.BigInteger;
0N/Aimport java.math.MathContext;
806N/Aimport java.math.RoundingMode;
0N/Aimport java.nio.charset.Charset;
3377N/Aimport java.nio.charset.IllegalCharsetNameException;
3377N/Aimport java.nio.charset.UnsupportedCharsetException;
0N/Aimport java.text.DateFormatSymbols;
0N/Aimport java.text.DecimalFormat;
0N/Aimport java.text.DecimalFormatSymbols;
0N/Aimport java.text.NumberFormat;
0N/Aimport java.util.regex.Matcher;
0N/Aimport java.util.regex.Pattern;
0N/A
0N/Aimport sun.misc.FpUtils;
0N/Aimport sun.misc.DoubleConsts;
0N/Aimport sun.misc.FormattedFloatingDecimal;
0N/A
0N/A/**
0N/A * An interpreter for printf-style format strings. This class provides support
0N/A * for layout justification and alignment, common formats for numeric, string,
0N/A * and date/time data, and locale-specific output. Common Java types such as
847N/A * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
0N/A * are supported. Limited formatting customization for arbitrary user types is
0N/A * provided through the {@link Formattable} interface.
0N/A *
0N/A * <p> Formatters are not necessarily safe for multithreaded access. Thread
0N/A * safety is optional and is the responsibility of users of methods in this
0N/A * class.
0N/A *
0N/A * <p> Formatted printing for the Java language is heavily inspired by C's
847N/A * {@code printf}. Although the format strings are similar to C, some
0N/A * customizations have been made to accommodate the Java language and exploit
0N/A * some of its features. Also, Java formatting is more strict than C's; for
0N/A * example, if a conversion is incompatible with a flag, an exception will be
0N/A * thrown. In C inapplicable flags are silently ignored. The format strings
0N/A * are thus intended to be recognizable to C programmers but not necessarily
0N/A * completely compatible with those in C.
0N/A *
0N/A * <p> Examples of expected usage:
0N/A *
0N/A * <blockquote><pre>
0N/A * StringBuilder sb = new StringBuilder();
0N/A * // Send all output to the Appendable object sb
0N/A * Formatter formatter = new Formatter(sb, Locale.US);
0N/A *
0N/A * // Explicit argument indices may be used to re-order output.
0N/A * formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
0N/A * // -&gt; " d c b a"
0N/A *
0N/A * // Optional locale as the first argument can be used to get
0N/A * // locale-specific formatting of numbers. The precision and width can be
0N/A * // given to round and align the value.
0N/A * formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
0N/A * // -&gt; "e = +2,7183"
0N/A *
0N/A * // The '(' numeric flag may be used to format negative numbers with
0N/A * // parentheses rather than a minus sign. Group separators are
0N/A * // automatically inserted.
0N/A * formatter.format("Amount gained or lost since last statement: $ %(,.2f",
0N/A * balanceDelta);
0N/A * // -&gt; "Amount gained or lost since last statement: $ (6,217.58)"
0N/A * </pre></blockquote>
0N/A *
0N/A * <p> Convenience methods for common formatting requests exist as illustrated
0N/A * by the following invocations:
0N/A *
0N/A * <blockquote><pre>
0N/A * // Writes a formatted string to System.out.
0N/A * System.out.format("Local time: %tT", Calendar.getInstance());
0N/A * // -&gt; "Local time: 13:34:18"
0N/A *
0N/A * // Writes formatted output to System.err.
0N/A * System.err.printf("Unable to open file '%1$s': %2$s",
0N/A * fileName, exception.getMessage());
0N/A * // -&gt; "Unable to open file 'food': No such file or directory"
0N/A * </pre></blockquote>
0N/A *
847N/A * <p> Like C's {@code sprintf(3)}, Strings may be formatted using the static
0N/A * method {@link String#format(String,Object...) String.format}:
0N/A *
0N/A * <blockquote><pre>
0N/A * // Format a string containing a date.
0N/A * import java.util.Calendar;
0N/A * import java.util.GregorianCalendar;
0N/A * import static java.util.Calendar.*;
0N/A *
0N/A * Calendar c = new GregorianCalendar(1995, MAY, 23);
0N/A * String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
0N/A * // -&gt; s == "Duke's Birthday: May 23, 1995"
0N/A * </pre></blockquote>
0N/A *
0N/A * <h3><a name="org">Organization</a></h3>
0N/A *
0N/A * <p> This specification is divided into two sections. The first section, <a
0N/A * href="#summary">Summary</a>, covers the basic formatting concepts. This
0N/A * section is intended for users who want to get started quickly and are
0N/A * familiar with formatted printing in other programming languages. The second
0N/A * section, <a href="#detail">Details</a>, covers the specific implementation
0N/A * details. It is intended for users who want more precise specification of
0N/A * formatting behavior.
0N/A *
0N/A * <h3><a name="summary">Summary</a></h3>
0N/A *
0N/A * <p> This section is intended to provide a brief overview of formatting
0N/A * concepts. For precise behavioral details, refer to the <a
0N/A * href="#detail">Details</a> section.
0N/A *
0N/A * <h4><a name="syntax">Format String Syntax</a></h4>
0N/A *
0N/A * <p> Every method which produces formatted output requires a <i>format
0N/A * string</i> and an <i>argument list</i>. The format string is a {@link
0N/A * String} which may contain fixed text and one or more embedded <i>format
0N/A * specifiers</i>. Consider the following example:
0N/A *
0N/A * <blockquote><pre>
0N/A * Calendar c = ...;
0N/A * String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
0N/A * </pre></blockquote>
0N/A *
847N/A * This format string is the first argument to the {@code format} method. It
847N/A * contains three format specifiers "{@code %1$tm}", "{@code %1$te}", and
847N/A * "{@code %1$tY}" which indicate how the arguments should be processed and
0N/A * where they should be inserted in the text. The remaining portions of the
847N/A * format string are fixed text including {@code "Dukes Birthday: "} and any
0N/A * other spaces or punctuation.
0N/A *
0N/A * The argument list consists of all arguments passed to the method after the
0N/A * format string. In the above example, the argument list is of size one and
847N/A * consists of the {@link java.util.Calendar Calendar} object {@code c}.
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li> The format specifiers for general, character, and numeric types have
0N/A * the following syntax:
0N/A *
0N/A * <blockquote><pre>
0N/A * %[argument_index$][flags][width][.precision]conversion
0N/A * </pre></blockquote>
0N/A *
0N/A * <p> The optional <i>argument_index</i> is a decimal integer indicating the
0N/A * position of the argument in the argument list. The first argument is
847N/A * referenced by "{@code 1$}", the second by "{@code 2$}", etc.
0N/A *
0N/A * <p> The optional <i>flags</i> is a set of characters that modify the output
0N/A * format. The set of valid flags depends on the conversion.
0N/A *
0N/A * <p> The optional <i>width</i> is a non-negative decimal integer indicating
0N/A * the minimum number of characters to be written to the output.
0N/A *
0N/A * <p> The optional <i>precision</i> is a non-negative decimal integer usually
0N/A * used to restrict the number of characters. The specific behavior depends on
0N/A * the conversion.
0N/A *
0N/A * <p> The required <i>conversion</i> is a character indicating how the
0N/A * argument should be formatted. The set of valid conversions for a given
0N/A * argument depends on the argument's data type.
0N/A *
0N/A * <li> The format specifiers for types which are used to represents dates and
0N/A * times have the following syntax:
0N/A *
0N/A * <blockquote><pre>
0N/A * %[argument_index$][flags][width]conversion
0N/A * </pre></blockquote>
0N/A *
0N/A * <p> The optional <i>argument_index</i>, <i>flags</i> and <i>width</i> are
0N/A * defined as above.
0N/A *
0N/A * <p> The required <i>conversion</i> is a two character sequence. The first
847N/A * character is {@code 't'} or {@code 'T'}. The second character indicates
0N/A * the format to be used. These characters are similar to but not completely
847N/A * identical to those defined by GNU {@code date} and POSIX
847N/A * {@code strftime(3c)}.
0N/A *
0N/A * <li> The format specifiers which do not correspond to arguments have the
0N/A * following syntax:
0N/A *
0N/A * <blockquote><pre>
0N/A * %[flags][width]conversion
0N/A * </pre></blockquote>
0N/A *
0N/A * <p> The optional <i>flags</i> and <i>width</i> is defined as above.
0N/A *
0N/A * <p> The required <i>conversion</i> is a character indicating content to be
0N/A * inserted in the output.
0N/A *
0N/A * </ul>
0N/A *
0N/A * <h4> Conversions </h4>
0N/A *
0N/A * <p> Conversions are divided into the following categories:
0N/A *
0N/A * <ol>
0N/A *
0N/A * <li> <b>General</b> - may be applied to any argument
0N/A * type
0N/A *
0N/A * <li> <b>Character</b> - may be applied to basic types which represent
847N/A * Unicode characters: {@code char}, {@link Character}, {@code byte}, {@link
847N/A * Byte}, {@code short}, and {@link Short}. This conversion may also be
847N/A * applied to the types {@code int} and {@link Integer} when {@link
847N/A * Character#isValidCodePoint} returns {@code true}
0N/A *
0N/A * <li> <b>Numeric</b>
0N/A *
0N/A * <ol>
0N/A *
847N/A * <li> <b>Integral</b> - may be applied to Java integral types: {@code byte},
847N/A * {@link Byte}, {@code short}, {@link Short}, {@code int} and {@link
847N/A * Integer}, {@code long}, {@link Long}, and {@link java.math.BigInteger
0N/A * BigInteger}
0N/A *
0N/A * <li><b>Floating Point</b> - may be applied to Java floating-point types:
847N/A * {@code float}, {@link Float}, {@code double}, {@link Double}, and {@link
0N/A * java.math.BigDecimal BigDecimal}
0N/A *
0N/A * </ol>
0N/A *
0N/A * <li> <b>Date/Time</b> - may be applied to Java types which are capable of
847N/A * encoding a date or time: {@code long}, {@link Long}, {@link Calendar}, and
0N/A * {@link Date}.
0N/A *
847N/A * <li> <b>Percent</b> - produces a literal {@code '%'}
0N/A * (<tt>'&#92;u0025'</tt>)
0N/A *
0N/A * <li> <b>Line Separator</b> - produces the platform-specific line separator
0N/A *
0N/A * </ol>
0N/A *
0N/A * <p> The following table summarizes the supported conversions. Conversions
847N/A * denoted by an upper-case character (i.e. {@code 'B'}, {@code 'H'},
847N/A * {@code 'S'}, {@code 'C'}, {@code 'X'}, {@code 'E'}, {@code 'G'},
847N/A * {@code 'A'}, and {@code 'T'}) are the same as those for the corresponding
0N/A * lower-case conversion characters except that the result is converted to
0N/A * upper case according to the rules of the prevailing {@link java.util.Locale
0N/A * Locale}. The result is equivalent to the following invocation of {@link
0N/A * String#toUpperCase()}
0N/A *
0N/A * <pre>
0N/A * out.toUpperCase() </pre>
0N/A *
0N/A * <table cellpadding=5 summary="genConv">
0N/A *
0N/A * <tr><th valign="bottom"> Conversion
0N/A * <th valign="bottom"> Argument Category
0N/A * <th valign="bottom"> Description
0N/A *
847N/A * <tr><td valign="top"> {@code 'b'}, {@code 'B'}
0N/A * <td valign="top"> general
847N/A * <td> If the argument <i>arg</i> is {@code null}, then the result is
847N/A * "{@code false}". If <i>arg</i> is a {@code boolean} or {@link
0N/A * Boolean}, then the result is the string returned by {@link
0N/A * String#valueOf(boolean) String.valueOf(arg)}. Otherwise, the result is
0N/A * "true".
0N/A *
847N/A * <tr><td valign="top"> {@code 'h'}, {@code 'H'}
0N/A * <td valign="top"> general
847N/A * <td> If the argument <i>arg</i> is {@code null}, then the result is
847N/A * "{@code null}". Otherwise, the result is obtained by invoking
847N/A * {@code Integer.toHexString(arg.hashCode())}.
847N/A *
847N/A * <tr><td valign="top"> {@code 's'}, {@code 'S'}
0N/A * <td valign="top"> general
847N/A * <td> If the argument <i>arg</i> is {@code null}, then the result is
847N/A * "{@code null}". If <i>arg</i> implements {@link Formattable}, then
0N/A * {@link Formattable#formatTo arg.formatTo} is invoked. Otherwise, the
847N/A * result is obtained by invoking {@code arg.toString()}.
847N/A *
847N/A * <tr><td valign="top">{@code 'c'}, {@code 'C'}
0N/A * <td valign="top"> character
0N/A * <td> The result is a Unicode character
0N/A *
847N/A * <tr><td valign="top">{@code 'd'}
0N/A * <td valign="top"> integral
0N/A * <td> The result is formatted as a decimal integer
0N/A *
847N/A * <tr><td valign="top">{@code 'o'}
0N/A * <td valign="top"> integral
0N/A * <td> The result is formatted as an octal integer
0N/A *
847N/A * <tr><td valign="top">{@code 'x'}, {@code 'X'}
0N/A * <td valign="top"> integral
0N/A * <td> The result is formatted as a hexadecimal integer
0N/A *
847N/A * <tr><td valign="top">{@code 'e'}, {@code 'E'}
0N/A * <td valign="top"> floating point
0N/A * <td> The result is formatted as a decimal number in computerized
0N/A * scientific notation
0N/A *
847N/A * <tr><td valign="top">{@code 'f'}
0N/A * <td valign="top"> floating point
0N/A * <td> The result is formatted as a decimal number
0N/A *
847N/A * <tr><td valign="top">{@code 'g'}, {@code 'G'}
0N/A * <td valign="top"> floating point
0N/A * <td> The result is formatted using computerized scientific notation or
0N/A * decimal format, depending on the precision and the value after rounding.
0N/A *
847N/A * <tr><td valign="top">{@code 'a'}, {@code 'A'}
0N/A * <td valign="top"> floating point
0N/A * <td> The result is formatted as a hexadecimal floating-point number with
0N/A * a significand and an exponent
0N/A *
847N/A * <tr><td valign="top">{@code 't'}, {@code 'T'}
0N/A * <td valign="top"> date/time
0N/A * <td> Prefix for date and time conversion characters. See <a
0N/A * href="#dt">Date/Time Conversions</a>.
0N/A *
847N/A * <tr><td valign="top">{@code '%'}
0N/A * <td valign="top"> percent
847N/A * <td> The result is a literal {@code '%'} (<tt>'&#92;u0025'</tt>)
847N/A *
847N/A * <tr><td valign="top">{@code 'n'}
0N/A * <td valign="top"> line separator
0N/A * <td> The result is the platform-specific line separator
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> Any characters not explicitly defined as conversions are illegal and are
0N/A * reserved for future extensions.
0N/A *
0N/A * <h4><a name="dt">Date/Time Conversions</a></h4>
0N/A *
0N/A * <p> The following date and time conversion suffix characters are defined for
847N/A * the {@code 't'} and {@code 'T'} conversions. The types are similar to but
847N/A * not completely identical to those defined by GNU {@code date} and POSIX
847N/A * {@code strftime(3c)}. Additional conversion types are provided to access
847N/A * Java-specific functionality (e.g. {@code 'L'} for milliseconds within the
0N/A * second).
0N/A *
0N/A * <p> The following conversion characters are used for formatting times:
0N/A *
0N/A * <table cellpadding=5 summary="time">
0N/A *
847N/A * <tr><td valign="top"> {@code 'H'}
0N/A * <td> Hour of the day for the 24-hour clock, formatted as two digits with
847N/A * a leading zero as necessary i.e. {@code 00 - 23}.
847N/A *
847N/A * <tr><td valign="top">{@code 'I'}
0N/A * <td> Hour for the 12-hour clock, formatted as two digits with a leading
847N/A * zero as necessary, i.e. {@code 01 - 12}.
847N/A *
847N/A * <tr><td valign="top">{@code 'k'}
847N/A * <td> Hour of the day for the 24-hour clock, i.e. {@code 0 - 23}.
847N/A *
847N/A * <tr><td valign="top">{@code 'l'}
847N/A * <td> Hour for the 12-hour clock, i.e. {@code 1 - 12}.
847N/A *
847N/A * <tr><td valign="top">{@code 'M'}
0N/A * <td> Minute within the hour formatted as two digits with a leading zero
847N/A * as necessary, i.e. {@code 00 - 59}.
847N/A *
847N/A * <tr><td valign="top">{@code 'S'}
0N/A * <td> Seconds within the minute, formatted as two digits with a leading
847N/A * zero as necessary, i.e. {@code 00 - 60} ("{@code 60}" is a special
0N/A * value required to support leap seconds).
0N/A *
847N/A * <tr><td valign="top">{@code 'L'}
0N/A * <td> Millisecond within the second formatted as three digits with
847N/A * leading zeros as necessary, i.e. {@code 000 - 999}.
847N/A *
847N/A * <tr><td valign="top">{@code 'N'}
0N/A * <td> Nanosecond within the second, formatted as nine digits with leading
847N/A * zeros as necessary, i.e. {@code 000000000 - 999999999}.
847N/A *
847N/A * <tr><td valign="top">{@code 'p'}
0N/A * <td> Locale-specific {@linkplain
0N/A * java.text.DateFormatSymbols#getAmPmStrings morning or afternoon} marker
847N/A * in lower case, e.g."{@code am}" or "{@code pm}". Use of the conversion
847N/A * prefix {@code 'T'} forces this output to upper case.
847N/A *
847N/A * <tr><td valign="top">{@code 'z'}
0N/A * <td> <a href="http://www.ietf.org/rfc/rfc0822.txt">RFC&nbsp;822</a>
847N/A * style numeric time zone offset from GMT, e.g. {@code -0800}. This
0N/A * value will be adjusted as necessary for Daylight Saving Time. For
847N/A * {@code long}, {@link Long}, and {@link Date} the time zone used is
847N/A * the {@linkplain TimeZone#getDefault() default time zone} for this
0N/A * instance of the Java virtual machine.
0N/A *
847N/A * <tr><td valign="top">{@code 'Z'}
0N/A * <td> A string representing the abbreviation for the time zone. This
0N/A * value will be adjusted as necessary for Daylight Saving Time. For
847N/A * {@code long}, {@link Long}, and {@link Date} the time zone used is
847N/A * the {@linkplain TimeZone#getDefault() default time zone} for this
0N/A * instance of the Java virtual machine. The Formatter's locale will
0N/A * supersede the locale of the argument (if any).
0N/A *
847N/A * <tr><td valign="top">{@code 's'}
0N/A * <td> Seconds since the beginning of the epoch starting at 1 January 1970
847N/A * {@code 00:00:00} UTC, i.e. {@code Long.MIN_VALUE/1000} to
847N/A * {@code Long.MAX_VALUE/1000}.
847N/A *
847N/A * <tr><td valign="top">{@code 'Q'}
0N/A * <td> Milliseconds since the beginning of the epoch starting at 1 January
847N/A * 1970 {@code 00:00:00} UTC, i.e. {@code Long.MIN_VALUE} to
847N/A * {@code Long.MAX_VALUE}.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> The following conversion characters are used for formatting dates:
0N/A *
0N/A * <table cellpadding=5 summary="date">
0N/A *
847N/A * <tr><td valign="top">{@code 'B'}
0N/A * <td> Locale-specific {@linkplain java.text.DateFormatSymbols#getMonths
847N/A * full month name}, e.g. {@code "January"}, {@code "February"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'b'}
0N/A * <td> Locale-specific {@linkplain
0N/A * java.text.DateFormatSymbols#getShortMonths abbreviated month name},
847N/A * e.g. {@code "Jan"}, {@code "Feb"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'h'}
847N/A * <td> Same as {@code 'b'}.
847N/A *
847N/A * <tr><td valign="top">{@code 'A'}
0N/A * <td> Locale-specific full name of the {@linkplain
0N/A * java.text.DateFormatSymbols#getWeekdays day of the week},
847N/A * e.g. {@code "Sunday"}, {@code "Monday"}
847N/A *
847N/A * <tr><td valign="top">{@code 'a'}
0N/A * <td> Locale-specific short name of the {@linkplain
0N/A * java.text.DateFormatSymbols#getShortWeekdays day of the week},
847N/A * e.g. {@code "Sun"}, {@code "Mon"}
847N/A *
847N/A * <tr><td valign="top">{@code 'C'}
847N/A * <td> Four-digit year divided by {@code 100}, formatted as two digits
847N/A * with leading zero as necessary, i.e. {@code 00 - 99}
847N/A *
847N/A * <tr><td valign="top">{@code 'Y'}
0N/A * <td> Year, formatted as at least four digits with leading zeros as
847N/A * necessary, e.g. {@code 0092} equals {@code 92} CE for the Gregorian
0N/A * calendar.
0N/A *
847N/A * <tr><td valign="top">{@code 'y'}
0N/A * <td> Last two digits of the year, formatted with leading zeros as
847N/A * necessary, i.e. {@code 00 - 99}.
847N/A *
847N/A * <tr><td valign="top">{@code 'j'}
0N/A * <td> Day of year, formatted as three digits with leading zeros as
847N/A * necessary, e.g. {@code 001 - 366} for the Gregorian calendar.
847N/A *
847N/A * <tr><td valign="top">{@code 'm'}
0N/A * <td> Month, formatted as two digits with leading zeros as necessary,
847N/A * i.e. {@code 01 - 13}.
847N/A *
847N/A * <tr><td valign="top">{@code 'd'}
0N/A * <td> Day of month, formatted as two digits with leading zeros as
847N/A * necessary, i.e. {@code 01 - 31}
847N/A *
847N/A * <tr><td valign="top">{@code 'e'}
847N/A * <td> Day of month, formatted as two digits, i.e. {@code 1 - 31}.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> The following conversion characters are used for formatting common
0N/A * date/time compositions.
0N/A *
0N/A * <table cellpadding=5 summary="composites">
0N/A *
847N/A * <tr><td valign="top">{@code 'R'}
847N/A * <td> Time formatted for the 24-hour clock as {@code "%tH:%tM"}
847N/A *
847N/A * <tr><td valign="top">{@code 'T'}
847N/A * <td> Time formatted for the 24-hour clock as {@code "%tH:%tM:%tS"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'r'}
847N/A * <td> Time formatted for the 12-hour clock as {@code "%tI:%tM:%tS %Tp"}.
847N/A * The location of the morning or afternoon marker ({@code '%Tp'}) may be
0N/A * locale-dependent.
0N/A *
847N/A * <tr><td valign="top">{@code 'D'}
847N/A * <td> Date formatted as {@code "%tm/%td/%ty"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'F'}
0N/A * <td> <a href="http://www.w3.org/TR/NOTE-datetime">ISO&nbsp;8601</a>
847N/A * complete date formatted as {@code "%tY-%tm-%td"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'c'}
847N/A * <td> Date and time formatted as {@code "%ta %tb %td %tT %tZ %tY"},
847N/A * e.g. {@code "Sun Jul 20 16:17:00 EDT 1969"}.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> Any characters not explicitly defined as date/time conversion suffixes
0N/A * are illegal and are reserved for future extensions.
0N/A *
0N/A * <h4> Flags </h4>
0N/A *
0N/A * <p> The following table summarizes the supported flags. <i>y</i> means the
0N/A * flag is supported for the indicated argument types.
0N/A *
0N/A * <table cellpadding=5 summary="genConv">
0N/A *
0N/A * <tr><th valign="bottom"> Flag <th valign="bottom"> General
0N/A * <th valign="bottom"> Character <th valign="bottom"> Integral
0N/A * <th valign="bottom"> Floating Point
0N/A * <th valign="bottom"> Date/Time
0N/A * <th valign="bottom"> Description
0N/A *
0N/A * <tr><td> '-' <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> y
0N/A * <td> The result will be left-justified.
0N/A *
0N/A * <tr><td> '#' <td align="center" valign="top"> y<sup>1</sup>
0N/A * <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> y<sup>3</sup>
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> -
0N/A * <td> The result should use a conversion-dependent alternate form
0N/A *
0N/A * <tr><td> '+' <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> y<sup>4</sup>
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> -
0N/A * <td> The result will always include a sign
0N/A *
0N/A * <tr><td> '&nbsp;&nbsp;' <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> y<sup>4</sup>
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> -
0N/A * <td> The result will include a leading space for positive values
0N/A *
0N/A * <tr><td> '0' <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> y
0N/A * <td align="center" valign="top"> -
0N/A * <td> The result will be zero-padded
0N/A *
0N/A * <tr><td> ',' <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> y<sup>2</sup>
0N/A * <td align="center" valign="top"> y<sup>5</sup>
0N/A * <td align="center" valign="top"> -
0N/A * <td> The result will include locale-specific {@linkplain
0N/A * java.text.DecimalFormatSymbols#getGroupingSeparator grouping separators}
0N/A *
0N/A * <tr><td> '(' <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> -
0N/A * <td align="center" valign="top"> y<sup>4</sup>
0N/A * <td align="center" valign="top"> y<sup>5</sup>
0N/A * <td align="center"> -
0N/A * <td> The result will enclose negative numbers in parentheses
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> <sup>1</sup> Depends on the definition of {@link Formattable}.
0N/A *
847N/A * <p> <sup>2</sup> For {@code 'd'} conversion only.
847N/A *
847N/A * <p> <sup>3</sup> For {@code 'o'}, {@code 'x'}, and {@code 'X'}
0N/A * conversions only.
0N/A *
847N/A * <p> <sup>4</sup> For {@code 'd'}, {@code 'o'}, {@code 'x'}, and
847N/A * {@code 'X'} conversions applied to {@link java.math.BigInteger BigInteger}
847N/A * or {@code 'd'} applied to {@code byte}, {@link Byte}, {@code short}, {@link
847N/A * Short}, {@code int} and {@link Integer}, {@code long}, and {@link Long}.
847N/A *
847N/A * <p> <sup>5</sup> For {@code 'e'}, {@code 'E'}, {@code 'f'},
847N/A * {@code 'g'}, and {@code 'G'} conversions only.
0N/A *
0N/A * <p> Any characters not explicitly defined as flags are illegal and are
0N/A * reserved for future extensions.
0N/A *
0N/A * <h4> Width </h4>
0N/A *
0N/A * <p> The width is the minimum number of characters to be written to the
0N/A * output. For the line separator conversion, width is not applicable; if it
0N/A * is provided, an exception will be thrown.
0N/A *
0N/A * <h4> Precision </h4>
0N/A *
0N/A * <p> For general argument types, the precision is the maximum number of
0N/A * characters to be written to the output.
0N/A *
847N/A * <p> For the floating-point conversions {@code 'e'}, {@code 'E'}, and
847N/A * {@code 'f'} the precision is the number of digits after the decimal
847N/A * separator. If the conversion is {@code 'g'} or {@code 'G'}, then the
0N/A * precision is the total number of digits in the resulting magnitude after
847N/A * rounding. If the conversion is {@code 'a'} or {@code 'A'}, then the
0N/A * precision must not be specified.
0N/A *
0N/A * <p> For character, integral, and date/time argument types and the percent
0N/A * and line separator conversions, the precision is not applicable; if a
0N/A * precision is provided, an exception will be thrown.
0N/A *
0N/A * <h4> Argument Index </h4>
0N/A *
0N/A * <p> The argument index is a decimal integer indicating the position of the
0N/A * argument in the argument list. The first argument is referenced by
847N/A * "{@code 1$}", the second by "{@code 2$}", etc.
0N/A *
0N/A * <p> Another way to reference arguments by position is to use the
847N/A * {@code '<'} (<tt>'&#92;u003c'</tt>) flag, which causes the argument for
0N/A * the previous format specifier to be re-used. For example, the following two
0N/A * statements would produce identical strings:
0N/A *
0N/A * <blockquote><pre>
0N/A * Calendar c = ...;
0N/A * String s1 = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
0N/A *
0N/A * String s2 = String.format("Duke's Birthday: %1$tm %&lt;te,%&lt;tY", c);
0N/A * </pre></blockquote>
0N/A *
0N/A * <hr>
0N/A * <h3><a name="detail">Details</a></h3>
0N/A *
0N/A * <p> This section is intended to provide behavioral details for formatting,
0N/A * including conditions and exceptions, supported data types, localization, and
0N/A * interactions between flags, conversions, and data types. For an overview of
0N/A * formatting concepts, refer to the <a href="#summary">Summary</a>
0N/A *
0N/A * <p> Any characters not explicitly defined as conversions, date/time
0N/A * conversion suffixes, or flags are illegal and are reserved for
0N/A * future extensions. Use of such a character in a format string will
0N/A * cause an {@link UnknownFormatConversionException} or {@link
0N/A * UnknownFormatFlagsException} to be thrown.
0N/A *
0N/A * <p> If the format specifier contains a width or precision with an invalid
0N/A * value or which is otherwise unsupported, then a {@link
0N/A * IllegalFormatWidthException} or {@link IllegalFormatPrecisionException}
0N/A * respectively will be thrown.
0N/A *
0N/A * <p> If a format specifier contains a conversion character that is not
0N/A * applicable to the corresponding argument, then an {@link
0N/A * IllegalFormatConversionException} will be thrown.
0N/A *
847N/A * <p> All specified exceptions may be thrown by any of the {@code format}
847N/A * methods of {@code Formatter} as well as by any {@code format} convenience
0N/A * methods such as {@link String#format(String,Object...) String.format} and
0N/A * {@link java.io.PrintStream#printf(String,Object...) PrintStream.printf}.
0N/A *
847N/A * <p> Conversions denoted by an upper-case character (i.e. {@code 'B'},
847N/A * {@code 'H'}, {@code 'S'}, {@code 'C'}, {@code 'X'}, {@code 'E'},
847N/A * {@code 'G'}, {@code 'A'}, and {@code 'T'}) are the same as those for the
0N/A * corresponding lower-case conversion characters except that the result is
0N/A * converted to upper case according to the rules of the prevailing {@link
0N/A * java.util.Locale Locale}. The result is equivalent to the following
0N/A * invocation of {@link String#toUpperCase()}
0N/A *
0N/A * <pre>
0N/A * out.toUpperCase() </pre>
0N/A *
0N/A * <h4><a name="dgen">General</a></h4>
0N/A *
0N/A * <p> The following general conversions may be applied to any argument type:
0N/A *
0N/A * <table cellpadding=5 summary="dgConv">
0N/A *
847N/A * <tr><td valign="top"> {@code 'b'}
0N/A * <td valign="top"> <tt>'&#92;u0062'</tt>
847N/A * <td> Produces either "{@code true}" or "{@code false}" as returned by
0N/A * {@link Boolean#toString(boolean)}.
0N/A *
847N/A * <p> If the argument is {@code null}, then the result is
847N/A * "{@code false}". If the argument is a {@code boolean} or {@link
0N/A * Boolean}, then the result is the string returned by {@link
0N/A * String#valueOf(boolean) String.valueOf()}. Otherwise, the result is
847N/A * "{@code true}".
847N/A *
847N/A * <p> If the {@code '#'} flag is given, then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'B'}
0N/A * <td valign="top"> <tt>'&#92;u0042'</tt>
847N/A * <td> The upper-case variant of {@code 'b'}.
847N/A *
847N/A * <tr><td valign="top"> {@code 'h'}
0N/A * <td valign="top"> <tt>'&#92;u0068'</tt>
0N/A * <td> Produces a string representing the hash code value of the object.
0N/A *
847N/A * <p> If the argument, <i>arg</i> is {@code null}, then the
847N/A * result is "{@code null}". Otherwise, the result is obtained
847N/A * by invoking {@code Integer.toHexString(arg.hashCode())}.
847N/A *
847N/A * <p> If the {@code '#'} flag is given, then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'H'}
0N/A * <td valign="top"> <tt>'&#92;u0048'</tt>
847N/A * <td> The upper-case variant of {@code 'h'}.
847N/A *
847N/A * <tr><td valign="top"> {@code 's'}
0N/A * <td valign="top"> <tt>'&#92;u0073'</tt>
0N/A * <td> Produces a string.
0N/A *
847N/A * <p> If the argument is {@code null}, then the result is
847N/A * "{@code null}". If the argument implements {@link Formattable}, then
0N/A * its {@link Formattable#formatTo formatTo} method is invoked.
0N/A * Otherwise, the result is obtained by invoking the argument's
847N/A * {@code toString()} method.
847N/A *
847N/A * <p> If the {@code '#'} flag is given and the argument is not a {@link
0N/A * Formattable} , then a {@link FormatFlagsConversionMismatchException}
0N/A * will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'S'}
0N/A * <td valign="top"> <tt>'&#92;u0053'</tt>
847N/A * <td> The upper-case variant of {@code 's'}.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> The following <a name="dFlags">flags</a> apply to general conversions:
0N/A *
0N/A * <table cellpadding=5 summary="dFlags">
0N/A *
847N/A * <tr><td valign="top"> {@code '-'}
0N/A * <td valign="top"> <tt>'&#92;u002d'</tt>
0N/A * <td> Left justifies the output. Spaces (<tt>'&#92;u0020'</tt>) will be
0N/A * added at the end of the converted value as required to fill the minimum
0N/A * width of the field. If the width is not provided, then a {@link
0N/A * MissingFormatWidthException} will be thrown. If this flag is not given
0N/A * then the output will be right-justified.
0N/A *
847N/A * <tr><td valign="top"> {@code '#'}
0N/A * <td valign="top"> <tt>'&#92;u0023'</tt>
0N/A * <td> Requires the output use an alternate form. The definition of the
0N/A * form is specified by the conversion.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> The <a name="genWidth">width</a> is the minimum number of characters to
0N/A * be written to the
0N/A * output. If the length of the converted value is less than the width then
847N/A * the output will be padded by <tt>'&nbsp;&nbsp;'</tt> (<tt>'&#92;u0020'</tt>)
0N/A * until the total number of characters equals the width. The padding is on
847N/A * the left by default. If the {@code '-'} flag is given, then the padding
0N/A * will be on the right. If the width is not specified then there is no
0N/A * minimum.
0N/A *
0N/A * <p> The precision is the maximum number of characters to be written to the
0N/A * output. The precision is applied before the width, thus the output will be
847N/A * truncated to {@code precision} characters even if the width is greater than
0N/A * the precision. If the precision is not specified then there is no explicit
0N/A * limit on the number of characters.
0N/A *
0N/A * <h4><a name="dchar">Character</a></h4>
0N/A *
847N/A * This conversion may be applied to {@code char} and {@link Character}. It
847N/A * may also be applied to the types {@code byte}, {@link Byte},
847N/A * {@code short}, and {@link Short}, {@code int} and {@link Integer} when
847N/A * {@link Character#isValidCodePoint} returns {@code true}. If it returns
847N/A * {@code false} then an {@link IllegalFormatCodePointException} will be
0N/A * thrown.
0N/A *
0N/A * <table cellpadding=5 summary="charConv">
0N/A *
847N/A * <tr><td valign="top"> {@code 'c'}
0N/A * <td valign="top"> <tt>'&#92;u0063'</tt>
0N/A * <td> Formats the argument as a Unicode character as described in <a
0N/A * href="../lang/Character.html#unicode">Unicode Character
847N/A * Representation</a>. This may be more than one 16-bit {@code char} in
0N/A * the case where the argument represents a supplementary character.
0N/A *
847N/A * <p> If the {@code '#'} flag is given, then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'C'}
0N/A * <td valign="top"> <tt>'&#92;u0043'</tt>
847N/A * <td> The upper-case variant of {@code 'c'}.
0N/A *
0N/A * </table>
0N/A *
847N/A * <p> The {@code '-'} flag defined for <a href="#dFlags">General
847N/A * conversions</a> applies. If the {@code '#'} flag is given, then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
0N/A * <p> The width is defined as for <a href="#genWidth">General conversions</a>.
0N/A *
0N/A * <p> The precision is not applicable. If the precision is specified then an
0N/A * {@link IllegalFormatPrecisionException} will be thrown.
0N/A *
0N/A * <h4><a name="dnum">Numeric</a></h4>
0N/A *
0N/A * <p> Numeric conversions are divided into the following categories:
0N/A *
0N/A * <ol>
0N/A *
0N/A * <li> <a href="#dnint"><b>Byte, Short, Integer, and Long</b></a>
0N/A *
0N/A * <li> <a href="#dnbint"><b>BigInteger</b></a>
0N/A *
0N/A * <li> <a href="#dndec"><b>Float and Double</b></a>
0N/A *
4226N/A * <li> <a href="#dnbdec"><b>BigDecimal</b></a>
0N/A *
0N/A * </ol>
0N/A *
0N/A * <p> Numeric types will be formatted according to the following algorithm:
0N/A *
0N/A * <p><b><a name="l10n algorithm"> Number Localization Algorithm</a></b>
0N/A *
0N/A * <p> After digits are obtained for the integer part, fractional part, and
0N/A * exponent (as appropriate for the data type), the following transformation
0N/A * is applied:
0N/A *
0N/A * <ol>
0N/A *
0N/A * <li> Each digit character <i>d</i> in the string is replaced by a
0N/A * locale-specific digit computed relative to the current locale's
0N/A * {@linkplain java.text.DecimalFormatSymbols#getZeroDigit() zero digit}
847N/A * <i>z</i>; that is <i>d&nbsp;-&nbsp;</i> {@code '0'}
0N/A * <i>&nbsp;+&nbsp;z</i>.
0N/A *
0N/A * <li> If a decimal separator is present, a locale-specific {@linkplain
0N/A * java.text.DecimalFormatSymbols#getDecimalSeparator decimal separator} is
0N/A * substituted.
0N/A *
847N/A * <li> If the {@code ','} (<tt>'&#92;u002c'</tt>)
0N/A * <a name="l10n group">flag</a> is given, then the locale-specific {@linkplain
0N/A * java.text.DecimalFormatSymbols#getGroupingSeparator grouping separator} is
0N/A * inserted by scanning the integer part of the string from least significant
0N/A * to most significant digits and inserting a separator at intervals defined by
0N/A * the locale's {@linkplain java.text.DecimalFormat#getGroupingSize() grouping
0N/A * size}.
0N/A *
847N/A * <li> If the {@code '0'} flag is given, then the locale-specific {@linkplain
0N/A * java.text.DecimalFormatSymbols#getZeroDigit() zero digits} are inserted
0N/A * after the sign character, if any, and before the first non-zero digit, until
0N/A * the length of the string is equal to the requested field width.
0N/A *
847N/A * <li> If the value is negative and the {@code '('} flag is given, then a
847N/A * {@code '('} (<tt>'&#92;u0028'</tt>) is prepended and a {@code ')'}
0N/A * (<tt>'&#92;u0029'</tt>) is appended.
0N/A *
0N/A * <li> If the value is negative (or floating-point negative zero) and
847N/A * {@code '('} flag is not given, then a {@code '-'} (<tt>'&#92;u002d'</tt>)
0N/A * is prepended.
0N/A *
847N/A * <li> If the {@code '+'} flag is given and the value is positive or zero (or
847N/A * floating-point positive zero), then a {@code '+'} (<tt>'&#92;u002b'</tt>)
0N/A * will be prepended.
0N/A *
0N/A * </ol>
0N/A *
0N/A * <p> If the value is NaN or positive infinity the literal strings "NaN" or
0N/A * "Infinity" respectively, will be output. If the value is negative infinity,
847N/A * then the output will be "(Infinity)" if the {@code '('} flag is given
0N/A * otherwise the output will be "-Infinity". These values are not localized.
0N/A *
0N/A * <p><a name="dnint"><b> Byte, Short, Integer, and Long </b></a>
0N/A *
847N/A * <p> The following conversions may be applied to {@code byte}, {@link Byte},
847N/A * {@code short}, {@link Short}, {@code int} and {@link Integer},
847N/A * {@code long}, and {@link Long}.
0N/A *
0N/A * <table cellpadding=5 summary="IntConv">
0N/A *
847N/A * <tr><td valign="top"> {@code 'd'}
0N/A * <td valign="top"> <tt>'&#92;u0054'</tt>
0N/A * <td> Formats the argument as a decimal integer. The <a
0N/A * href="#l10n algorithm">localization algorithm</a> is applied.
0N/A *
847N/A * <p> If the {@code '0'} flag is given and the value is negative, then
0N/A * the zero padding will occur after the sign.
0N/A *
847N/A * <p> If the {@code '#'} flag is given then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'o'}
0N/A * <td valign="top"> <tt>'&#92;u006f'</tt>
0N/A * <td> Formats the argument as an integer in base eight. No localization
0N/A * is applied.
0N/A *
0N/A * <p> If <i>x</i> is negative then the result will be an unsigned value
847N/A * generated by adding 2<sup>n</sup> to the value where {@code n} is the
847N/A * number of bits in the type as returned by the static {@code SIZE} field
0N/A * in the {@linkplain Byte#SIZE Byte}, {@linkplain Short#SIZE Short},
0N/A * {@linkplain Integer#SIZE Integer}, or {@linkplain Long#SIZE Long}
0N/A * classes as appropriate.
0N/A *
847N/A * <p> If the {@code '#'} flag is given then the output will always begin
847N/A * with the radix indicator {@code '0'}.
847N/A *
847N/A * <p> If the {@code '0'} flag is given then the output will be padded
0N/A * with leading zeros to the field width following any indication of sign.
0N/A *
847N/A * <p> If {@code '('}, {@code '+'}, '&nbsp&nbsp;', or {@code ','} flags
0N/A * are given then a {@link FormatFlagsConversionMismatchException} will be
0N/A * thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'x'}
0N/A * <td valign="top"> <tt>'&#92;u0078'</tt>
0N/A * <td> Formats the argument as an integer in base sixteen. No
0N/A * localization is applied.
0N/A *
0N/A * <p> If <i>x</i> is negative then the result will be an unsigned value
847N/A * generated by adding 2<sup>n</sup> to the value where {@code n} is the
847N/A * number of bits in the type as returned by the static {@code SIZE} field
0N/A * in the {@linkplain Byte#SIZE Byte}, {@linkplain Short#SIZE Short},
0N/A * {@linkplain Integer#SIZE Integer}, or {@linkplain Long#SIZE Long}
0N/A * classes as appropriate.
0N/A *
847N/A * <p> If the {@code '#'} flag is given then the output will always begin
847N/A * with the radix indicator {@code "0x"}.
847N/A *
847N/A * <p> If the {@code '0'} flag is given then the output will be padded to
0N/A * the field width with leading zeros after the radix indicator or sign (if
0N/A * present).
0N/A *
847N/A * <p> If {@code '('}, <tt>'&nbsp;&nbsp;'</tt>, {@code '+'}, or
847N/A * {@code ','} flags are given then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'X'}
0N/A * <td valign="top"> <tt>'&#92;u0058'</tt>
847N/A * <td> The upper-case variant of {@code 'x'}. The entire string
0N/A * representing the number will be converted to {@linkplain
847N/A * String#toUpperCase upper case} including the {@code 'x'} (if any) and
847N/A * all hexadecimal digits {@code 'a'} - {@code 'f'}
0N/A * (<tt>'&#92;u0061'</tt> - <tt>'&#92;u0066'</tt>).
0N/A *
0N/A * </table>
0N/A *
847N/A * <p> If the conversion is {@code 'o'}, {@code 'x'}, or {@code 'X'} and
847N/A * both the {@code '#'} and the {@code '0'} flags are given, then result will
847N/A * contain the radix indicator ({@code '0'} for octal and {@code "0x"} or
847N/A * {@code "0X"} for hexadecimal), some number of zeros (based on the width),
0N/A * and the value.
0N/A *
847N/A * <p> If the {@code '-'} flag is not given, then the space padding will occur
0N/A * before the sign.
0N/A *
0N/A * <p> The following <a name="intFlags">flags</a> apply to numeric integral
0N/A * conversions:
0N/A *
0N/A * <table cellpadding=5 summary="intFlags">
0N/A *
847N/A * <tr><td valign="top"> {@code '+'}
0N/A * <td valign="top"> <tt>'&#92;u002b'</tt>
0N/A * <td> Requires the output to include a positive sign for all positive
0N/A * numbers. If this flag is not given then only negative values will
0N/A * include a sign.
0N/A *
847N/A * <p> If both the {@code '+'} and <tt>'&nbsp;&nbsp;'</tt> flags are given
0N/A * then an {@link IllegalFormatFlagsException} will be thrown.
0N/A *
0N/A * <tr><td valign="top"> <tt>'&nbsp;&nbsp;'</tt>
0N/A * <td valign="top"> <tt>'&#92;u0020'</tt>
0N/A * <td> Requires the output to include a single extra space
0N/A * (<tt>'&#92;u0020'</tt>) for non-negative values.
0N/A *
847N/A * <p> If both the {@code '+'} and <tt>'&nbsp;&nbsp;'</tt> flags are given
0N/A * then an {@link IllegalFormatFlagsException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code '0'}
0N/A * <td valign="top"> <tt>'&#92;u0030'</tt>
0N/A * <td> Requires the output to be padded with leading {@linkplain
0N/A * java.text.DecimalFormatSymbols#getZeroDigit zeros} to the minimum field
0N/A * width following any sign or radix indicator except when converting NaN
0N/A * or infinity. If the width is not provided, then a {@link
0N/A * MissingFormatWidthException} will be thrown.
0N/A *
847N/A * <p> If both the {@code '-'} and {@code '0'} flags are given then an
0N/A * {@link IllegalFormatFlagsException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code ','}
0N/A * <td valign="top"> <tt>'&#92;u002c'</tt>
0N/A * <td> Requires the output to include the locale-specific {@linkplain
0N/A * java.text.DecimalFormatSymbols#getGroupingSeparator group separators} as
0N/A * described in the <a href="#l10n group">"group" section</a> of the
0N/A * localization algorithm.
0N/A *
847N/A * <tr><td valign="top"> {@code '('}
0N/A * <td valign="top"> <tt>'&#92;u0028'</tt>
847N/A * <td> Requires the output to prepend a {@code '('}
847N/A * (<tt>'&#92;u0028'</tt>) and append a {@code ')'}
0N/A * (<tt>'&#92;u0029'</tt>) to negative values.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> If no <a name="intdFlags">flags</a> are given the default formatting is
0N/A * as follows:
0N/A *
0N/A * <ul>
0N/A *
847N/A * <li> The output is right-justified within the {@code width}
847N/A *
847N/A * <li> Negative numbers begin with a {@code '-'} (<tt>'&#92;u002d'</tt>)
0N/A *
0N/A * <li> Positive numbers and zero do not include a sign or extra leading
0N/A * space
0N/A *
0N/A * <li> No grouping separators are included
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> The <a name="intWidth">width</a> is the minimum number of characters to
0N/A * be written to the output. This includes any signs, digits, grouping
0N/A * separators, radix indicator, and parentheses. If the length of the
0N/A * converted value is less than the width then the output will be padded by
0N/A * spaces (<tt>'&#92;u0020'</tt>) until the total number of characters equals
847N/A * width. The padding is on the left by default. If {@code '-'} flag is
0N/A * given then the padding will be on the right. If width is not specified then
0N/A * there is no minimum.
0N/A *
0N/A * <p> The precision is not applicable. If precision is specified then an
0N/A * {@link IllegalFormatPrecisionException} will be thrown.
0N/A *
0N/A * <p><a name="dnbint"><b> BigInteger </b></a>
0N/A *
0N/A * <p> The following conversions may be applied to {@link
0N/A * java.math.BigInteger}.
0N/A *
0N/A * <table cellpadding=5 summary="BIntConv">
0N/A *
847N/A * <tr><td valign="top"> {@code 'd'}
0N/A * <td valign="top"> <tt>'&#92;u0054'</tt>
0N/A * <td> Requires the output to be formatted as a decimal integer. The <a
0N/A * href="#l10n algorithm">localization algorithm</a> is applied.
0N/A *
847N/A * <p> If the {@code '#'} flag is given {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'o'}
0N/A * <td valign="top"> <tt>'&#92;u006f'</tt>
0N/A * <td> Requires the output to be formatted as an integer in base eight.
0N/A * No localization is applied.
0N/A *
0N/A * <p> If <i>x</i> is negative then the result will be a signed value
847N/A * beginning with {@code '-'} (<tt>'&#92;u002d'</tt>). Signed output is
0N/A * allowed for this type because unlike the primitive types it is not
0N/A * possible to create an unsigned equivalent without assuming an explicit
0N/A * data-type size.
0N/A *
847N/A * <p> If <i>x</i> is positive or zero and the {@code '+'} flag is given
847N/A * then the result will begin with {@code '+'} (<tt>'&#92;u002b'</tt>).
847N/A *
847N/A * <p> If the {@code '#'} flag is given then the output will always begin
847N/A * with {@code '0'} prefix.
847N/A *
847N/A * <p> If the {@code '0'} flag is given then the output will be padded
0N/A * with leading zeros to the field width following any indication of sign.
0N/A *
847N/A * <p> If the {@code ','} flag is given then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'x'}
0N/A * <td valign="top"> <tt>'&#92;u0078'</tt>
0N/A * <td> Requires the output to be formatted as an integer in base
0N/A * sixteen. No localization is applied.
0N/A *
0N/A * <p> If <i>x</i> is negative then the result will be a signed value
847N/A * beginning with {@code '-'} (<tt>'&#92;u002d'</tt>). Signed output is
0N/A * allowed for this type because unlike the primitive types it is not
0N/A * possible to create an unsigned equivalent without assuming an explicit
0N/A * data-type size.
0N/A *
847N/A * <p> If <i>x</i> is positive or zero and the {@code '+'} flag is given
847N/A * then the result will begin with {@code '+'} (<tt>'&#92;u002b'</tt>).
847N/A *
847N/A * <p> If the {@code '#'} flag is given then the output will always begin
847N/A * with the radix indicator {@code "0x"}.
847N/A *
847N/A * <p> If the {@code '0'} flag is given then the output will be padded to
0N/A * the field width with leading zeros after the radix indicator or sign (if
0N/A * present).
0N/A *
847N/A * <p> If the {@code ','} flag is given then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'X'}
0N/A * <td valign="top"> <tt>'&#92;u0058'</tt>
847N/A * <td> The upper-case variant of {@code 'x'}. The entire string
0N/A * representing the number will be converted to {@linkplain
847N/A * String#toUpperCase upper case} including the {@code 'x'} (if any) and
847N/A * all hexadecimal digits {@code 'a'} - {@code 'f'}
0N/A * (<tt>'&#92;u0061'</tt> - <tt>'&#92;u0066'</tt>).
0N/A *
0N/A * </table>
0N/A *
847N/A * <p> If the conversion is {@code 'o'}, {@code 'x'}, or {@code 'X'} and
847N/A * both the {@code '#'} and the {@code '0'} flags are given, then result will
847N/A * contain the base indicator ({@code '0'} for octal and {@code "0x"} or
847N/A * {@code "0X"} for hexadecimal), some number of zeros (based on the width),
0N/A * and the value.
0N/A *
847N/A * <p> If the {@code '0'} flag is given and the value is negative, then the
0N/A * zero padding will occur after the sign.
0N/A *
847N/A * <p> If the {@code '-'} flag is not given, then the space padding will occur
0N/A * before the sign.
0N/A *
0N/A * <p> All <a href="#intFlags">flags</a> defined for Byte, Short, Integer, and
0N/A * Long apply. The <a href="#intdFlags">default behavior</a> when no flags are
0N/A * given is the same as for Byte, Short, Integer, and Long.
0N/A *
0N/A * <p> The specification of <a href="#intWidth">width</a> is the same as
0N/A * defined for Byte, Short, Integer, and Long.
0N/A *
0N/A * <p> The precision is not applicable. If precision is specified then an
0N/A * {@link IllegalFormatPrecisionException} will be thrown.
0N/A *
0N/A * <p><a name="dndec"><b> Float and Double</b></a>
0N/A *
847N/A * <p> The following conversions may be applied to {@code float}, {@link
847N/A * Float}, {@code double} and {@link Double}.
0N/A *
0N/A * <table cellpadding=5 summary="floatConv">
0N/A *
847N/A * <tr><td valign="top"> {@code 'e'}
0N/A * <td valign="top"> <tt>'&#92;u0065'</tt>
0N/A * <td> Requires the output to be formatted using <a
0N/A * name="scientific">computerized scientific notation</a>. The <a
0N/A * href="#l10n algorithm">localization algorithm</a> is applied.
0N/A *
0N/A * <p> The formatting of the magnitude <i>m</i> depends upon its value.
0N/A *
0N/A * <p> If <i>m</i> is NaN or infinite, the literal strings "NaN" or
0N/A * "Infinity", respectively, will be output. These values are not
0N/A * localized.
0N/A *
0N/A * <p> If <i>m</i> is positive-zero or negative-zero, then the exponent
847N/A * will be {@code "+00"}.
0N/A *
0N/A * <p> Otherwise, the result is a string that represents the sign and
0N/A * magnitude (absolute value) of the argument. The formatting of the sign
0N/A * is described in the <a href="#l10n algorithm">localization
0N/A * algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
0N/A * value.
0N/A *
0N/A * <p> Let <i>n</i> be the unique integer such that 10<sup><i>n</i></sup>
0N/A * &lt;= <i>m</i> &lt; 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
0N/A * mathematically exact quotient of <i>m</i> and 10<sup><i>n</i></sup> so
0N/A * that 1 &lt;= <i>a</i> &lt; 10. The magnitude is then represented as the
0N/A * integer part of <i>a</i>, as a single decimal digit, followed by the
0N/A * decimal separator followed by decimal digits representing the fractional
847N/A * part of <i>a</i>, followed by the exponent symbol {@code 'e'}
0N/A * (<tt>'&#92;u0065'</tt>), followed by the sign of the exponent, followed
0N/A * by a representation of <i>n</i> as a decimal integer, as produced by the
0N/A * method {@link Long#toString(long, int)}, and zero-padded to include at
0N/A * least two digits.
0N/A *
0N/A * <p> The number of digits in the result for the fractional part of
0N/A * <i>m</i> or <i>a</i> is equal to the precision. If the precision is not
847N/A * specified then the default value is {@code 6}. If the precision is less
0N/A * than the number of digits which would appear after the decimal point in
0N/A * the string returned by {@link Float#toString(float)} or {@link
0N/A * Double#toString(double)} respectively, then the value will be rounded
0N/A * using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
0N/A * algorithm}. Otherwise, zeros may be appended to reach the precision.
0N/A * For a canonical representation of the value, use {@link
0N/A * Float#toString(float)} or {@link Double#toString(double)} as
0N/A * appropriate.
0N/A *
847N/A * <p>If the {@code ','} flag is given, then an {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'E'}
0N/A * <td valign="top"> <tt>'&#92;u0045'</tt>
847N/A * <td> The upper-case variant of {@code 'e'}. The exponent symbol
847N/A * will be {@code 'E'} (<tt>'&#92;u0045'</tt>).
847N/A *
847N/A * <tr><td valign="top"> {@code 'g'}
0N/A * <td valign="top"> <tt>'&#92;u0067'</tt>
0N/A * <td> Requires the output to be formatted in general scientific notation
0N/A * as described below. The <a href="#l10n algorithm">localization
0N/A * algorithm</a> is applied.
0N/A *
0N/A * <p> After rounding for the precision, the formatting of the resulting
0N/A * magnitude <i>m</i> depends on its value.
0N/A *
0N/A * <p> If <i>m</i> is greater than or equal to 10<sup>-4</sup> but less
0N/A * than 10<sup>precision</sup> then it is represented in <i><a
0N/A * href="#decimal">decimal format</a></i>.
0N/A *
0N/A * <p> If <i>m</i> is less than 10<sup>-4</sup> or greater than or equal to
0N/A * 10<sup>precision</sup>, then it is represented in <i><a
0N/A * href="#scientific">computerized scientific notation</a></i>.
0N/A *
0N/A * <p> The total number of significant digits in <i>m</i> is equal to the
0N/A * precision. If the precision is not specified, then the default value is
847N/A * {@code 6}. If the precision is {@code 0}, then it is taken to be
847N/A * {@code 1}.
847N/A *
847N/A * <p> If the {@code '#'} flag is given then an {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'G'}
0N/A * <td valign="top"> <tt>'&#92;u0047'</tt>
847N/A * <td> The upper-case variant of {@code 'g'}.
847N/A *
847N/A * <tr><td valign="top"> {@code 'f'}
0N/A * <td valign="top"> <tt>'&#92;u0066'</tt>
0N/A * <td> Requires the output to be formatted using <a name="decimal">decimal
0N/A * format</a>. The <a href="#l10n algorithm">localization algorithm</a> is
0N/A * applied.
0N/A *
0N/A * <p> The result is a string that represents the sign and magnitude
0N/A * (absolute value) of the argument. The formatting of the sign is
0N/A * described in the <a href="#l10n algorithm">localization
0N/A * algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
0N/A * value.
0N/A *
0N/A * <p> If <i>m</i> NaN or infinite, the literal strings "NaN" or
0N/A * "Infinity", respectively, will be output. These values are not
0N/A * localized.
0N/A *
0N/A * <p> The magnitude is formatted as the integer part of <i>m</i>, with no
0N/A * leading zeroes, followed by the decimal separator followed by one or
0N/A * more decimal digits representing the fractional part of <i>m</i>.
0N/A *
0N/A * <p> The number of digits in the result for the fractional part of
0N/A * <i>m</i> or <i>a</i> is equal to the precision. If the precision is not
847N/A * specified then the default value is {@code 6}. If the precision is less
0N/A * than the number of digits which would appear after the decimal point in
0N/A * the string returned by {@link Float#toString(float)} or {@link
0N/A * Double#toString(double)} respectively, then the value will be rounded
0N/A * using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
0N/A * algorithm}. Otherwise, zeros may be appended to reach the precision.
806N/A * For a canonical representation of the value, use {@link
0N/A * Float#toString(float)} or {@link Double#toString(double)} as
0N/A * appropriate.
0N/A *
847N/A * <tr><td valign="top"> {@code 'a'}
0N/A * <td valign="top"> <tt>'&#92;u0061'</tt>
0N/A * <td> Requires the output to be formatted in hexadecimal exponential
0N/A * form. No localization is applied.
0N/A *
0N/A * <p> The result is a string that represents the sign and magnitude
0N/A * (absolute value) of the argument <i>x</i>.
0N/A *
0N/A * <p> If <i>x</i> is negative or a negative-zero value then the result
847N/A * will begin with {@code '-'} (<tt>'&#92;u002d'</tt>).
0N/A *
0N/A * <p> If <i>x</i> is positive or a positive-zero value and the
847N/A * {@code '+'} flag is given then the result will begin with {@code '+'}
0N/A * (<tt>'&#92;u002b'</tt>).
0N/A *
0N/A * <p> The formatting of the magnitude <i>m</i> depends upon its value.
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li> If the value is NaN or infinite, the literal strings "NaN" or
0N/A * "Infinity", respectively, will be output.
0N/A *
0N/A * <li> If <i>m</i> is zero then it is represented by the string
847N/A * {@code "0x0.0p0"}.
847N/A *
847N/A * <li> If <i>m</i> is a {@code double} value with a normalized
0N/A * representation then substrings are used to represent the significand and
0N/A * exponent fields. The significand is represented by the characters
847N/A * {@code "0x1."} followed by the hexadecimal representation of the rest
0N/A * of the significand as a fraction. The exponent is represented by
847N/A * {@code 'p'} (<tt>'&#92;u0070'</tt>) followed by a decimal string of the
0N/A * unbiased exponent as if produced by invoking {@link
0N/A * Integer#toString(int) Integer.toString} on the exponent value.
0N/A *
847N/A * <li> If <i>m</i> is a {@code double} value with a subnormal
0N/A * representation then the significand is represented by the characters
847N/A * {@code '0x0.'} followed by the hexadecimal representation of the rest
0N/A * of the significand as a fraction. The exponent is represented by
847N/A * {@code 'p-1022'}. Note that there must be at least one nonzero digit
0N/A * in a subnormal significand.
0N/A *
0N/A * </ul>
0N/A *
847N/A * <p> If the {@code '('} or {@code ','} flags are given, then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'A'}
0N/A * <td valign="top"> <tt>'&#92;u0041'</tt>
847N/A * <td> The upper-case variant of {@code 'a'}. The entire string
0N/A * representing the number will be converted to upper case including the
847N/A * {@code 'x'} (<tt>'&#92;u0078'</tt>) and {@code 'p'}
847N/A * (<tt>'&#92;u0070'</tt> and all hexadecimal digits {@code 'a'} -
847N/A * {@code 'f'} (<tt>'&#92;u0061'</tt> - <tt>'&#92;u0066'</tt>).
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> All <a href="#intFlags">flags</a> defined for Byte, Short, Integer, and
0N/A * Long apply.
0N/A *
847N/A * <p> If the {@code '#'} flag is given, then the decimal separator will
0N/A * always be present.
0N/A *
0N/A * <p> If no <a name="floatdFlags">flags</a> are given the default formatting
0N/A * is as follows:
0N/A *
0N/A * <ul>
0N/A *
847N/A * <li> The output is right-justified within the {@code width}
847N/A *
847N/A * <li> Negative numbers begin with a {@code '-'}
0N/A *
0N/A * <li> Positive numbers and positive zero do not include a sign or extra
0N/A * leading space
0N/A *
0N/A * <li> No grouping separators are included
0N/A *
0N/A * <li> The decimal separator will only appear if a digit follows it
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> The <a name="floatDWidth">width</a> is the minimum number of characters
0N/A * to be written to the output. This includes any signs, digits, grouping
0N/A * separators, decimal separators, exponential symbol, radix indicator,
0N/A * parentheses, and strings representing infinity and NaN as applicable. If
0N/A * the length of the converted value is less than the width then the output
0N/A * will be padded by spaces (<tt>'&#92;u0020'</tt>) until the total number of
0N/A * characters equals width. The padding is on the left by default. If the
847N/A * {@code '-'} flag is given then the padding will be on the right. If width
0N/A * is not specified then there is no minimum.
0N/A *
847N/A * <p> If the <a name="floatDPrec">conversion</a> is {@code 'e'},
847N/A * {@code 'E'} or {@code 'f'}, then the precision is the number of digits
0N/A * after the decimal separator. If the precision is not specified, then it is
847N/A * assumed to be {@code 6}.
847N/A *
847N/A * <p> If the conversion is {@code 'g'} or {@code 'G'}, then the precision is
0N/A * the total number of significant digits in the resulting magnitude after
0N/A * rounding. If the precision is not specified, then the default value is
847N/A * {@code 6}. If the precision is {@code 0}, then it is taken to be
847N/A * {@code 1}.
847N/A *
847N/A * <p> If the conversion is {@code 'a'} or {@code 'A'}, then the precision
0N/A * is the number of hexadecimal digits after the decimal separator. If the
0N/A * precision is not provided, then all of the digits as returned by {@link
0N/A * Double#toHexString(double)} will be output.
0N/A *
4226N/A * <p><a name="dnbdec"><b> BigDecimal </b></a>
0N/A *
0N/A * <p> The following conversions may be applied {@link java.math.BigDecimal
0N/A * BigDecimal}.
0N/A *
0N/A * <table cellpadding=5 summary="floatConv">
0N/A *
847N/A * <tr><td valign="top"> {@code 'e'}
0N/A * <td valign="top"> <tt>'&#92;u0065'</tt>
0N/A * <td> Requires the output to be formatted using <a
4226N/A * name="bscientific">computerized scientific notation</a>. The <a
0N/A * href="#l10n algorithm">localization algorithm</a> is applied.
0N/A *
0N/A * <p> The formatting of the magnitude <i>m</i> depends upon its value.
0N/A *
0N/A * <p> If <i>m</i> is positive-zero or negative-zero, then the exponent
847N/A * will be {@code "+00"}.
0N/A *
0N/A * <p> Otherwise, the result is a string that represents the sign and
0N/A * magnitude (absolute value) of the argument. The formatting of the sign
0N/A * is described in the <a href="#l10n algorithm">localization
0N/A * algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
0N/A * value.
0N/A *
0N/A * <p> Let <i>n</i> be the unique integer such that 10<sup><i>n</i></sup>
0N/A * &lt;= <i>m</i> &lt; 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
0N/A * mathematically exact quotient of <i>m</i> and 10<sup><i>n</i></sup> so
0N/A * that 1 &lt;= <i>a</i> &lt; 10. The magnitude is then represented as the
0N/A * integer part of <i>a</i>, as a single decimal digit, followed by the
0N/A * decimal separator followed by decimal digits representing the fractional
847N/A * part of <i>a</i>, followed by the exponent symbol {@code 'e'}
0N/A * (<tt>'&#92;u0065'</tt>), followed by the sign of the exponent, followed
0N/A * by a representation of <i>n</i> as a decimal integer, as produced by the
0N/A * method {@link Long#toString(long, int)}, and zero-padded to include at
0N/A * least two digits.
0N/A *
0N/A * <p> The number of digits in the result for the fractional part of
0N/A * <i>m</i> or <i>a</i> is equal to the precision. If the precision is not
847N/A * specified then the default value is {@code 6}. If the precision is
3770N/A * less than the number of digits to the right of the decimal point then
3770N/A * the value will be rounded using the
3770N/A * {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
0N/A * algorithm}. Otherwise, zeros may be appended to reach the precision.
0N/A * For a canonical representation of the value, use {@link
0N/A * BigDecimal#toString()}.
0N/A *
847N/A * <p> If the {@code ','} flag is given, then an {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'E'}
0N/A * <td valign="top"> <tt>'&#92;u0045'</tt>
847N/A * <td> The upper-case variant of {@code 'e'}. The exponent symbol
847N/A * will be {@code 'E'} (<tt>'&#92;u0045'</tt>).
847N/A *
847N/A * <tr><td valign="top"> {@code 'g'}
0N/A * <td valign="top"> <tt>'&#92;u0067'</tt>
0N/A * <td> Requires the output to be formatted in general scientific notation
0N/A * as described below. The <a href="#l10n algorithm">localization
0N/A * algorithm</a> is applied.
0N/A *
0N/A * <p> After rounding for the precision, the formatting of the resulting
0N/A * magnitude <i>m</i> depends on its value.
0N/A *
0N/A * <p> If <i>m</i> is greater than or equal to 10<sup>-4</sup> but less
0N/A * than 10<sup>precision</sup> then it is represented in <i><a
4226N/A * href="#bdecimal">decimal format</a></i>.
0N/A *
0N/A * <p> If <i>m</i> is less than 10<sup>-4</sup> or greater than or equal to
0N/A * 10<sup>precision</sup>, then it is represented in <i><a
4226N/A * href="#bscientific">computerized scientific notation</a></i>.
0N/A *
0N/A * <p> The total number of significant digits in <i>m</i> is equal to the
0N/A * precision. If the precision is not specified, then the default value is
847N/A * {@code 6}. If the precision is {@code 0}, then it is taken to be
847N/A * {@code 1}.
847N/A *
847N/A * <p> If the {@code '#'} flag is given then an {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
847N/A * <tr><td valign="top"> {@code 'G'}
0N/A * <td valign="top"> <tt>'&#92;u0047'</tt>
847N/A * <td> The upper-case variant of {@code 'g'}.
847N/A *
847N/A * <tr><td valign="top"> {@code 'f'}
0N/A * <td valign="top"> <tt>'&#92;u0066'</tt>
4226N/A * <td> Requires the output to be formatted using <a name="bdecimal">decimal
0N/A * format</a>. The <a href="#l10n algorithm">localization algorithm</a> is
0N/A * applied.
0N/A *
0N/A * <p> The result is a string that represents the sign and magnitude
0N/A * (absolute value) of the argument. The formatting of the sign is
0N/A * described in the <a href="#l10n algorithm">localization
0N/A * algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
0N/A * value.
0N/A *
0N/A * <p> The magnitude is formatted as the integer part of <i>m</i>, with no
0N/A * leading zeroes, followed by the decimal separator followed by one or
0N/A * more decimal digits representing the fractional part of <i>m</i>.
0N/A *
0N/A * <p> The number of digits in the result for the fractional part of
3770N/A * <i>m</i> or <i>a</i> is equal to the precision. If the precision is not
847N/A * specified then the default value is {@code 6}. If the precision is
3770N/A * less than the number of digits to the right of the decimal point
3770N/A * then the value will be rounded using the
3770N/A * {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
0N/A * algorithm}. Otherwise, zeros may be appended to reach the precision.
0N/A * For a canonical representation of the value, use {@link
0N/A * BigDecimal#toString()}.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> All <a href="#intFlags">flags</a> defined for Byte, Short, Integer, and
0N/A * Long apply.
0N/A *
847N/A * <p> If the {@code '#'} flag is given, then the decimal separator will
0N/A * always be present.
0N/A *
0N/A * <p> The <a href="#floatdFlags">default behavior</a> when no flags are
0N/A * given is the same as for Float and Double.
0N/A *
0N/A * <p> The specification of <a href="#floatDWidth">width</a> and <a
0N/A * href="#floatDPrec">precision</a> is the same as defined for Float and
0N/A * Double.
0N/A *
0N/A * <h4><a name="ddt">Date/Time</a></h4>
0N/A *
847N/A * <p> This conversion may be applied to {@code long}, {@link Long}, {@link
0N/A * Calendar}, and {@link Date}.
0N/A *
0N/A * <table cellpadding=5 summary="DTConv">
0N/A *
847N/A * <tr><td valign="top"> {@code 't'}
0N/A * <td valign="top"> <tt>'&#92;u0074'</tt>
0N/A * <td> Prefix for date and time conversion characters.
847N/A * <tr><td valign="top"> {@code 'T'}
0N/A * <td valign="top"> <tt>'&#92;u0054'</tt>
847N/A * <td> The upper-case variant of {@code 't'}.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> The following date and time conversion character suffixes are defined
847N/A * for the {@code 't'} and {@code 'T'} conversions. The types are similar to
847N/A * but not completely identical to those defined by GNU {@code date} and
847N/A * POSIX {@code strftime(3c)}. Additional conversion types are provided to
847N/A * access Java-specific functionality (e.g. {@code 'L'} for milliseconds
0N/A * within the second).
0N/A *
0N/A * <p> The following conversion characters are used for formatting times:
0N/A *
0N/A * <table cellpadding=5 summary="time">
0N/A *
847N/A * <tr><td valign="top"> {@code 'H'}
0N/A * <td valign="top"> <tt>'&#92;u0048'</tt>
0N/A * <td> Hour of the day for the 24-hour clock, formatted as two digits with
847N/A * a leading zero as necessary i.e. {@code 00 - 23}. {@code 00}
0N/A * corresponds to midnight.
0N/A *
847N/A * <tr><td valign="top">{@code 'I'}
0N/A * <td valign="top"> <tt>'&#92;u0049'</tt>
0N/A * <td> Hour for the 12-hour clock, formatted as two digits with a leading
847N/A * zero as necessary, i.e. {@code 01 - 12}. {@code 01} corresponds to
0N/A * one o'clock (either morning or afternoon).
0N/A *
847N/A * <tr><td valign="top">{@code 'k'}
0N/A * <td valign="top"> <tt>'&#92;u006b'</tt>
847N/A * <td> Hour of the day for the 24-hour clock, i.e. {@code 0 - 23}.
847N/A * {@code 0} corresponds to midnight.
847N/A *
847N/A * <tr><td valign="top">{@code 'l'}
0N/A * <td valign="top"> <tt>'&#92;u006c'</tt>
847N/A * <td> Hour for the 12-hour clock, i.e. {@code 1 - 12}. {@code 1}
0N/A * corresponds to one o'clock (either morning or afternoon).
0N/A *
847N/A * <tr><td valign="top">{@code 'M'}
0N/A * <td valign="top"> <tt>'&#92;u004d'</tt>
0N/A * <td> Minute within the hour formatted as two digits with a leading zero
847N/A * as necessary, i.e. {@code 00 - 59}.
847N/A *
847N/A * <tr><td valign="top">{@code 'S'}
0N/A * <td valign="top"> <tt>'&#92;u0053'</tt>
0N/A * <td> Seconds within the minute, formatted as two digits with a leading
847N/A * zero as necessary, i.e. {@code 00 - 60} ("{@code 60}" is a special
0N/A * value required to support leap seconds).
0N/A *
847N/A * <tr><td valign="top">{@code 'L'}
0N/A * <td valign="top"> <tt>'&#92;u004c'</tt>
0N/A * <td> Millisecond within the second formatted as three digits with
847N/A * leading zeros as necessary, i.e. {@code 000 - 999}.
847N/A *
847N/A * <tr><td valign="top">{@code 'N'}
0N/A * <td valign="top"> <tt>'&#92;u004e'</tt>
0N/A * <td> Nanosecond within the second, formatted as nine digits with leading
847N/A * zeros as necessary, i.e. {@code 000000000 - 999999999}. The precision
0N/A * of this value is limited by the resolution of the underlying operating
0N/A * system or hardware.
0N/A *
847N/A * <tr><td valign="top">{@code 'p'}
0N/A * <td valign="top"> <tt>'&#92;u0070'</tt>
0N/A * <td> Locale-specific {@linkplain
0N/A * java.text.DateFormatSymbols#getAmPmStrings morning or afternoon} marker
847N/A * in lower case, e.g."{@code am}" or "{@code pm}". Use of the
847N/A * conversion prefix {@code 'T'} forces this output to upper case. (Note
847N/A * that {@code 'p'} produces lower-case output. This is different from
847N/A * GNU {@code date} and POSIX {@code strftime(3c)} which produce
0N/A * upper-case output.)
0N/A *
847N/A * <tr><td valign="top">{@code 'z'}
0N/A * <td valign="top"> <tt>'&#92;u007a'</tt>
0N/A * <td> <a href="http://www.ietf.org/rfc/rfc0822.txt">RFC&nbsp;822</a>
847N/A * style numeric time zone offset from GMT, e.g. {@code -0800}. This
0N/A * value will be adjusted as necessary for Daylight Saving Time. For
847N/A * {@code long}, {@link Long}, and {@link Date} the time zone used is
847N/A * the {@linkplain TimeZone#getDefault() default time zone} for this
0N/A * instance of the Java virtual machine.
0N/A *
847N/A * <tr><td valign="top">{@code 'Z'}
3107N/A * <td valign="top"> <tt>'&#92;u005a'</tt>
0N/A * <td> A string representing the abbreviation for the time zone. This
0N/A * value will be adjusted as necessary for Daylight Saving Time. For
847N/A * {@code long}, {@link Long}, and {@link Date} the time zone used is
847N/A * the {@linkplain TimeZone#getDefault() default time zone} for this
0N/A * instance of the Java virtual machine. The Formatter's locale will
0N/A * supersede the locale of the argument (if any).
0N/A *
847N/A * <tr><td valign="top">{@code 's'}
0N/A * <td valign="top"> <tt>'&#92;u0073'</tt>
0N/A * <td> Seconds since the beginning of the epoch starting at 1 January 1970
847N/A * {@code 00:00:00} UTC, i.e. {@code Long.MIN_VALUE/1000} to
847N/A * {@code Long.MAX_VALUE/1000}.
847N/A *
847N/A * <tr><td valign="top">{@code 'Q'}
0N/A * <td valign="top"> <tt>'&#92;u004f'</tt>
0N/A * <td> Milliseconds since the beginning of the epoch starting at 1 January
847N/A * 1970 {@code 00:00:00} UTC, i.e. {@code Long.MIN_VALUE} to
847N/A * {@code Long.MAX_VALUE}. The precision of this value is limited by
0N/A * the resolution of the underlying operating system or hardware.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> The following conversion characters are used for formatting dates:
0N/A *
0N/A * <table cellpadding=5 summary="date">
0N/A *
847N/A * <tr><td valign="top">{@code 'B'}
0N/A * <td valign="top"> <tt>'&#92;u0042'</tt>
0N/A * <td> Locale-specific {@linkplain java.text.DateFormatSymbols#getMonths
847N/A * full month name}, e.g. {@code "January"}, {@code "February"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'b'}
0N/A * <td valign="top"> <tt>'&#92;u0062'</tt>
0N/A * <td> Locale-specific {@linkplain
0N/A * java.text.DateFormatSymbols#getShortMonths abbreviated month name},
847N/A * e.g. {@code "Jan"}, {@code "Feb"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'h'}
0N/A * <td valign="top"> <tt>'&#92;u0068'</tt>
847N/A * <td> Same as {@code 'b'}.
847N/A *
847N/A * <tr><td valign="top">{@code 'A'}
0N/A * <td valign="top"> <tt>'&#92;u0041'</tt>
0N/A * <td> Locale-specific full name of the {@linkplain
0N/A * java.text.DateFormatSymbols#getWeekdays day of the week},
847N/A * e.g. {@code "Sunday"}, {@code "Monday"}
847N/A *
847N/A * <tr><td valign="top">{@code 'a'}
0N/A * <td valign="top"> <tt>'&#92;u0061'</tt>
0N/A * <td> Locale-specific short name of the {@linkplain
0N/A * java.text.DateFormatSymbols#getShortWeekdays day of the week},
847N/A * e.g. {@code "Sun"}, {@code "Mon"}
847N/A *
847N/A * <tr><td valign="top">{@code 'C'}
0N/A * <td valign="top"> <tt>'&#92;u0043'</tt>
847N/A * <td> Four-digit year divided by {@code 100}, formatted as two digits
847N/A * with leading zero as necessary, i.e. {@code 00 - 99}
847N/A *
847N/A * <tr><td valign="top">{@code 'Y'}
0N/A * <td valign="top"> <tt>'&#92;u0059'</tt> <td> Year, formatted to at least
847N/A * four digits with leading zeros as necessary, e.g. {@code 0092} equals
847N/A * {@code 92} CE for the Gregorian calendar.
847N/A *
847N/A * <tr><td valign="top">{@code 'y'}
0N/A * <td valign="top"> <tt>'&#92;u0079'</tt>
0N/A * <td> Last two digits of the year, formatted with leading zeros as
847N/A * necessary, i.e. {@code 00 - 99}.
847N/A *
847N/A * <tr><td valign="top">{@code 'j'}
0N/A * <td valign="top"> <tt>'&#92;u006a'</tt>
0N/A * <td> Day of year, formatted as three digits with leading zeros as
847N/A * necessary, e.g. {@code 001 - 366} for the Gregorian calendar.
847N/A * {@code 001} corresponds to the first day of the year.
847N/A *
847N/A * <tr><td valign="top">{@code 'm'}
0N/A * <td valign="top"> <tt>'&#92;u006d'</tt>
0N/A * <td> Month, formatted as two digits with leading zeros as necessary,
847N/A * i.e. {@code 01 - 13}, where "{@code 01}" is the first month of the
847N/A * year and ("{@code 13}" is a special value required to support lunar
0N/A * calendars).
0N/A *
847N/A * <tr><td valign="top">{@code 'd'}
0N/A * <td valign="top"> <tt>'&#92;u0064'</tt>
0N/A * <td> Day of month, formatted as two digits with leading zeros as
847N/A * necessary, i.e. {@code 01 - 31}, where "{@code 01}" is the first day
0N/A * of the month.
0N/A *
847N/A * <tr><td valign="top">{@code 'e'}
0N/A * <td valign="top"> <tt>'&#92;u0065'</tt>
847N/A * <td> Day of month, formatted as two digits, i.e. {@code 1 - 31} where
847N/A * "{@code 1}" is the first day of the month.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> The following conversion characters are used for formatting common
0N/A * date/time compositions.
0N/A *
0N/A * <table cellpadding=5 summary="composites">
0N/A *
847N/A * <tr><td valign="top">{@code 'R'}
0N/A * <td valign="top"> <tt>'&#92;u0052'</tt>
847N/A * <td> Time formatted for the 24-hour clock as {@code "%tH:%tM"}
847N/A *
847N/A * <tr><td valign="top">{@code 'T'}
0N/A * <td valign="top"> <tt>'&#92;u0054'</tt>
847N/A * <td> Time formatted for the 24-hour clock as {@code "%tH:%tM:%tS"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'r'}
0N/A * <td valign="top"> <tt>'&#92;u0072'</tt>
847N/A * <td> Time formatted for the 12-hour clock as {@code "%tI:%tM:%tS
847N/A * %Tp"}. The location of the morning or afternoon marker
847N/A * ({@code '%Tp'}) may be locale-dependent.
847N/A *
847N/A * <tr><td valign="top">{@code 'D'}
0N/A * <td valign="top"> <tt>'&#92;u0044'</tt>
847N/A * <td> Date formatted as {@code "%tm/%td/%ty"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'F'}
0N/A * <td valign="top"> <tt>'&#92;u0046'</tt>
0N/A * <td> <a href="http://www.w3.org/TR/NOTE-datetime">ISO&nbsp;8601</a>
847N/A * complete date formatted as {@code "%tY-%tm-%td"}.
847N/A *
847N/A * <tr><td valign="top">{@code 'c'}
0N/A * <td valign="top"> <tt>'&#92;u0063'</tt>
847N/A * <td> Date and time formatted as {@code "%ta %tb %td %tT %tZ %tY"},
847N/A * e.g. {@code "Sun Jul 20 16:17:00 EDT 1969"}.
0N/A *
0N/A * </table>
0N/A *
847N/A * <p> The {@code '-'} flag defined for <a href="#dFlags">General
847N/A * conversions</a> applies. If the {@code '#'} flag is given, then a {@link
0N/A * FormatFlagsConversionMismatchException} will be thrown.
0N/A *
0N/A * <p> The <a name="dtWidth">width</a> is the minimum number of characters to
0N/A * be written to the output. If the length of the converted value is less than
847N/A * the {@code width} then the output will be padded by spaces
0N/A * (<tt>'&#92;u0020'</tt>) until the total number of characters equals width.
847N/A * The padding is on the left by default. If the {@code '-'} flag is given
0N/A * then the padding will be on the right. If width is not specified then there
0N/A * is no minimum.
0N/A *
0N/A * <p> The precision is not applicable. If the precision is specified then an
0N/A * {@link IllegalFormatPrecisionException} will be thrown.
0N/A *
0N/A * <h4><a name="dper">Percent</a></h4>
0N/A *
0N/A * <p> The conversion does not correspond to any argument.
0N/A *
0N/A * <table cellpadding=5 summary="DTConv">
0N/A *
847N/A * <tr><td valign="top">{@code '%'}
847N/A * <td> The result is a literal {@code '%'} (<tt>'&#92;u0025'</tt>)
0N/A *
0N/A * <p> The <a name="dtWidth">width</a> is the minimum number of characters to
847N/A * be written to the output including the {@code '%'}. If the length of the
847N/A * converted value is less than the {@code width} then the output will be
0N/A * padded by spaces (<tt>'&#92;u0020'</tt>) until the total number of
0N/A * characters equals width. The padding is on the left. If width is not
847N/A * specified then just the {@code '%'} is output.
847N/A *
847N/A * <p> The {@code '-'} flag defined for <a href="#dFlags">General
0N/A * conversions</a> applies. If any other flags are provided, then a
0N/A * {@link FormatFlagsConversionMismatchException} will be thrown.
0N/A *
0N/A * <p> The precision is not applicable. If the precision is specified an
0N/A * {@link IllegalFormatPrecisionException} will be thrown.
0N/A *
0N/A * </table>
0N/A *
0N/A * <h4><a name="dls">Line Separator</a></h4>
0N/A *
0N/A * <p> The conversion does not correspond to any argument.
0N/A *
0N/A * <table cellpadding=5 summary="DTConv">
0N/A *
847N/A * <tr><td valign="top">{@code 'n'}
0N/A * <td> the platform-specific line separator as returned by {@link
0N/A * System#getProperty System.getProperty("line.separator")}.
0N/A *
0N/A * </table>
0N/A *
0N/A * <p> Flags, width, and precision are not applicable. If any are provided an
0N/A * {@link IllegalFormatFlagsException}, {@link IllegalFormatWidthException},
0N/A * and {@link IllegalFormatPrecisionException}, respectively will be thrown.
0N/A *
0N/A * <h4><a name="dpos">Argument Index</a></h4>
0N/A *
0N/A * <p> Format specifiers can reference arguments in three ways:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li> <i>Explicit indexing</i> is used when the format specifier contains an
0N/A * argument index. The argument index is a decimal integer indicating the
0N/A * position of the argument in the argument list. The first argument is
847N/A * referenced by "{@code 1$}", the second by "{@code 2$}", etc. An argument
0N/A * may be referenced more than once.
0N/A *
0N/A * <p> For example:
0N/A *
0N/A * <blockquote><pre>
0N/A * formatter.format("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
0N/A * "a", "b", "c", "d")
0N/A * // -&gt; "d c b a d c b a"
0N/A * </pre></blockquote>
0N/A *
0N/A * <li> <i>Relative indexing</i> is used when the format specifier contains a
847N/A * {@code '<'} (<tt>'&#92;u003c'</tt>) flag which causes the argument for
0N/A * the previous format specifier to be re-used. If there is no previous
0N/A * argument, then a {@link MissingFormatArgumentException} is thrown.
0N/A *
0N/A * <blockquote><pre>
0N/A * formatter.format("%s %s %&lt;s %&lt;s", "a", "b", "c", "d")
0N/A * // -&gt; "a b b b"
0N/A * // "c" and "d" are ignored because they are not referenced
0N/A * </pre></blockquote>
0N/A *
0N/A * <li> <i>Ordinary indexing</i> is used when the format specifier contains
847N/A * neither an argument index nor a {@code '<'} flag. Each format specifier
0N/A * which uses ordinary indexing is assigned a sequential implicit index into
0N/A * argument list which is independent of the indices used by explicit or
0N/A * relative indexing.
0N/A *
0N/A * <blockquote><pre>
0N/A * formatter.format("%s %s %s %s", "a", "b", "c", "d")
0N/A * // -&gt; "a b c d"
0N/A * </pre></blockquote>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> It is possible to have a format string which uses all forms of indexing,
0N/A * for example:
0N/A *
0N/A * <blockquote><pre>
0N/A * formatter.format("%2$s %s %&lt;s %s", "a", "b", "c", "d")
0N/A * // -&gt; "b a a b"
0N/A * // "c" and "d" are ignored because they are not referenced
0N/A * </pre></blockquote>
0N/A *
0N/A * <p> The maximum number of arguments is limited by the maximum dimension of a
4008N/A * Java array as defined by
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>.
4008N/A * If the argument index is does not correspond to an
0N/A * available argument, then a {@link MissingFormatArgumentException} is thrown.
0N/A *
0N/A * <p> If there are more arguments than format specifiers, the extra arguments
0N/A * are ignored.
0N/A *
847N/A * <p> Unless otherwise specified, passing a {@code null} argument to any
0N/A * method or constructor in this class will cause a {@link
0N/A * NullPointerException} to be thrown.
0N/A *
0N/A * @author Iris Clark
0N/A * @since 1.5
0N/A */
0N/Apublic final class Formatter implements Closeable, Flushable {
0N/A private Appendable a;
3377N/A private final Locale l;
0N/A
0N/A private IOException lastException;
0N/A
3377N/A private final char zero;
0N/A private static double scaleUp;
0N/A
0N/A // 1 (sign) + 19 (max # sig digits) + 1 ('.') + 1 ('e') + 1 (sign)
0N/A // + 3 (max # exp digits) + 4 (error) = 30
0N/A private static final int MAX_FD_CHARS = 30;
0N/A
3377N/A /**
3377N/A * Returns a charset object for the given charset name.
3377N/A * @throws NullPointerException is csn is null
3377N/A * @throws UnsupportedEncodingException if the charset is not supported
3377N/A */
3377N/A private static Charset toCharset(String csn)
3377N/A throws UnsupportedEncodingException
3377N/A {
3479N/A Objects.requireNonNull(csn, "charsetName");
3377N/A try {
3377N/A return Charset.forName(csn);
3377N/A } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
3377N/A // UnsupportedEncodingException should be thrown
3377N/A throw new UnsupportedEncodingException(csn);
3377N/A }
3377N/A }
3377N/A
3377N/A private static final Appendable nonNullAppendable(Appendable a) {
3377N/A if (a == null)
3377N/A return new StringBuilder();
3377N/A
3377N/A return a;
3377N/A }
3377N/A
3377N/A /* Private constructors */
3377N/A private Formatter(Locale l, Appendable a) {
0N/A this.a = a;
0N/A this.l = l;
3377N/A this.zero = getZero(l);
3377N/A }
3377N/A
3377N/A private Formatter(Charset charset, Locale l, File file)
3377N/A throws FileNotFoundException
3377N/A {
3377N/A this(l,
3377N/A new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter.
0N/A *
0N/A * <p> The destination of the formatted output is a {@link StringBuilder}
0N/A * which may be retrieved by invoking {@link #out out()} and whose
0N/A * current content may be converted into a string by invoking {@link
0N/A * #toString toString()}. The locale used is the {@linkplain
0N/A * Locale#getDefault() default locale} for this instance of the Java
0N/A * virtual machine.
0N/A */
0N/A public Formatter() {
3377N/A this(Locale.getDefault(Locale.Category.FORMAT), new StringBuilder());
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified destination.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault() default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * @param a
847N/A * Destination for the formatted output. If {@code a} is
847N/A * {@code null} then a {@link StringBuilder} will be created.
0N/A */
0N/A public Formatter(Appendable a) {
3377N/A this(Locale.getDefault(Locale.Category.FORMAT), nonNullAppendable(a));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified locale.
0N/A *
0N/A * <p> The destination of the formatted output is a {@link StringBuilder}
0N/A * which may be retrieved by invoking {@link #out out()} and whose current
0N/A * content may be converted into a string by invoking {@link #toString
0N/A * toString()}.
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
847N/A * formatting. If {@code l} is {@code null} then no localization
0N/A * is applied.
0N/A */
0N/A public Formatter(Locale l) {
3377N/A this(l, new StringBuilder());
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified destination and locale.
0N/A *
0N/A * @param a
847N/A * Destination for the formatted output. If {@code a} is
847N/A * {@code null} then a {@link StringBuilder} will be created.
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
847N/A * formatting. If {@code l} is {@code null} then no localization
0N/A * is applied.
0N/A */
0N/A public Formatter(Appendable a, Locale l) {
3377N/A this(l, nonNullAppendable(a));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified file name.
0N/A *
0N/A * <p> The charset used is the {@linkplain
0N/A * java.nio.charset.Charset#defaultCharset() default charset} for this
0N/A * instance of the Java virtual machine.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault() default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * @param fileName
0N/A * The name of the file to use as the destination of this
0N/A * formatter. If the file exists then it will be truncated to
0N/A * zero size; otherwise, a new file will be created. The output
0N/A * will be written to the file and is buffered.
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(fileName)} denies write
0N/A * access to the file
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file name does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A */
0N/A public Formatter(String fileName) throws FileNotFoundException {
3377N/A this(Locale.getDefault(Locale.Category.FORMAT),
3377N/A new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified file name and charset.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * @param fileName
0N/A * The name of the file to use as the destination of this
0N/A * formatter. If the file exists then it will be truncated to
0N/A * zero size; otherwise, a new file will be created. The output
0N/A * will be written to the file and is buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file name does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(fileName)} denies write
0N/A * access to the file
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A */
0N/A public Formatter(String fileName, String csn)
0N/A throws FileNotFoundException, UnsupportedEncodingException
0N/A {
2700N/A this(fileName, csn, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified file name, charset, and
0N/A * locale.
0N/A *
0N/A * @param fileName
0N/A * The name of the file to use as the destination of this
0N/A * formatter. If the file exists then it will be truncated to
0N/A * zero size; otherwise, a new file will be created. The output
0N/A * will be written to the file and is buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
847N/A * formatting. If {@code l} is {@code null} then no localization
0N/A * is applied.
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file name does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(fileName)} denies write
0N/A * access to the file
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A */
0N/A public Formatter(String fileName, String csn, Locale l)
0N/A throws FileNotFoundException, UnsupportedEncodingException
0N/A {
3377N/A this(toCharset(csn), l, new File(fileName));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified file.
0N/A *
0N/A * <p> The charset used is the {@linkplain
0N/A * java.nio.charset.Charset#defaultCharset() default charset} for this
0N/A * instance of the Java virtual machine.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault() default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * @param file
0N/A * The file to use as the destination of this formatter. If the
0N/A * file exists then it will be truncated to zero size; otherwise,
0N/A * a new file will be created. The output will be written to the
0N/A * file and is buffered.
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(file.getPath())} denies
0N/A * write access to the file
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file object does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A */
0N/A public Formatter(File file) throws FileNotFoundException {
3377N/A this(Locale.getDefault(Locale.Category.FORMAT),
3377N/A new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified file and charset.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * @param file
0N/A * The file to use as the destination of this formatter. If the
0N/A * file exists then it will be truncated to zero size; otherwise,
0N/A * a new file will be created. The output will be written to the
0N/A * file and is buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file object does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(file.getPath())} denies
0N/A * write access to the file
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A */
0N/A public Formatter(File file, String csn)
0N/A throws FileNotFoundException, UnsupportedEncodingException
0N/A {
2700N/A this(file, csn, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified file, charset, and
0N/A * locale.
0N/A *
0N/A * @param file
0N/A * The file to use as the destination of this formatter. If the
0N/A * file exists then it will be truncated to zero size; otherwise,
0N/A * a new file will be created. The output will be written to the
0N/A * file and is buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
847N/A * formatting. If {@code l} is {@code null} then no localization
0N/A * is applied.
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file object does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(file.getPath())} denies
0N/A * write access to the file
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A */
0N/A public Formatter(File file, String csn, Locale l)
0N/A throws FileNotFoundException, UnsupportedEncodingException
0N/A {
3377N/A this(toCharset(csn), l, file);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified print stream.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault() default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * <p> Characters are written to the given {@link java.io.PrintStream
0N/A * PrintStream} object and are therefore encoded using that object's
0N/A * charset.
0N/A *
0N/A * @param ps
0N/A * The stream to use as the destination of this formatter.
0N/A */
0N/A public Formatter(PrintStream ps) {
3377N/A this(Locale.getDefault(Locale.Category.FORMAT),
3479N/A (Appendable)Objects.requireNonNull(ps));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified output stream.
0N/A *
0N/A * <p> The charset used is the {@linkplain
0N/A * java.nio.charset.Charset#defaultCharset() default charset} for this
0N/A * instance of the Java virtual machine.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault() default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * @param os
0N/A * The output stream to use as the destination of this formatter.
0N/A * The output will be buffered.
0N/A */
0N/A public Formatter(OutputStream os) {
3377N/A this(Locale.getDefault(Locale.Category.FORMAT),
3377N/A new BufferedWriter(new OutputStreamWriter(os)));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified output stream and
0N/A * charset.
0N/A *
0N/A * <p> The locale used is the {@linkplain Locale#getDefault default
0N/A * locale} for this instance of the Java virtual machine.
0N/A *
0N/A * @param os
0N/A * The output stream to use as the destination of this formatter.
0N/A * The output will be buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A */
0N/A public Formatter(OutputStream os, String csn)
0N/A throws UnsupportedEncodingException
0N/A {
2700N/A this(os, csn, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new formatter with the specified output stream, charset,
0N/A * and locale.
0N/A *
0N/A * @param os
0N/A * The output stream to use as the destination of this formatter.
0N/A * The output will be buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
847N/A * formatting. If {@code l} is {@code null} then no localization
0N/A * is applied.
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A */
0N/A public Formatter(OutputStream os, String csn, Locale l)
0N/A throws UnsupportedEncodingException
0N/A {
3377N/A this(l, new BufferedWriter(new OutputStreamWriter(os, csn)));
0N/A }
0N/A
3377N/A private static char getZero(Locale l) {
0N/A if ((l != null) && !l.equals(Locale.US)) {
0N/A DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
3377N/A return dfs.getZeroDigit();
3377N/A } else {
3377N/A return '0';
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the locale set by the construction of this formatter.
0N/A *
0N/A * <p> The {@link #format(java.util.Locale,String,Object...) format} method
0N/A * for this object which has a locale argument does not change this value.
0N/A *
847N/A * @return {@code null} if no localization is applied, otherwise a
0N/A * locale
0N/A *
0N/A * @throws FormatterClosedException
0N/A * If this formatter has been closed by invoking its {@link
0N/A * #close()} method
0N/A */
0N/A public Locale locale() {
0N/A ensureOpen();
0N/A return l;
0N/A }
0N/A
0N/A /**
0N/A * Returns the destination for the output.
0N/A *
0N/A * @return The destination for the output
0N/A *
0N/A * @throws FormatterClosedException
0N/A * If this formatter has been closed by invoking its {@link
0N/A * #close()} method
0N/A */
0N/A public Appendable out() {
0N/A ensureOpen();
0N/A return a;
0N/A }
0N/A
0N/A /**
847N/A * Returns the result of invoking {@code toString()} on the destination
0N/A * for the output. For example, the following code formats text into a
0N/A * {@link StringBuilder} then retrieves the resultant string:
0N/A *
0N/A * <blockquote><pre>
0N/A * Formatter f = new Formatter();
0N/A * f.format("Last reboot at %tc", lastRebootDate);
0N/A * String s = f.toString();
0N/A * // -&gt; s == "Last reboot at Sat Jan 01 00:00:00 PST 2000"
0N/A * </pre></blockquote>
0N/A *
0N/A * <p> An invocation of this method behaves in exactly the same way as the
0N/A * invocation
0N/A *
0N/A * <pre>
0N/A * out().toString() </pre>
0N/A *
847N/A * <p> Depending on the specification of {@code toString} for the {@link
0N/A * Appendable}, the returned string may or may not contain the characters
0N/A * written to the destination. For instance, buffers typically return
847N/A * their contents in {@code toString()}, but streams cannot since the
0N/A * data is discarded.
0N/A *
847N/A * @return The result of invoking {@code toString()} on the destination
0N/A * for the output
0N/A *
0N/A * @throws FormatterClosedException
0N/A * If this formatter has been closed by invoking its {@link
0N/A * #close()} method
0N/A */
0N/A public String toString() {
0N/A ensureOpen();
0N/A return a.toString();
0N/A }
0N/A
0N/A /**
0N/A * Flushes this formatter. If the destination implements the {@link
847N/A * java.io.Flushable} interface, its {@code flush} method will be invoked.
0N/A *
0N/A * <p> Flushing a formatter writes any buffered output in the destination
0N/A * to the underlying stream.
0N/A *
0N/A * @throws FormatterClosedException
0N/A * If this formatter has been closed by invoking its {@link
0N/A * #close()} method
0N/A */
0N/A public void flush() {
0N/A ensureOpen();
0N/A if (a instanceof Flushable) {
0N/A try {
0N/A ((Flushable)a).flush();
0N/A } catch (IOException ioe) {
0N/A lastException = ioe;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Closes this formatter. If the destination implements the {@link
847N/A * java.io.Closeable} interface, its {@code close} method will be invoked.
0N/A *
0N/A * <p> Closing a formatter allows it to release resources it may be holding
0N/A * (such as open files). If the formatter is already closed, then invoking
0N/A * this method has no effect.
0N/A *
0N/A * <p> Attempting to invoke any methods except {@link #ioException()} in
0N/A * this formatter after it has been closed will result in a {@link
0N/A * FormatterClosedException}.
0N/A */
0N/A public void close() {
0N/A if (a == null)
0N/A return;
0N/A try {
0N/A if (a instanceof Closeable)
0N/A ((Closeable)a).close();
0N/A } catch (IOException ioe) {
0N/A lastException = ioe;
0N/A } finally {
0N/A a = null;
0N/A }
0N/A }
0N/A
0N/A private void ensureOpen() {
0N/A if (a == null)
0N/A throw new FormatterClosedException();
0N/A }
0N/A
0N/A /**
847N/A * Returns the {@code IOException} last thrown by this formatter's {@link
0N/A * Appendable}.
0N/A *
847N/A * <p> If the destination's {@code append()} method never throws
847N/A * {@code IOException}, then this method will always return {@code null}.
0N/A *
847N/A * @return The last exception thrown by the Appendable or {@code null} if
0N/A * no such exception exists.
0N/A */
0N/A public IOException ioException() {
0N/A return lastException;
0N/A }
0N/A
0N/A /**
0N/A * Writes a formatted string to this object's destination using the
0N/A * specified format string and arguments. The locale used is the one
0N/A * defined during the construction of this formatter.
0N/A *
0N/A * @param format
0N/A * A format string as described in <a href="#syntax">Format string
0N/A * syntax</a>.
0N/A *
0N/A * @param args
0N/A * Arguments referenced by the format specifiers in the format
0N/A * string. If there are more arguments than format specifiers, the
0N/A * extra arguments are ignored. The maximum number of arguments is
0N/A * limited by the maximum dimension of a Java array as defined by
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>.
0N/A *
0N/A * @throws IllegalFormatException
0N/A * If a format string contains an illegal syntax, a format
0N/A * specifier that is incompatible with the given arguments,
0N/A * insufficient arguments given the format string, or other
0N/A * illegal conditions. For specification of all possible
0N/A * formatting errors, see the <a href="#detail">Details</a>
0N/A * section of the formatter class specification.
0N/A *
0N/A * @throws FormatterClosedException
0N/A * If this formatter has been closed by invoking its {@link
0N/A * #close()} method
0N/A *
0N/A * @return This formatter
0N/A */
0N/A public Formatter format(String format, Object ... args) {
0N/A return format(l, format, args);
0N/A }
0N/A
0N/A /**
0N/A * Writes a formatted string to this object's destination using the
0N/A * specified locale, format string, and arguments.
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
847N/A * formatting. If {@code l} is {@code null} then no localization
0N/A * is applied. This does not change this object's locale that was
0N/A * set during construction.
0N/A *
0N/A * @param format
0N/A * A format string as described in <a href="#syntax">Format string
0N/A * syntax</a>
0N/A *
0N/A * @param args
0N/A * Arguments referenced by the format specifiers in the format
0N/A * string. If there are more arguments than format specifiers, the
0N/A * extra arguments are ignored. The maximum number of arguments is
0N/A * limited by the maximum dimension of a Java array as defined by
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>.
0N/A *
0N/A * @throws IllegalFormatException
0N/A * If a format string contains an illegal syntax, a format
0N/A * specifier that is incompatible with the given arguments,
0N/A * insufficient arguments given the format string, or other
0N/A * illegal conditions. For specification of all possible
0N/A * formatting errors, see the <a href="#detail">Details</a>
0N/A * section of the formatter class specification.
0N/A *
0N/A * @throws FormatterClosedException
0N/A * If this formatter has been closed by invoking its {@link
0N/A * #close()} method
0N/A *
0N/A * @return This formatter
0N/A */
0N/A public Formatter format(Locale l, String format, Object ... args) {
0N/A ensureOpen();
0N/A
0N/A // index of last argument referenced
0N/A int last = -1;
0N/A // last ordinary index
0N/A int lasto = -1;
0N/A
0N/A FormatString[] fsa = parse(format);
0N/A for (int i = 0; i < fsa.length; i++) {
0N/A FormatString fs = fsa[i];
0N/A int index = fs.index();
0N/A try {
0N/A switch (index) {
0N/A case -2: // fixed string, "%n", or "%%"
0N/A fs.print(null, l);
0N/A break;
0N/A case -1: // relative index
0N/A if (last < 0 || (args != null && last > args.length - 1))
0N/A throw new MissingFormatArgumentException(fs.toString());
0N/A fs.print((args == null ? null : args[last]), l);
0N/A break;
0N/A case 0: // ordinary index
0N/A lasto++;
0N/A last = lasto;
0N/A if (args != null && lasto > args.length - 1)
0N/A throw new MissingFormatArgumentException(fs.toString());
0N/A fs.print((args == null ? null : args[lasto]), l);
0N/A break;
0N/A default: // explicit index
0N/A last = index - 1;
0N/A if (args != null && last > args.length - 1)
0N/A throw new MissingFormatArgumentException(fs.toString());
0N/A fs.print((args == null ? null : args[last]), l);
0N/A break;
0N/A }
0N/A } catch (IOException x) {
0N/A lastException = x;
0N/A }
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A // %[argument_index$][flags][width][.precision][t]conversion
0N/A private static final String formatSpecifier
0N/A = "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";
0N/A
0N/A private static Pattern fsPattern = Pattern.compile(formatSpecifier);
0N/A
1819N/A /**
1819N/A * Finds format specifiers in the format string.
1819N/A */
0N/A private FormatString[] parse(String s) {
3323N/A ArrayList<FormatString> al = new ArrayList<>();
0N/A Matcher m = fsPattern.matcher(s);
1819N/A for (int i = 0, len = s.length(); i < len; ) {
0N/A if (m.find(i)) {
0N/A // Anything between the start of the string and the beginning
0N/A // of the format specifier is either fixed text or contains
0N/A // an invalid format string.
0N/A if (m.start() != i) {
0N/A // Make sure we didn't miss any invalid format specifiers
1819N/A checkText(s, i, m.start());
0N/A // Assume previous characters were fixed text
0N/A al.add(new FixedString(s.substring(i, m.start())));
0N/A }
0N/A
1819N/A al.add(new FormatSpecifier(m));
0N/A i = m.end();
0N/A } else {
0N/A // No more valid format specifiers. Check for possible invalid
0N/A // format specifiers.
1819N/A checkText(s, i, len);
0N/A // The rest of the string is fixed text
0N/A al.add(new FixedString(s.substring(i)));
0N/A break;
0N/A }
0N/A }
1819N/A return al.toArray(new FormatString[al.size()]);
0N/A }
0N/A
1819N/A private static void checkText(String s, int start, int end) {
1819N/A for (int i = start; i < end; i++) {
1819N/A // Any '%' found in the region starts an invalid format specifier.
1819N/A if (s.charAt(i) == '%') {
1819N/A char c = (i == end - 1) ? '%' : s.charAt(i + 1);
1819N/A throw new UnknownFormatConversionException(String.valueOf(c));
1819N/A }
0N/A }
0N/A }
0N/A
0N/A private interface FormatString {
0N/A int index();
0N/A void print(Object arg, Locale l) throws IOException;
0N/A String toString();
0N/A }
0N/A
0N/A private class FixedString implements FormatString {
0N/A private String s;
0N/A FixedString(String s) { this.s = s; }
0N/A public int index() { return -2; }
0N/A public void print(Object arg, Locale l)
0N/A throws IOException { a.append(s); }
0N/A public String toString() { return s; }
0N/A }
0N/A
0N/A public enum BigDecimalLayoutForm { SCIENTIFIC, DECIMAL_FLOAT };
0N/A
0N/A private class FormatSpecifier implements FormatString {
0N/A private int index = -1;
0N/A private Flags f = Flags.NONE;
0N/A private int width;
0N/A private int precision;
0N/A private boolean dt = false;
0N/A private char c;
0N/A
0N/A private int index(String s) {
0N/A if (s != null) {
0N/A try {
0N/A index = Integer.parseInt(s.substring(0, s.length() - 1));
0N/A } catch (NumberFormatException x) {
0N/A assert(false);
0N/A }
0N/A } else {
0N/A index = 0;
0N/A }
0N/A return index;
0N/A }
0N/A
0N/A public int index() {
0N/A return index;
0N/A }
0N/A
0N/A private Flags flags(String s) {
0N/A f = Flags.parse(s);
0N/A if (f.contains(Flags.PREVIOUS))
0N/A index = -1;
0N/A return f;
0N/A }
0N/A
0N/A Flags flags() {
0N/A return f;
0N/A }
0N/A
0N/A private int width(String s) {
0N/A width = -1;
0N/A if (s != null) {
0N/A try {
0N/A width = Integer.parseInt(s);
0N/A if (width < 0)
0N/A throw new IllegalFormatWidthException(width);
0N/A } catch (NumberFormatException x) {
0N/A assert(false);
0N/A }
0N/A }
0N/A return width;
0N/A }
0N/A
0N/A int width() {
0N/A return width;
0N/A }
0N/A
0N/A private int precision(String s) {
0N/A precision = -1;
0N/A if (s != null) {
0N/A try {
0N/A // remove the '.'
0N/A precision = Integer.parseInt(s.substring(1));
0N/A if (precision < 0)
0N/A throw new IllegalFormatPrecisionException(precision);
0N/A } catch (NumberFormatException x) {
0N/A assert(false);
0N/A }
0N/A }
0N/A return precision;
0N/A }
0N/A
0N/A int precision() {
0N/A return precision;
0N/A }
0N/A
0N/A private char conversion(String s) {
0N/A c = s.charAt(0);
0N/A if (!dt) {
0N/A if (!Conversion.isValid(c))
0N/A throw new UnknownFormatConversionException(String.valueOf(c));
0N/A if (Character.isUpperCase(c))
0N/A f.add(Flags.UPPERCASE);
0N/A c = Character.toLowerCase(c);
0N/A if (Conversion.isText(c))
0N/A index = -2;
0N/A }
0N/A return c;
0N/A }
0N/A
0N/A private char conversion() {
0N/A return c;
0N/A }
0N/A
1819N/A FormatSpecifier(Matcher m) {
1819N/A int idx = 1;
1819N/A
1819N/A index(m.group(idx++));
1819N/A flags(m.group(idx++));
1819N/A width(m.group(idx++));
1819N/A precision(m.group(idx++));
1819N/A
1819N/A String tT = m.group(idx++);
1819N/A if (tT != null) {
0N/A dt = true;
1819N/A if (tT.equals("T"))
0N/A f.add(Flags.UPPERCASE);
0N/A }
1819N/A
1819N/A conversion(m.group(idx));
0N/A
0N/A if (dt)
0N/A checkDateTime();
0N/A else if (Conversion.isGeneral(c))
0N/A checkGeneral();
0N/A else if (Conversion.isCharacter(c))
0N/A checkCharacter();
0N/A else if (Conversion.isInteger(c))
0N/A checkInteger();
0N/A else if (Conversion.isFloat(c))
0N/A checkFloat();
0N/A else if (Conversion.isText(c))
0N/A checkText();
0N/A else
0N/A throw new UnknownFormatConversionException(String.valueOf(c));
0N/A }
0N/A
0N/A public void print(Object arg, Locale l) throws IOException {
0N/A if (dt) {
0N/A printDateTime(arg, l);
0N/A return;
0N/A }
0N/A switch(c) {
0N/A case Conversion.DECIMAL_INTEGER:
0N/A case Conversion.OCTAL_INTEGER:
0N/A case Conversion.HEXADECIMAL_INTEGER:
0N/A printInteger(arg, l);
0N/A break;
0N/A case Conversion.SCIENTIFIC:
0N/A case Conversion.GENERAL:
0N/A case Conversion.DECIMAL_FLOAT:
0N/A case Conversion.HEXADECIMAL_FLOAT:
0N/A printFloat(arg, l);
0N/A break;
0N/A case Conversion.CHARACTER:
0N/A case Conversion.CHARACTER_UPPER:
0N/A printCharacter(arg);
0N/A break;
0N/A case Conversion.BOOLEAN:
0N/A printBoolean(arg);
0N/A break;
0N/A case Conversion.STRING:
0N/A printString(arg, l);
0N/A break;
0N/A case Conversion.HASHCODE:
0N/A printHashCode(arg);
0N/A break;
0N/A case Conversion.LINE_SEPARATOR:
1960N/A a.append(System.lineSeparator());
0N/A break;
0N/A case Conversion.PERCENT_SIGN:
0N/A a.append('%');
0N/A break;
0N/A default:
0N/A assert false;
0N/A }
0N/A }
0N/A
0N/A private void printInteger(Object arg, Locale l) throws IOException {
0N/A if (arg == null)
0N/A print("null");
0N/A else if (arg instanceof Byte)
0N/A print(((Byte)arg).byteValue(), l);
0N/A else if (arg instanceof Short)
0N/A print(((Short)arg).shortValue(), l);
0N/A else if (arg instanceof Integer)
0N/A print(((Integer)arg).intValue(), l);
0N/A else if (arg instanceof Long)
0N/A print(((Long)arg).longValue(), l);
0N/A else if (arg instanceof BigInteger)
0N/A print(((BigInteger)arg), l);
0N/A else
0N/A failConversion(c, arg);
0N/A }
0N/A
0N/A private void printFloat(Object arg, Locale l) throws IOException {
0N/A if (arg == null)
0N/A print("null");
0N/A else if (arg instanceof Float)
0N/A print(((Float)arg).floatValue(), l);
0N/A else if (arg instanceof Double)
0N/A print(((Double)arg).doubleValue(), l);
0N/A else if (arg instanceof BigDecimal)
0N/A print(((BigDecimal)arg), l);
0N/A else
0N/A failConversion(c, arg);
0N/A }
0N/A
0N/A private void printDateTime(Object arg, Locale l) throws IOException {
0N/A if (arg == null) {
0N/A print("null");
0N/A return;
0N/A }
0N/A Calendar cal = null;
0N/A
0N/A // Instead of Calendar.setLenient(true), perhaps we should
0N/A // wrap the IllegalArgumentException that might be thrown?
0N/A if (arg instanceof Long) {
0N/A // Note that the following method uses an instance of the
0N/A // default time zone (TimeZone.getDefaultRef().
0N/A cal = Calendar.getInstance(l == null ? Locale.US : l);
0N/A cal.setTimeInMillis((Long)arg);
0N/A } else if (arg instanceof Date) {
0N/A // Note that the following method uses an instance of the
0N/A // default time zone (TimeZone.getDefaultRef().
0N/A cal = Calendar.getInstance(l == null ? Locale.US : l);
0N/A cal.setTime((Date)arg);
0N/A } else if (arg instanceof Calendar) {
0N/A cal = (Calendar) ((Calendar)arg).clone();
0N/A cal.setLenient(true);
0N/A } else {
0N/A failConversion(c, arg);
0N/A }
0N/A // Use the provided locale so that invocations of
0N/A // localizedMagnitude() use optimizations for null.
0N/A print(cal, c, l);
0N/A }
0N/A
0N/A private void printCharacter(Object arg) throws IOException {
0N/A if (arg == null) {
0N/A print("null");
0N/A return;
0N/A }
0N/A String s = null;
0N/A if (arg instanceof Character) {
0N/A s = ((Character)arg).toString();
0N/A } else if (arg instanceof Byte) {
0N/A byte i = ((Byte)arg).byteValue();
0N/A if (Character.isValidCodePoint(i))
0N/A s = new String(Character.toChars(i));
0N/A else
0N/A throw new IllegalFormatCodePointException(i);
0N/A } else if (arg instanceof Short) {
0N/A short i = ((Short)arg).shortValue();
0N/A if (Character.isValidCodePoint(i))
0N/A s = new String(Character.toChars(i));
0N/A else
0N/A throw new IllegalFormatCodePointException(i);
0N/A } else if (arg instanceof Integer) {
0N/A int i = ((Integer)arg).intValue();
0N/A if (Character.isValidCodePoint(i))
0N/A s = new String(Character.toChars(i));
0N/A else
0N/A throw new IllegalFormatCodePointException(i);
0N/A } else {
0N/A failConversion(c, arg);
0N/A }
0N/A print(s);
0N/A }
0N/A
0N/A private void printString(Object arg, Locale l) throws IOException {
1469N/A if (arg instanceof Formattable) {
1819N/A Formatter fmt = Formatter.this;
1819N/A if (fmt.locale() != l)
1819N/A fmt = new Formatter(fmt.out(), l);
0N/A ((Formattable)arg).formatTo(fmt, f.valueOf(), width, precision);
0N/A } else {
1469N/A if (f.contains(Flags.ALTERNATE))
1469N/A failMismatch(Flags.ALTERNATE, 's');
1469N/A if (arg == null)
1469N/A print("null");
1469N/A else
1469N/A print(arg.toString());
0N/A }
0N/A }
0N/A
0N/A private void printBoolean(Object arg) throws IOException {
0N/A String s;
0N/A if (arg != null)
0N/A s = ((arg instanceof Boolean)
0N/A ? ((Boolean)arg).toString()
0N/A : Boolean.toString(true));
0N/A else
0N/A s = Boolean.toString(false);
0N/A print(s);
0N/A }
0N/A
0N/A private void printHashCode(Object arg) throws IOException {
0N/A String s = (arg == null
0N/A ? "null"
0N/A : Integer.toHexString(arg.hashCode()));
0N/A print(s);
0N/A }
0N/A
0N/A private void print(String s) throws IOException {
0N/A if (precision != -1 && precision < s.length())
0N/A s = s.substring(0, precision);
0N/A if (f.contains(Flags.UPPERCASE))
0N/A s = s.toUpperCase();
0N/A a.append(justify(s));
0N/A }
0N/A
0N/A private String justify(String s) {
0N/A if (width == -1)
0N/A return s;
0N/A StringBuilder sb = new StringBuilder();
0N/A boolean pad = f.contains(Flags.LEFT_JUSTIFY);
0N/A int sp = width - s.length();
0N/A if (!pad)
0N/A for (int i = 0; i < sp; i++) sb.append(' ');
0N/A sb.append(s);
0N/A if (pad)
0N/A for (int i = 0; i < sp; i++) sb.append(' ');
0N/A return sb.toString();
0N/A }
0N/A
0N/A public String toString() {
0N/A StringBuilder sb = new StringBuilder('%');
0N/A // Flags.UPPERCASE is set internally for legal conversions.
0N/A Flags dupf = f.dup().remove(Flags.UPPERCASE);
0N/A sb.append(dupf.toString());
0N/A if (index > 0)
0N/A sb.append(index).append('$');
0N/A if (width != -1)
0N/A sb.append(width);
0N/A if (precision != -1)
0N/A sb.append('.').append(precision);
0N/A if (dt)
0N/A sb.append(f.contains(Flags.UPPERCASE) ? 'T' : 't');
0N/A sb.append(f.contains(Flags.UPPERCASE)
0N/A ? Character.toUpperCase(c) : c);
0N/A return sb.toString();
0N/A }
0N/A
0N/A private void checkGeneral() {
0N/A if ((c == Conversion.BOOLEAN || c == Conversion.HASHCODE)
0N/A && f.contains(Flags.ALTERNATE))
0N/A failMismatch(Flags.ALTERNATE, c);
0N/A // '-' requires a width
0N/A if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
0N/A throw new MissingFormatWidthException(toString());
0N/A checkBadFlags(Flags.PLUS, Flags.LEADING_SPACE, Flags.ZERO_PAD,
0N/A Flags.GROUP, Flags.PARENTHESES);
0N/A }
0N/A
0N/A private void checkDateTime() {
0N/A if (precision != -1)
0N/A throw new IllegalFormatPrecisionException(precision);
0N/A if (!DateTime.isValid(c))
0N/A throw new UnknownFormatConversionException("t" + c);
0N/A checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE,
0N/A Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES);
0N/A // '-' requires a width
0N/A if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
0N/A throw new MissingFormatWidthException(toString());
0N/A }
0N/A
0N/A private void checkCharacter() {
0N/A if (precision != -1)
0N/A throw new IllegalFormatPrecisionException(precision);
0N/A checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE,
0N/A Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES);
0N/A // '-' requires a width
0N/A if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
0N/A throw new MissingFormatWidthException(toString());
0N/A }
0N/A
0N/A private void checkInteger() {
0N/A checkNumeric();
0N/A if (precision != -1)
0N/A throw new IllegalFormatPrecisionException(precision);
0N/A
0N/A if (c == Conversion.DECIMAL_INTEGER)
0N/A checkBadFlags(Flags.ALTERNATE);
0N/A else if (c == Conversion.OCTAL_INTEGER)
0N/A checkBadFlags(Flags.GROUP);
0N/A else
0N/A checkBadFlags(Flags.GROUP);
0N/A }
0N/A
0N/A private void checkBadFlags(Flags ... badFlags) {
0N/A for (int i = 0; i < badFlags.length; i++)
0N/A if (f.contains(badFlags[i]))
0N/A failMismatch(badFlags[i], c);
0N/A }
0N/A
0N/A private void checkFloat() {
0N/A checkNumeric();
0N/A if (c == Conversion.DECIMAL_FLOAT) {
0N/A } else if (c == Conversion.HEXADECIMAL_FLOAT) {
0N/A checkBadFlags(Flags.PARENTHESES, Flags.GROUP);
0N/A } else if (c == Conversion.SCIENTIFIC) {
0N/A checkBadFlags(Flags.GROUP);
0N/A } else if (c == Conversion.GENERAL) {
0N/A checkBadFlags(Flags.ALTERNATE);
0N/A }
0N/A }
0N/A
0N/A private void checkNumeric() {
0N/A if (width != -1 && width < 0)
0N/A throw new IllegalFormatWidthException(width);
0N/A
0N/A if (precision != -1 && precision < 0)
0N/A throw new IllegalFormatPrecisionException(precision);
0N/A
0N/A // '-' and '0' require a width
0N/A if (width == -1
0N/A && (f.contains(Flags.LEFT_JUSTIFY) || f.contains(Flags.ZERO_PAD)))
0N/A throw new MissingFormatWidthException(toString());
0N/A
0N/A // bad combination
0N/A if ((f.contains(Flags.PLUS) && f.contains(Flags.LEADING_SPACE))
0N/A || (f.contains(Flags.LEFT_JUSTIFY) && f.contains(Flags.ZERO_PAD)))
0N/A throw new IllegalFormatFlagsException(f.toString());
0N/A }
0N/A
0N/A private void checkText() {
0N/A if (precision != -1)
0N/A throw new IllegalFormatPrecisionException(precision);
0N/A switch (c) {
0N/A case Conversion.PERCENT_SIGN:
0N/A if (f.valueOf() != Flags.LEFT_JUSTIFY.valueOf()
0N/A && f.valueOf() != Flags.NONE.valueOf())
0N/A throw new IllegalFormatFlagsException(f.toString());
0N/A // '-' requires a width
0N/A if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
0N/A throw new MissingFormatWidthException(toString());
0N/A break;
0N/A case Conversion.LINE_SEPARATOR:
0N/A if (width != -1)
0N/A throw new IllegalFormatWidthException(width);
0N/A if (f.valueOf() != Flags.NONE.valueOf())
0N/A throw new IllegalFormatFlagsException(f.toString());
0N/A break;
0N/A default:
0N/A assert false;
0N/A }
0N/A }
0N/A
0N/A private void print(byte value, Locale l) throws IOException {
0N/A long v = value;
0N/A if (value < 0
0N/A && (c == Conversion.OCTAL_INTEGER
0N/A || c == Conversion.HEXADECIMAL_INTEGER)) {
0N/A v += (1L << 8);
0N/A assert v >= 0 : v;
0N/A }
0N/A print(v, l);
0N/A }
0N/A
0N/A private void print(short value, Locale l) throws IOException {
0N/A long v = value;
0N/A if (value < 0
0N/A && (c == Conversion.OCTAL_INTEGER
0N/A || c == Conversion.HEXADECIMAL_INTEGER)) {
0N/A v += (1L << 16);
0N/A assert v >= 0 : v;
0N/A }
0N/A print(v, l);
0N/A }
0N/A
0N/A private void print(int value, Locale l) throws IOException {
0N/A long v = value;
0N/A if (value < 0
0N/A && (c == Conversion.OCTAL_INTEGER
0N/A || c == Conversion.HEXADECIMAL_INTEGER)) {
0N/A v += (1L << 32);
0N/A assert v >= 0 : v;
0N/A }
0N/A print(v, l);
0N/A }
0N/A
0N/A private void print(long value, Locale l) throws IOException {
0N/A
0N/A StringBuilder sb = new StringBuilder();
0N/A
0N/A if (c == Conversion.DECIMAL_INTEGER) {
0N/A boolean neg = value < 0;
0N/A char[] va;
0N/A if (value < 0)
0N/A va = Long.toString(value, 10).substring(1).toCharArray();
0N/A else
0N/A va = Long.toString(value, 10).toCharArray();
0N/A
0N/A // leading sign indicator
0N/A leadingSign(sb, neg);
0N/A
0N/A // the value
0N/A localizedMagnitude(sb, va, f, adjustWidth(width, f, neg), l);
0N/A
0N/A // trailing sign indicator
0N/A trailingSign(sb, neg);
0N/A } else if (c == Conversion.OCTAL_INTEGER) {
0N/A checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE,
0N/A Flags.PLUS);
0N/A String s = Long.toOctalString(value);
0N/A int len = (f.contains(Flags.ALTERNATE)
0N/A ? s.length() + 1
0N/A : s.length());
0N/A
0N/A // apply ALTERNATE (radix indicator for octal) before ZERO_PAD
0N/A if (f.contains(Flags.ALTERNATE))
0N/A sb.append('0');
0N/A if (f.contains(Flags.ZERO_PAD))
0N/A for (int i = 0; i < width - len; i++) sb.append('0');
0N/A sb.append(s);
0N/A } else if (c == Conversion.HEXADECIMAL_INTEGER) {
0N/A checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE,
0N/A Flags.PLUS);
0N/A String s = Long.toHexString(value);
0N/A int len = (f.contains(Flags.ALTERNATE)
0N/A ? s.length() + 2
0N/A : s.length());
0N/A
0N/A // apply ALTERNATE (radix indicator for hex) before ZERO_PAD
0N/A if (f.contains(Flags.ALTERNATE))
0N/A sb.append(f.contains(Flags.UPPERCASE) ? "0X" : "0x");
0N/A if (f.contains(Flags.ZERO_PAD))
0N/A for (int i = 0; i < width - len; i++) sb.append('0');
0N/A if (f.contains(Flags.UPPERCASE))
0N/A s = s.toUpperCase();
0N/A sb.append(s);
0N/A }
0N/A
0N/A // justify based on width
0N/A a.append(justify(sb.toString()));
0N/A }
0N/A
0N/A // neg := val < 0
0N/A private StringBuilder leadingSign(StringBuilder sb, boolean neg) {
0N/A if (!neg) {
0N/A if (f.contains(Flags.PLUS)) {
0N/A sb.append('+');
0N/A } else if (f.contains(Flags.LEADING_SPACE)) {
0N/A sb.append(' ');
0N/A }
0N/A } else {
0N/A if (f.contains(Flags.PARENTHESES))
0N/A sb.append('(');
0N/A else
0N/A sb.append('-');
0N/A }
0N/A return sb;
0N/A }
0N/A
0N/A // neg := val < 0
0N/A private StringBuilder trailingSign(StringBuilder sb, boolean neg) {
0N/A if (neg && f.contains(Flags.PARENTHESES))
0N/A sb.append(')');
0N/A return sb;
0N/A }
0N/A
0N/A private void print(BigInteger value, Locale l) throws IOException {
0N/A StringBuilder sb = new StringBuilder();
0N/A boolean neg = value.signum() == -1;
0N/A BigInteger v = value.abs();
0N/A
0N/A // leading sign indicator
0N/A leadingSign(sb, neg);
0N/A
0N/A // the value
0N/A if (c == Conversion.DECIMAL_INTEGER) {
0N/A char[] va = v.toString().toCharArray();
0N/A localizedMagnitude(sb, va, f, adjustWidth(width, f, neg), l);
0N/A } else if (c == Conversion.OCTAL_INTEGER) {
0N/A String s = v.toString(8);
0N/A
0N/A int len = s.length() + sb.length();
0N/A if (neg && f.contains(Flags.PARENTHESES))
0N/A len++;
0N/A
0N/A // apply ALTERNATE (radix indicator for octal) before ZERO_PAD
0N/A if (f.contains(Flags.ALTERNATE)) {
0N/A len++;
0N/A sb.append('0');
0N/A }
0N/A if (f.contains(Flags.ZERO_PAD)) {
0N/A for (int i = 0; i < width - len; i++)
0N/A sb.append('0');
0N/A }
0N/A sb.append(s);
0N/A } else if (c == Conversion.HEXADECIMAL_INTEGER) {
0N/A String s = v.toString(16);
0N/A
0N/A int len = s.length() + sb.length();
0N/A if (neg && f.contains(Flags.PARENTHESES))
0N/A len++;
0N/A
0N/A // apply ALTERNATE (radix indicator for hex) before ZERO_PAD
0N/A if (f.contains(Flags.ALTERNATE)) {
0N/A len += 2;
0N/A sb.append(f.contains(Flags.UPPERCASE) ? "0X" : "0x");
0N/A }
0N/A if (f.contains(Flags.ZERO_PAD))
0N/A for (int i = 0; i < width - len; i++)
0N/A sb.append('0');
0N/A if (f.contains(Flags.UPPERCASE))
0N/A s = s.toUpperCase();
0N/A sb.append(s);
0N/A }
0N/A
0N/A // trailing sign indicator
0N/A trailingSign(sb, (value.signum() == -1));
0N/A
0N/A // justify based on width
0N/A a.append(justify(sb.toString()));
0N/A }
0N/A
0N/A private void print(float value, Locale l) throws IOException {
0N/A print((double) value, l);
0N/A }
0N/A
0N/A private void print(double value, Locale l) throws IOException {
0N/A StringBuilder sb = new StringBuilder();
0N/A boolean neg = Double.compare(value, 0.0) == -1;
0N/A
0N/A if (!Double.isNaN(value)) {
0N/A double v = Math.abs(value);
0N/A
0N/A // leading sign indicator
0N/A leadingSign(sb, neg);
0N/A
0N/A // the value
0N/A if (!Double.isInfinite(v))
0N/A print(sb, v, l, f, c, precision, neg);
0N/A else
0N/A sb.append(f.contains(Flags.UPPERCASE)
0N/A ? "INFINITY" : "Infinity");
0N/A
0N/A // trailing sign indicator
0N/A trailingSign(sb, neg);
0N/A } else {
0N/A sb.append(f.contains(Flags.UPPERCASE) ? "NAN" : "NaN");
0N/A }
0N/A
0N/A // justify based on width
0N/A a.append(justify(sb.toString()));
0N/A }
0N/A
0N/A // !Double.isInfinite(value) && !Double.isNaN(value)
0N/A private void print(StringBuilder sb, double value, Locale l,
0N/A Flags f, char c, int precision, boolean neg)
0N/A throws IOException
0N/A {
0N/A if (c == Conversion.SCIENTIFIC) {
0N/A // Create a new FormattedFloatingDecimal with the desired
0N/A // precision.
0N/A int prec = (precision == -1 ? 6 : precision);
0N/A
0N/A FormattedFloatingDecimal fd
0N/A = new FormattedFloatingDecimal(value, prec,
0N/A FormattedFloatingDecimal.Form.SCIENTIFIC);
0N/A
0N/A char[] v = new char[MAX_FD_CHARS];
0N/A int len = fd.getChars(v);
0N/A
0N/A char[] mant = addZeros(mantissa(v, len), prec);
0N/A
0N/A // If the precision is zero and the '#' flag is set, add the
0N/A // requested decimal point.
0N/A if (f.contains(Flags.ALTERNATE) && (prec == 0))
0N/A mant = addDot(mant);
0N/A
0N/A char[] exp = (value == 0.0)
0N/A ? new char[] {'+','0','0'} : exponent(v, len);
0N/A
0N/A int newW = width;
0N/A if (width != -1)
0N/A newW = adjustWidth(width - exp.length - 1, f, neg);
0N/A localizedMagnitude(sb, mant, f, newW, l);
0N/A
0N/A sb.append(f.contains(Flags.UPPERCASE) ? 'E' : 'e');
0N/A
0N/A Flags flags = f.dup().remove(Flags.GROUP);
0N/A char sign = exp[0];
0N/A assert(sign == '+' || sign == '-');
0N/A sb.append(sign);
0N/A
0N/A char[] tmp = new char[exp.length - 1];
0N/A System.arraycopy(exp, 1, tmp, 0, exp.length - 1);
0N/A sb.append(localizedMagnitude(null, tmp, flags, -1, l));
0N/A } else if (c == Conversion.DECIMAL_FLOAT) {
0N/A // Create a new FormattedFloatingDecimal with the desired
0N/A // precision.
0N/A int prec = (precision == -1 ? 6 : precision);
0N/A
0N/A FormattedFloatingDecimal fd
0N/A = new FormattedFloatingDecimal(value, prec,
0N/A FormattedFloatingDecimal.Form.DECIMAL_FLOAT);
0N/A
0N/A // MAX_FD_CHARS + 1 (round?)
0N/A char[] v = new char[MAX_FD_CHARS + 1
0N/A + Math.abs(fd.getExponent())];
0N/A int len = fd.getChars(v);
0N/A
0N/A char[] mant = addZeros(mantissa(v, len), prec);
0N/A
0N/A // If the precision is zero and the '#' flag is set, add the
0N/A // requested decimal point.
0N/A if (f.contains(Flags.ALTERNATE) && (prec == 0))
0N/A mant = addDot(mant);
0N/A
0N/A int newW = width;
0N/A if (width != -1)
0N/A newW = adjustWidth(width, f, neg);
0N/A localizedMagnitude(sb, mant, f, newW, l);
0N/A } else if (c == Conversion.GENERAL) {
0N/A int prec = precision;
0N/A if (precision == -1)
0N/A prec = 6;
0N/A else if (precision == 0)
0N/A prec = 1;
0N/A
0N/A FormattedFloatingDecimal fd
0N/A = new FormattedFloatingDecimal(value, prec,
0N/A FormattedFloatingDecimal.Form.GENERAL);
0N/A
0N/A // MAX_FD_CHARS + 1 (round?)
0N/A char[] v = new char[MAX_FD_CHARS + 1
0N/A + Math.abs(fd.getExponent())];
0N/A int len = fd.getChars(v);
0N/A
0N/A char[] exp = exponent(v, len);
0N/A if (exp != null) {
0N/A prec -= 1;
0N/A } else {
0N/A prec = prec - (value == 0 ? 0 : fd.getExponentRounded()) - 1;
0N/A }
0N/A
0N/A char[] mant = addZeros(mantissa(v, len), prec);
0N/A // If the precision is zero and the '#' flag is set, add the
0N/A // requested decimal point.
0N/A if (f.contains(Flags.ALTERNATE) && (prec == 0))
0N/A mant = addDot(mant);
0N/A
0N/A int newW = width;
0N/A if (width != -1) {
0N/A if (exp != null)
0N/A newW = adjustWidth(width - exp.length - 1, f, neg);
0N/A else
0N/A newW = adjustWidth(width, f, neg);
0N/A }
0N/A localizedMagnitude(sb, mant, f, newW, l);
0N/A
0N/A if (exp != null) {
0N/A sb.append(f.contains(Flags.UPPERCASE) ? 'E' : 'e');
0N/A
0N/A Flags flags = f.dup().remove(Flags.GROUP);
0N/A char sign = exp[0];
0N/A assert(sign == '+' || sign == '-');
0N/A sb.append(sign);
0N/A
0N/A char[] tmp = new char[exp.length - 1];
0N/A System.arraycopy(exp, 1, tmp, 0, exp.length - 1);
0N/A sb.append(localizedMagnitude(null, tmp, flags, -1, l));
0N/A }
0N/A } else if (c == Conversion.HEXADECIMAL_FLOAT) {
0N/A int prec = precision;
0N/A if (precision == -1)
0N/A // assume that we want all of the digits
0N/A prec = 0;
0N/A else if (precision == 0)
0N/A prec = 1;
0N/A
0N/A String s = hexDouble(value, prec);
0N/A
0N/A char[] va;
0N/A boolean upper = f.contains(Flags.UPPERCASE);
0N/A sb.append(upper ? "0X" : "0x");
0N/A
0N/A if (f.contains(Flags.ZERO_PAD))
0N/A for (int i = 0; i < width - s.length() - 2; i++)
0N/A sb.append('0');
0N/A
0N/A int idx = s.indexOf('p');
0N/A va = s.substring(0, idx).toCharArray();
0N/A if (upper) {
0N/A String tmp = new String(va);
0N/A // don't localize hex
0N/A tmp = tmp.toUpperCase(Locale.US);
0N/A va = tmp.toCharArray();
0N/A }
0N/A sb.append(prec != 0 ? addZeros(va, prec) : va);
0N/A sb.append(upper ? 'P' : 'p');
0N/A sb.append(s.substring(idx+1));
0N/A }
0N/A }
0N/A
0N/A private char[] mantissa(char[] v, int len) {
0N/A int i;
0N/A for (i = 0; i < len; i++) {
0N/A if (v[i] == 'e')
0N/A break;
0N/A }
0N/A char[] tmp = new char[i];
0N/A System.arraycopy(v, 0, tmp, 0, i);
0N/A return tmp;
0N/A }
0N/A
0N/A private char[] exponent(char[] v, int len) {
0N/A int i;
0N/A for (i = len - 1; i >= 0; i--) {
0N/A if (v[i] == 'e')
0N/A break;
0N/A }
0N/A if (i == -1)
0N/A return null;
0N/A char[] tmp = new char[len - i - 1];
0N/A System.arraycopy(v, i + 1, tmp, 0, len - i - 1);
0N/A return tmp;
0N/A }
0N/A
0N/A // Add zeros to the requested precision.
0N/A private char[] addZeros(char[] v, int prec) {
0N/A // Look for the dot. If we don't find one, the we'll need to add
0N/A // it before we add the zeros.
0N/A int i;
0N/A for (i = 0; i < v.length; i++) {
0N/A if (v[i] == '.')
0N/A break;
0N/A }
0N/A boolean needDot = false;
0N/A if (i == v.length) {
0N/A needDot = true;
0N/A }
0N/A
0N/A // Determine existing precision.
0N/A int outPrec = v.length - i - (needDot ? 0 : 1);
0N/A assert (outPrec <= prec);
0N/A if (outPrec == prec)
0N/A return v;
0N/A
0N/A // Create new array with existing contents.
0N/A char[] tmp
0N/A = new char[v.length + prec - outPrec + (needDot ? 1 : 0)];
0N/A System.arraycopy(v, 0, tmp, 0, v.length);
0N/A
0N/A // Add dot if previously determined to be necessary.
0N/A int start = v.length;
0N/A if (needDot) {
0N/A tmp[v.length] = '.';
0N/A start++;
0N/A }
0N/A
0N/A // Add zeros.
0N/A for (int j = start; j < tmp.length; j++)
0N/A tmp[j] = '0';
0N/A
0N/A return tmp;
0N/A }
0N/A
0N/A // Method assumes that d > 0.
0N/A private String hexDouble(double d, int prec) {
0N/A // Let Double.toHexString handle simple cases
0N/A if(!FpUtils.isFinite(d) || d == 0.0 || prec == 0 || prec >= 13)
0N/A // remove "0x"
0N/A return Double.toHexString(d).substring(2);
0N/A else {
0N/A assert(prec >= 1 && prec <= 12);
0N/A
0N/A int exponent = FpUtils.getExponent(d);
0N/A boolean subnormal
0N/A = (exponent == DoubleConsts.MIN_EXPONENT - 1);
0N/A
0N/A // If this is subnormal input so normalize (could be faster to
0N/A // do as integer operation).
0N/A if (subnormal) {
0N/A scaleUp = FpUtils.scalb(1.0, 54);
0N/A d *= scaleUp;
0N/A // Calculate the exponent. This is not just exponent + 54
0N/A // since the former is not the normalized exponent.
0N/A exponent = FpUtils.getExponent(d);
0N/A assert exponent >= DoubleConsts.MIN_EXPONENT &&
0N/A exponent <= DoubleConsts.MAX_EXPONENT: exponent;
0N/A }
0N/A
0N/A int precision = 1 + prec*4;
0N/A int shiftDistance
0N/A = DoubleConsts.SIGNIFICAND_WIDTH - precision;
0N/A assert(shiftDistance >= 1 && shiftDistance < DoubleConsts.SIGNIFICAND_WIDTH);
0N/A
0N/A long doppel = Double.doubleToLongBits(d);
0N/A // Deterime the number of bits to keep.
0N/A long newSignif
0N/A = (doppel & (DoubleConsts.EXP_BIT_MASK
0N/A | DoubleConsts.SIGNIF_BIT_MASK))
0N/A >> shiftDistance;
0N/A // Bits to round away.
0N/A long roundingBits = doppel & ~(~0L << shiftDistance);
0N/A
0N/A // To decide how to round, look at the low-order bit of the
0N/A // working significand, the highest order discarded bit (the
0N/A // round bit) and whether any of the lower order discarded bits
0N/A // are nonzero (the sticky bit).
0N/A
0N/A boolean leastZero = (newSignif & 0x1L) == 0L;
0N/A boolean round
0N/A = ((1L << (shiftDistance - 1) ) & roundingBits) != 0L;
0N/A boolean sticky = shiftDistance > 1 &&
0N/A (~(1L<< (shiftDistance - 1)) & roundingBits) != 0;
0N/A if((leastZero && round && sticky) || (!leastZero && round)) {
0N/A newSignif++;
0N/A }
0N/A
0N/A long signBit = doppel & DoubleConsts.SIGN_BIT_MASK;
0N/A newSignif = signBit | (newSignif << shiftDistance);
0N/A double result = Double.longBitsToDouble(newSignif);
0N/A
0N/A if (Double.isInfinite(result) ) {
0N/A // Infinite result generated by rounding
0N/A return "1.0p1024";
0N/A } else {
0N/A String res = Double.toHexString(result).substring(2);
0N/A if (!subnormal)
0N/A return res;
0N/A else {
0N/A // Create a normalized subnormal string.
0N/A int idx = res.indexOf('p');
0N/A if (idx == -1) {
0N/A // No 'p' character in hex string.
0N/A assert false;
0N/A return null;
0N/A } else {
0N/A // Get exponent and append at the end.
0N/A String exp = res.substring(idx + 1);
0N/A int iexp = Integer.parseInt(exp) -54;
0N/A return res.substring(0, idx) + "p"
0N/A + Integer.toString(iexp);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void print(BigDecimal value, Locale l) throws IOException {
0N/A if (c == Conversion.HEXADECIMAL_FLOAT)
0N/A failConversion(c, value);
0N/A StringBuilder sb = new StringBuilder();
0N/A boolean neg = value.signum() == -1;
0N/A BigDecimal v = value.abs();
0N/A // leading sign indicator
0N/A leadingSign(sb, neg);
0N/A
0N/A // the value
0N/A print(sb, v, l, f, c, precision, neg);
0N/A
0N/A // trailing sign indicator
0N/A trailingSign(sb, neg);
0N/A
0N/A // justify based on width
0N/A a.append(justify(sb.toString()));
0N/A }
0N/A
0N/A // value > 0
0N/A private void print(StringBuilder sb, BigDecimal value, Locale l,
0N/A Flags f, char c, int precision, boolean neg)
0N/A throws IOException
0N/A {
0N/A if (c == Conversion.SCIENTIFIC) {
0N/A // Create a new BigDecimal with the desired precision.
0N/A int prec = (precision == -1 ? 6 : precision);
0N/A int scale = value.scale();
0N/A int origPrec = value.precision();
0N/A int nzeros = 0;
0N/A int compPrec;
0N/A
0N/A if (prec > origPrec - 1) {
0N/A compPrec = origPrec;
0N/A nzeros = prec - (origPrec - 1);
0N/A } else {
0N/A compPrec = prec + 1;
0N/A }
0N/A
0N/A MathContext mc = new MathContext(compPrec);
0N/A BigDecimal v
0N/A = new BigDecimal(value.unscaledValue(), scale, mc);
0N/A
0N/A BigDecimalLayout bdl
0N/A = new BigDecimalLayout(v.unscaledValue(), v.scale(),
0N/A BigDecimalLayoutForm.SCIENTIFIC);
0N/A
0N/A char[] mant = bdl.mantissa();
0N/A
0N/A // Add a decimal point if necessary. The mantissa may not
0N/A // contain a decimal point if the scale is zero (the internal
0N/A // representation has no fractional part) or the original
0N/A // precision is one. Append a decimal point if '#' is set or if
0N/A // we require zero padding to get to the requested precision.
0N/A if ((origPrec == 1 || !bdl.hasDot())
0N/A && (nzeros > 0 || (f.contains(Flags.ALTERNATE))))
0N/A mant = addDot(mant);
0N/A
0N/A // Add trailing zeros in the case precision is greater than
0N/A // the number of available digits after the decimal separator.
0N/A mant = trailingZeros(mant, nzeros);
0N/A
0N/A char[] exp = bdl.exponent();
0N/A int newW = width;
0N/A if (width != -1)
0N/A newW = adjustWidth(width - exp.length - 1, f, neg);
0N/A localizedMagnitude(sb, mant, f, newW, l);
0N/A
0N/A sb.append(f.contains(Flags.UPPERCASE) ? 'E' : 'e');
0N/A
0N/A Flags flags = f.dup().remove(Flags.GROUP);
0N/A char sign = exp[0];
0N/A assert(sign == '+' || sign == '-');
0N/A sb.append(exp[0]);
0N/A
0N/A char[] tmp = new char[exp.length - 1];
0N/A System.arraycopy(exp, 1, tmp, 0, exp.length - 1);
0N/A sb.append(localizedMagnitude(null, tmp, flags, -1, l));
0N/A } else if (c == Conversion.DECIMAL_FLOAT) {
0N/A // Create a new BigDecimal with the desired precision.
0N/A int prec = (precision == -1 ? 6 : precision);
0N/A int scale = value.scale();
806N/A
806N/A if (scale > prec) {
3770N/A // more "scale" digits than the requested "precision"
806N/A int compPrec = value.precision();
806N/A if (compPrec <= scale) {
806N/A // case of 0.xxxxxx
806N/A value = value.setScale(prec, RoundingMode.HALF_UP);
806N/A } else {
806N/A compPrec -= (scale - prec);
806N/A value = new BigDecimal(value.unscaledValue(),
806N/A scale,
806N/A new MathContext(compPrec));
806N/A }
806N/A }
806N/A BigDecimalLayout bdl = new BigDecimalLayout(
806N/A value.unscaledValue(),
806N/A value.scale(),
0N/A BigDecimalLayoutForm.DECIMAL_FLOAT);
0N/A
0N/A char mant[] = bdl.mantissa();
0N/A int nzeros = (bdl.scale() < prec ? prec - bdl.scale() : 0);
0N/A
0N/A // Add a decimal point if necessary. The mantissa may not
0N/A // contain a decimal point if the scale is zero (the internal
0N/A // representation has no fractional part). Append a decimal
0N/A // point if '#' is set or we require zero padding to get to the
0N/A // requested precision.
0N/A if (bdl.scale() == 0 && (f.contains(Flags.ALTERNATE) || nzeros > 0))
0N/A mant = addDot(bdl.mantissa());
0N/A
0N/A // Add trailing zeros if the precision is greater than the
0N/A // number of available digits after the decimal separator.
0N/A mant = trailingZeros(mant, nzeros);
0N/A
0N/A localizedMagnitude(sb, mant, f, adjustWidth(width, f, neg), l);
0N/A } else if (c == Conversion.GENERAL) {
0N/A int prec = precision;
0N/A if (precision == -1)
0N/A prec = 6;
0N/A else if (precision == 0)
0N/A prec = 1;
0N/A
0N/A BigDecimal tenToTheNegFour = BigDecimal.valueOf(1, 4);
0N/A BigDecimal tenToThePrec = BigDecimal.valueOf(1, -prec);
0N/A if ((value.equals(BigDecimal.ZERO))
0N/A || ((value.compareTo(tenToTheNegFour) != -1)
0N/A && (value.compareTo(tenToThePrec) == -1))) {
0N/A
0N/A int e = - value.scale()
0N/A + (value.unscaledValue().toString().length() - 1);
0N/A
0N/A // xxx.yyy
0N/A // g precision (# sig digits) = #x + #y
0N/A // f precision = #y
0N/A // exponent = #x - 1
0N/A // => f precision = g precision - exponent - 1
0N/A // 0.000zzz
0N/A // g precision (# sig digits) = #z
0N/A // f precision = #0 (after '.') + #z
0N/A // exponent = - #0 (after '.') - 1
0N/A // => f precision = g precision - exponent - 1
0N/A prec = prec - e - 1;
0N/A
0N/A print(sb, value, l, f, Conversion.DECIMAL_FLOAT, prec,
0N/A neg);
0N/A } else {
0N/A print(sb, value, l, f, Conversion.SCIENTIFIC, prec - 1, neg);
0N/A }
0N/A } else if (c == Conversion.HEXADECIMAL_FLOAT) {
0N/A // This conversion isn't supported. The error should be
0N/A // reported earlier.
0N/A assert false;
0N/A }
0N/A }
0N/A
0N/A private class BigDecimalLayout {
0N/A private StringBuilder mant;
0N/A private StringBuilder exp;
0N/A private boolean dot = false;
0N/A private int scale;
0N/A
0N/A public BigDecimalLayout(BigInteger intVal, int scale, BigDecimalLayoutForm form) {
0N/A layout(intVal, scale, form);
0N/A }
0N/A
0N/A public boolean hasDot() {
0N/A return dot;
0N/A }
0N/A
0N/A public int scale() {
0N/A return scale;
0N/A }
0N/A
0N/A // char[] with canonical string representation
0N/A public char[] layoutChars() {
0N/A StringBuilder sb = new StringBuilder(mant);
0N/A if (exp != null) {
0N/A sb.append('E');
0N/A sb.append(exp);
0N/A }
0N/A return toCharArray(sb);
0N/A }
0N/A
0N/A public char[] mantissa() {
0N/A return toCharArray(mant);
0N/A }
0N/A
0N/A // The exponent will be formatted as a sign ('+' or '-') followed
0N/A // by the exponent zero-padded to include at least two digits.
0N/A public char[] exponent() {
0N/A return toCharArray(exp);
0N/A }
0N/A
0N/A private char[] toCharArray(StringBuilder sb) {
0N/A if (sb == null)
0N/A return null;
0N/A char[] result = new char[sb.length()];
0N/A sb.getChars(0, result.length, result, 0);
0N/A return result;
0N/A }
0N/A
0N/A private void layout(BigInteger intVal, int scale, BigDecimalLayoutForm form) {
0N/A char coeff[] = intVal.toString().toCharArray();
0N/A this.scale = scale;
0N/A
0N/A // Construct a buffer, with sufficient capacity for all cases.
0N/A // If E-notation is needed, length will be: +1 if negative, +1
0N/A // if '.' needed, +2 for "E+", + up to 10 for adjusted
0N/A // exponent. Otherwise it could have +1 if negative, plus
0N/A // leading "0.00000"
0N/A mant = new StringBuilder(coeff.length + 14);
0N/A
0N/A if (scale == 0) {
0N/A int len = coeff.length;
0N/A if (len > 1) {
0N/A mant.append(coeff[0]);
0N/A if (form == BigDecimalLayoutForm.SCIENTIFIC) {
0N/A mant.append('.');
0N/A dot = true;
0N/A mant.append(coeff, 1, len - 1);
0N/A exp = new StringBuilder("+");
0N/A if (len < 10)
0N/A exp.append("0").append(len - 1);
0N/A else
0N/A exp.append(len - 1);
0N/A } else {
0N/A mant.append(coeff, 1, len - 1);
0N/A }
0N/A } else {
0N/A mant.append(coeff);
0N/A if (form == BigDecimalLayoutForm.SCIENTIFIC)
0N/A exp = new StringBuilder("+00");
0N/A }
0N/A return;
0N/A }
0N/A long adjusted = -(long) scale + (coeff.length - 1);
0N/A if (form == BigDecimalLayoutForm.DECIMAL_FLOAT) {
0N/A // count of padding zeros
0N/A int pad = scale - coeff.length;
0N/A if (pad >= 0) {
0N/A // 0.xxx form
0N/A mant.append("0.");
0N/A dot = true;
0N/A for (; pad > 0 ; pad--) mant.append('0');
0N/A mant.append(coeff);
0N/A } else {
0N/A if (-pad < coeff.length) {
0N/A // xx.xx form
0N/A mant.append(coeff, 0, -pad);
0N/A mant.append('.');
0N/A dot = true;
0N/A mant.append(coeff, -pad, scale);
0N/A } else {
0N/A // xx form
0N/A mant.append(coeff, 0, coeff.length);
0N/A for (int i = 0; i < -scale; i++)
0N/A mant.append('0');
0N/A this.scale = 0;
0N/A }
0N/A }
0N/A } else {
0N/A // x.xxx form
0N/A mant.append(coeff[0]);
0N/A if (coeff.length > 1) {
0N/A mant.append('.');
0N/A dot = true;
0N/A mant.append(coeff, 1, coeff.length-1);
0N/A }
0N/A exp = new StringBuilder();
0N/A if (adjusted != 0) {
0N/A long abs = Math.abs(adjusted);
0N/A // require sign
0N/A exp.append(adjusted < 0 ? '-' : '+');
0N/A if (abs < 10)
0N/A exp.append('0');
0N/A exp.append(abs);
0N/A } else {
0N/A exp.append("+00");
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private int adjustWidth(int width, Flags f, boolean neg) {
0N/A int newW = width;
0N/A if (newW != -1 && neg && f.contains(Flags.PARENTHESES))
0N/A newW--;
0N/A return newW;
0N/A }
0N/A
0N/A // Add a '.' to th mantissa if required
0N/A private char[] addDot(char[] mant) {
0N/A char[] tmp = mant;
0N/A tmp = new char[mant.length + 1];
0N/A System.arraycopy(mant, 0, tmp, 0, mant.length);
0N/A tmp[tmp.length - 1] = '.';
0N/A return tmp;
0N/A }
0N/A
0N/A // Add trailing zeros in the case precision is greater than the number
0N/A // of available digits after the decimal separator.
0N/A private char[] trailingZeros(char[] mant, int nzeros) {
0N/A char[] tmp = mant;
0N/A if (nzeros > 0) {
0N/A tmp = new char[mant.length + nzeros];
0N/A System.arraycopy(mant, 0, tmp, 0, mant.length);
0N/A for (int i = mant.length; i < tmp.length; i++)
0N/A tmp[i] = '0';
0N/A }
0N/A return tmp;
0N/A }
0N/A
0N/A private void print(Calendar t, char c, Locale l) throws IOException
0N/A {
0N/A StringBuilder sb = new StringBuilder();
0N/A print(sb, t, c, l);
0N/A
0N/A // justify based on width
0N/A String s = justify(sb.toString());
0N/A if (f.contains(Flags.UPPERCASE))
0N/A s = s.toUpperCase();
0N/A
0N/A a.append(s);
0N/A }
0N/A
0N/A private Appendable print(StringBuilder sb, Calendar t, char c,
0N/A Locale l)
0N/A throws IOException
0N/A {
0N/A assert(width == -1);
0N/A if (sb == null)
0N/A sb = new StringBuilder();
0N/A switch (c) {
0N/A case DateTime.HOUR_OF_DAY_0: // 'H' (00 - 23)
0N/A case DateTime.HOUR_0: // 'I' (01 - 12)
0N/A case DateTime.HOUR_OF_DAY: // 'k' (0 - 23) -- like H
0N/A case DateTime.HOUR: { // 'l' (1 - 12) -- like I
0N/A int i = t.get(Calendar.HOUR_OF_DAY);
0N/A if (c == DateTime.HOUR_0 || c == DateTime.HOUR)
0N/A i = (i == 0 || i == 12 ? 12 : i % 12);
0N/A Flags flags = (c == DateTime.HOUR_OF_DAY_0
0N/A || c == DateTime.HOUR_0
0N/A ? Flags.ZERO_PAD
0N/A : Flags.NONE);
0N/A sb.append(localizedMagnitude(null, i, flags, 2, l));
0N/A break;
0N/A }
0N/A case DateTime.MINUTE: { // 'M' (00 - 59)
0N/A int i = t.get(Calendar.MINUTE);
0N/A Flags flags = Flags.ZERO_PAD;
0N/A sb.append(localizedMagnitude(null, i, flags, 2, l));
0N/A break;
0N/A }
0N/A case DateTime.NANOSECOND: { // 'N' (000000000 - 999999999)
0N/A int i = t.get(Calendar.MILLISECOND) * 1000000;
0N/A Flags flags = Flags.ZERO_PAD;
0N/A sb.append(localizedMagnitude(null, i, flags, 9, l));
0N/A break;
0N/A }
0N/A case DateTime.MILLISECOND: { // 'L' (000 - 999)
0N/A int i = t.get(Calendar.MILLISECOND);
0N/A Flags flags = Flags.ZERO_PAD;
0N/A sb.append(localizedMagnitude(null, i, flags, 3, l));
0N/A break;
0N/A }
0N/A case DateTime.MILLISECOND_SINCE_EPOCH: { // 'Q' (0 - 99...?)
0N/A long i = t.getTimeInMillis();
0N/A Flags flags = Flags.NONE;
0N/A sb.append(localizedMagnitude(null, i, flags, width, l));
0N/A break;
0N/A }
0N/A case DateTime.AM_PM: { // 'p' (am or pm)
0N/A // Calendar.AM = 0, Calendar.PM = 1, LocaleElements defines upper
0N/A String[] ampm = { "AM", "PM" };
0N/A if (l != null && l != Locale.US) {
0N/A DateFormatSymbols dfs = DateFormatSymbols.getInstance(l);
0N/A ampm = dfs.getAmPmStrings();
0N/A }
0N/A String s = ampm[t.get(Calendar.AM_PM)];
0N/A sb.append(s.toLowerCase(l != null ? l : Locale.US));
0N/A break;
0N/A }
0N/A case DateTime.SECONDS_SINCE_EPOCH: { // 's' (0 - 99...?)
0N/A long i = t.getTimeInMillis() / 1000;
0N/A Flags flags = Flags.NONE;
0N/A sb.append(localizedMagnitude(null, i, flags, width, l));
0N/A break;
0N/A }
0N/A case DateTime.SECOND: { // 'S' (00 - 60 - leap second)
0N/A int i = t.get(Calendar.SECOND);
0N/A Flags flags = Flags.ZERO_PAD;
0N/A sb.append(localizedMagnitude(null, i, flags, 2, l));
0N/A break;
0N/A }
0N/A case DateTime.ZONE_NUMERIC: { // 'z' ({-|+}####) - ls minus?
0N/A int i = t.get(Calendar.ZONE_OFFSET) + t.get(Calendar.DST_OFFSET);
0N/A boolean neg = i < 0;
0N/A sb.append(neg ? '-' : '+');
0N/A if (neg)
0N/A i = -i;
0N/A int min = i / 60000;
0N/A // combine minute and hour into a single integer
0N/A int offset = (min / 60) * 100 + (min % 60);
0N/A Flags flags = Flags.ZERO_PAD;
0N/A
0N/A sb.append(localizedMagnitude(null, offset, flags, 4, l));
0N/A break;
0N/A }
0N/A case DateTime.ZONE: { // 'Z' (symbol)
0N/A TimeZone tz = t.getTimeZone();
0N/A sb.append(tz.getDisplayName((t.get(Calendar.DST_OFFSET) != 0),
0N/A TimeZone.SHORT,
0N/A (l == null) ? Locale.US : l));
0N/A break;
0N/A }
0N/A
0N/A // Date
0N/A case DateTime.NAME_OF_DAY_ABBREV: // 'a'
0N/A case DateTime.NAME_OF_DAY: { // 'A'
0N/A int i = t.get(Calendar.DAY_OF_WEEK);
0N/A Locale lt = ((l == null) ? Locale.US : l);
0N/A DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
0N/A if (c == DateTime.NAME_OF_DAY)
0N/A sb.append(dfs.getWeekdays()[i]);
0N/A else
0N/A sb.append(dfs.getShortWeekdays()[i]);
0N/A break;
0N/A }
0N/A case DateTime.NAME_OF_MONTH_ABBREV: // 'b'
0N/A case DateTime.NAME_OF_MONTH_ABBREV_X: // 'h' -- same b
0N/A case DateTime.NAME_OF_MONTH: { // 'B'
0N/A int i = t.get(Calendar.MONTH);
0N/A Locale lt = ((l == null) ? Locale.US : l);
0N/A DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
0N/A if (c == DateTime.NAME_OF_MONTH)
0N/A sb.append(dfs.getMonths()[i]);
0N/A else
0N/A sb.append(dfs.getShortMonths()[i]);
0N/A break;
0N/A }
0N/A case DateTime.CENTURY: // 'C' (00 - 99)
0N/A case DateTime.YEAR_2: // 'y' (00 - 99)
0N/A case DateTime.YEAR_4: { // 'Y' (0000 - 9999)
0N/A int i = t.get(Calendar.YEAR);
0N/A int size = 2;
0N/A switch (c) {
0N/A case DateTime.CENTURY:
0N/A i /= 100;
0N/A break;
0N/A case DateTime.YEAR_2:
0N/A i %= 100;
0N/A break;
0N/A case DateTime.YEAR_4:
0N/A size = 4;
0N/A break;
0N/A }
0N/A Flags flags = Flags.ZERO_PAD;
0N/A sb.append(localizedMagnitude(null, i, flags, size, l));
0N/A break;
0N/A }
0N/A case DateTime.DAY_OF_MONTH_0: // 'd' (01 - 31)
0N/A case DateTime.DAY_OF_MONTH: { // 'e' (1 - 31) -- like d
0N/A int i = t.get(Calendar.DATE);
0N/A Flags flags = (c == DateTime.DAY_OF_MONTH_0
0N/A ? Flags.ZERO_PAD
0N/A : Flags.NONE);
0N/A sb.append(localizedMagnitude(null, i, flags, 2, l));
0N/A break;
0N/A }
0N/A case DateTime.DAY_OF_YEAR: { // 'j' (001 - 366)
0N/A int i = t.get(Calendar.DAY_OF_YEAR);
0N/A Flags flags = Flags.ZERO_PAD;
0N/A sb.append(localizedMagnitude(null, i, flags, 3, l));
0N/A break;
0N/A }
0N/A case DateTime.MONTH: { // 'm' (01 - 12)
0N/A int i = t.get(Calendar.MONTH) + 1;
0N/A Flags flags = Flags.ZERO_PAD;
0N/A sb.append(localizedMagnitude(null, i, flags, 2, l));
0N/A break;
0N/A }
0N/A
0N/A // Composites
0N/A case DateTime.TIME: // 'T' (24 hour hh:mm:ss - %tH:%tM:%tS)
0N/A case DateTime.TIME_24_HOUR: { // 'R' (hh:mm same as %H:%M)
0N/A char sep = ':';
0N/A print(sb, t, DateTime.HOUR_OF_DAY_0, l).append(sep);
0N/A print(sb, t, DateTime.MINUTE, l);
0N/A if (c == DateTime.TIME) {
0N/A sb.append(sep);
0N/A print(sb, t, DateTime.SECOND, l);
0N/A }
0N/A break;
0N/A }
0N/A case DateTime.TIME_12_HOUR: { // 'r' (hh:mm:ss [AP]M)
0N/A char sep = ':';
0N/A print(sb, t, DateTime.HOUR_0, l).append(sep);
0N/A print(sb, t, DateTime.MINUTE, l).append(sep);
0N/A print(sb, t, DateTime.SECOND, l).append(' ');
0N/A // this may be in wrong place for some locales
0N/A StringBuilder tsb = new StringBuilder();
0N/A print(tsb, t, DateTime.AM_PM, l);
0N/A sb.append(tsb.toString().toUpperCase(l != null ? l : Locale.US));
0N/A break;
0N/A }
0N/A case DateTime.DATE_TIME: { // 'c' (Sat Nov 04 12:02:33 EST 1999)
0N/A char sep = ' ';
0N/A print(sb, t, DateTime.NAME_OF_DAY_ABBREV, l).append(sep);
0N/A print(sb, t, DateTime.NAME_OF_MONTH_ABBREV, l).append(sep);
0N/A print(sb, t, DateTime.DAY_OF_MONTH_0, l).append(sep);
0N/A print(sb, t, DateTime.TIME, l).append(sep);
0N/A print(sb, t, DateTime.ZONE, l).append(sep);
0N/A print(sb, t, DateTime.YEAR_4, l);
0N/A break;
0N/A }
0N/A case DateTime.DATE: { // 'D' (mm/dd/yy)
0N/A char sep = '/';
0N/A print(sb, t, DateTime.MONTH, l).append(sep);
0N/A print(sb, t, DateTime.DAY_OF_MONTH_0, l).append(sep);
0N/A print(sb, t, DateTime.YEAR_2, l);
0N/A break;
0N/A }
0N/A case DateTime.ISO_STANDARD_DATE: { // 'F' (%Y-%m-%d)
0N/A char sep = '-';
0N/A print(sb, t, DateTime.YEAR_4, l).append(sep);
0N/A print(sb, t, DateTime.MONTH, l).append(sep);
0N/A print(sb, t, DateTime.DAY_OF_MONTH_0, l);
0N/A break;
0N/A }
0N/A default:
0N/A assert false;
0N/A }
0N/A return sb;
0N/A }
0N/A
0N/A // -- Methods to support throwing exceptions --
0N/A
0N/A private void failMismatch(Flags f, char c) {
0N/A String fs = f.toString();
0N/A throw new FormatFlagsConversionMismatchException(fs, c);
0N/A }
0N/A
0N/A private void failConversion(char c, Object arg) {
0N/A throw new IllegalFormatConversionException(c, arg.getClass());
0N/A }
0N/A
0N/A private char getZero(Locale l) {
0N/A if ((l != null) && !l.equals(locale())) {
0N/A DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
0N/A return dfs.getZeroDigit();
0N/A }
0N/A return zero;
0N/A }
0N/A
0N/A private StringBuilder
0N/A localizedMagnitude(StringBuilder sb, long value, Flags f,
0N/A int width, Locale l)
0N/A {
0N/A char[] va = Long.toString(value, 10).toCharArray();
0N/A return localizedMagnitude(sb, va, f, width, l);
0N/A }
0N/A
0N/A private StringBuilder
0N/A localizedMagnitude(StringBuilder sb, char[] value, Flags f,
0N/A int width, Locale l)
0N/A {
0N/A if (sb == null)
0N/A sb = new StringBuilder();
0N/A int begin = sb.length();
0N/A
0N/A char zero = getZero(l);
0N/A
0N/A // determine localized grouping separator and size
0N/A char grpSep = '\0';
0N/A int grpSize = -1;
0N/A char decSep = '\0';
0N/A
0N/A int len = value.length;
0N/A int dot = len;
0N/A for (int j = 0; j < len; j++) {
0N/A if (value[j] == '.') {
0N/A dot = j;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (dot < len) {
0N/A if (l == null || l.equals(Locale.US)) {
0N/A decSep = '.';
0N/A } else {
0N/A DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
0N/A decSep = dfs.getDecimalSeparator();
0N/A }
0N/A }
0N/A
0N/A if (f.contains(Flags.GROUP)) {
0N/A if (l == null || l.equals(Locale.US)) {
0N/A grpSep = ',';
0N/A grpSize = 3;
0N/A } else {
0N/A DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
0N/A grpSep = dfs.getGroupingSeparator();
0N/A DecimalFormat df = (DecimalFormat) NumberFormat.getIntegerInstance(l);
0N/A grpSize = df.getGroupingSize();
0N/A }
0N/A }
0N/A
0N/A // localize the digits inserting group separators as necessary
0N/A for (int j = 0; j < len; j++) {
0N/A if (j == dot) {
0N/A sb.append(decSep);
0N/A // no more group separators after the decimal separator
0N/A grpSep = '\0';
0N/A continue;
0N/A }
0N/A
0N/A char c = value[j];
0N/A sb.append((char) ((c - '0') + zero));
0N/A if (grpSep != '\0' && j != dot - 1 && ((dot - j) % grpSize == 1))
0N/A sb.append(grpSep);
0N/A }
0N/A
0N/A // apply zero padding
0N/A len = sb.length();
0N/A if (width != -1 && f.contains(Flags.ZERO_PAD))
0N/A for (int k = 0; k < width - len; k++)
0N/A sb.insert(begin, zero);
0N/A
0N/A return sb;
0N/A }
0N/A }
0N/A
0N/A private static class Flags {
0N/A private int flags;
0N/A
0N/A static final Flags NONE = new Flags(0); // ''
0N/A
0N/A // duplicate declarations from Formattable.java
0N/A static final Flags LEFT_JUSTIFY = new Flags(1<<0); // '-'
0N/A static final Flags UPPERCASE = new Flags(1<<1); // '^'
0N/A static final Flags ALTERNATE = new Flags(1<<2); // '#'
0N/A
0N/A // numerics
0N/A static final Flags PLUS = new Flags(1<<3); // '+'
0N/A static final Flags LEADING_SPACE = new Flags(1<<4); // ' '
0N/A static final Flags ZERO_PAD = new Flags(1<<5); // '0'
0N/A static final Flags GROUP = new Flags(1<<6); // ','
0N/A static final Flags PARENTHESES = new Flags(1<<7); // '('
0N/A
0N/A // indexing
0N/A static final Flags PREVIOUS = new Flags(1<<8); // '<'
0N/A
0N/A private Flags(int f) {
0N/A flags = f;
0N/A }
0N/A
0N/A public int valueOf() {
0N/A return flags;
0N/A }
0N/A
0N/A public boolean contains(Flags f) {
0N/A return (flags & f.valueOf()) == f.valueOf();
0N/A }
0N/A
0N/A public Flags dup() {
0N/A return new Flags(flags);
0N/A }
0N/A
0N/A private Flags add(Flags f) {
0N/A flags |= f.valueOf();
0N/A return this;
0N/A }
0N/A
0N/A public Flags remove(Flags f) {
0N/A flags &= ~f.valueOf();
0N/A return this;
0N/A }
0N/A
0N/A public static Flags parse(String s) {
0N/A char[] ca = s.toCharArray();
0N/A Flags f = new Flags(0);
0N/A for (int i = 0; i < ca.length; i++) {
0N/A Flags v = parse(ca[i]);
0N/A if (f.contains(v))
0N/A throw new DuplicateFormatFlagsException(v.toString());
0N/A f.add(v);
0N/A }
0N/A return f;
0N/A }
0N/A
0N/A // parse those flags which may be provided by users
0N/A private static Flags parse(char c) {
0N/A switch (c) {
0N/A case '-': return LEFT_JUSTIFY;
0N/A case '#': return ALTERNATE;
0N/A case '+': return PLUS;
0N/A case ' ': return LEADING_SPACE;
0N/A case '0': return ZERO_PAD;
0N/A case ',': return GROUP;
0N/A case '(': return PARENTHESES;
0N/A case '<': return PREVIOUS;
0N/A default:
0N/A throw new UnknownFormatFlagsException(String.valueOf(c));
0N/A }
0N/A }
0N/A
847N/A // Returns a string representation of the current {@code Flags}.
0N/A public static String toString(Flags f) {
0N/A return f.toString();
0N/A }
0N/A
0N/A public String toString() {
0N/A StringBuilder sb = new StringBuilder();
0N/A if (contains(LEFT_JUSTIFY)) sb.append('-');
0N/A if (contains(UPPERCASE)) sb.append('^');
0N/A if (contains(ALTERNATE)) sb.append('#');
0N/A if (contains(PLUS)) sb.append('+');
0N/A if (contains(LEADING_SPACE)) sb.append(' ');
0N/A if (contains(ZERO_PAD)) sb.append('0');
0N/A if (contains(GROUP)) sb.append(',');
0N/A if (contains(PARENTHESES)) sb.append('(');
0N/A if (contains(PREVIOUS)) sb.append('<');
0N/A return sb.toString();
0N/A }
0N/A }
0N/A
0N/A private static class Conversion {
0N/A // Byte, Short, Integer, Long, BigInteger
0N/A // (and associated primitives due to autoboxing)
0N/A static final char DECIMAL_INTEGER = 'd';
0N/A static final char OCTAL_INTEGER = 'o';
0N/A static final char HEXADECIMAL_INTEGER = 'x';
0N/A static final char HEXADECIMAL_INTEGER_UPPER = 'X';
0N/A
0N/A // Float, Double, BigDecimal
0N/A // (and associated primitives due to autoboxing)
0N/A static final char SCIENTIFIC = 'e';
0N/A static final char SCIENTIFIC_UPPER = 'E';
0N/A static final char GENERAL = 'g';
0N/A static final char GENERAL_UPPER = 'G';
0N/A static final char DECIMAL_FLOAT = 'f';
0N/A static final char HEXADECIMAL_FLOAT = 'a';
0N/A static final char HEXADECIMAL_FLOAT_UPPER = 'A';
0N/A
0N/A // Character, Byte, Short, Integer
0N/A // (and associated primitives due to autoboxing)
0N/A static final char CHARACTER = 'c';
0N/A static final char CHARACTER_UPPER = 'C';
0N/A
0N/A // java.util.Date, java.util.Calendar, long
0N/A static final char DATE_TIME = 't';
0N/A static final char DATE_TIME_UPPER = 'T';
0N/A
0N/A // if (arg.TYPE != boolean) return boolean
0N/A // if (arg != null) return true; else return false;
0N/A static final char BOOLEAN = 'b';
0N/A static final char BOOLEAN_UPPER = 'B';
0N/A // if (arg instanceof Formattable) arg.formatTo()
0N/A // else arg.toString();
0N/A static final char STRING = 's';
0N/A static final char STRING_UPPER = 'S';
0N/A // arg.hashCode()
0N/A static final char HASHCODE = 'h';
0N/A static final char HASHCODE_UPPER = 'H';
0N/A
0N/A static final char LINE_SEPARATOR = 'n';
0N/A static final char PERCENT_SIGN = '%';
0N/A
0N/A static boolean isValid(char c) {
0N/A return (isGeneral(c) || isInteger(c) || isFloat(c) || isText(c)
0N/A || c == 't' || isCharacter(c));
0N/A }
0N/A
0N/A // Returns true iff the Conversion is applicable to all objects.
0N/A static boolean isGeneral(char c) {
0N/A switch (c) {
0N/A case BOOLEAN:
0N/A case BOOLEAN_UPPER:
0N/A case STRING:
0N/A case STRING_UPPER:
0N/A case HASHCODE:
0N/A case HASHCODE_UPPER:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A // Returns true iff the Conversion is applicable to character.
0N/A static boolean isCharacter(char c) {
0N/A switch (c) {
0N/A case CHARACTER:
0N/A case CHARACTER_UPPER:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A // Returns true iff the Conversion is an integer type.
0N/A static boolean isInteger(char c) {
0N/A switch (c) {
0N/A case DECIMAL_INTEGER:
0N/A case OCTAL_INTEGER:
0N/A case HEXADECIMAL_INTEGER:
0N/A case HEXADECIMAL_INTEGER_UPPER:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A // Returns true iff the Conversion is a floating-point type.
0N/A static boolean isFloat(char c) {
0N/A switch (c) {
0N/A case SCIENTIFIC:
0N/A case SCIENTIFIC_UPPER:
0N/A case GENERAL:
0N/A case GENERAL_UPPER:
0N/A case DECIMAL_FLOAT:
0N/A case HEXADECIMAL_FLOAT:
0N/A case HEXADECIMAL_FLOAT_UPPER:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A // Returns true iff the Conversion does not require an argument
0N/A static boolean isText(char c) {
0N/A switch (c) {
0N/A case LINE_SEPARATOR:
0N/A case PERCENT_SIGN:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static class DateTime {
0N/A static final char HOUR_OF_DAY_0 = 'H'; // (00 - 23)
0N/A static final char HOUR_0 = 'I'; // (01 - 12)
0N/A static final char HOUR_OF_DAY = 'k'; // (0 - 23) -- like H
0N/A static final char HOUR = 'l'; // (1 - 12) -- like I
0N/A static final char MINUTE = 'M'; // (00 - 59)
0N/A static final char NANOSECOND = 'N'; // (000000000 - 999999999)
0N/A static final char MILLISECOND = 'L'; // jdk, not in gnu (000 - 999)
0N/A static final char MILLISECOND_SINCE_EPOCH = 'Q'; // (0 - 99...?)
0N/A static final char AM_PM = 'p'; // (am or pm)
0N/A static final char SECONDS_SINCE_EPOCH = 's'; // (0 - 99...?)
0N/A static final char SECOND = 'S'; // (00 - 60 - leap second)
0N/A static final char TIME = 'T'; // (24 hour hh:mm:ss)
0N/A static final char ZONE_NUMERIC = 'z'; // (-1200 - +1200) - ls minus?
0N/A static final char ZONE = 'Z'; // (symbol)
0N/A
0N/A // Date
0N/A static final char NAME_OF_DAY_ABBREV = 'a'; // 'a'
0N/A static final char NAME_OF_DAY = 'A'; // 'A'
0N/A static final char NAME_OF_MONTH_ABBREV = 'b'; // 'b'
0N/A static final char NAME_OF_MONTH = 'B'; // 'B'
0N/A static final char CENTURY = 'C'; // (00 - 99)
0N/A static final char DAY_OF_MONTH_0 = 'd'; // (01 - 31)
0N/A static final char DAY_OF_MONTH = 'e'; // (1 - 31) -- like d
0N/A// * static final char ISO_WEEK_OF_YEAR_2 = 'g'; // cross %y %V
0N/A// * static final char ISO_WEEK_OF_YEAR_4 = 'G'; // cross %Y %V
0N/A static final char NAME_OF_MONTH_ABBREV_X = 'h'; // -- same b
0N/A static final char DAY_OF_YEAR = 'j'; // (001 - 366)
0N/A static final char MONTH = 'm'; // (01 - 12)
0N/A// * static final char DAY_OF_WEEK_1 = 'u'; // (1 - 7) Monday
0N/A// * static final char WEEK_OF_YEAR_SUNDAY = 'U'; // (0 - 53) Sunday+
0N/A// * static final char WEEK_OF_YEAR_MONDAY_01 = 'V'; // (01 - 53) Monday+
0N/A// * static final char DAY_OF_WEEK_0 = 'w'; // (0 - 6) Sunday
0N/A// * static final char WEEK_OF_YEAR_MONDAY = 'W'; // (00 - 53) Monday
0N/A static final char YEAR_2 = 'y'; // (00 - 99)
0N/A static final char YEAR_4 = 'Y'; // (0000 - 9999)
0N/A
0N/A // Composites
0N/A static final char TIME_12_HOUR = 'r'; // (hh:mm:ss [AP]M)
0N/A static final char TIME_24_HOUR = 'R'; // (hh:mm same as %H:%M)
0N/A// * static final char LOCALE_TIME = 'X'; // (%H:%M:%S) - parse format?
0N/A static final char DATE_TIME = 'c';
0N/A // (Sat Nov 04 12:02:33 EST 1999)
0N/A static final char DATE = 'D'; // (mm/dd/yy)
0N/A static final char ISO_STANDARD_DATE = 'F'; // (%Y-%m-%d)
0N/A// * static final char LOCALE_DATE = 'x'; // (mm/dd/yy)
0N/A
0N/A static boolean isValid(char c) {
0N/A switch (c) {
0N/A case HOUR_OF_DAY_0:
0N/A case HOUR_0:
0N/A case HOUR_OF_DAY:
0N/A case HOUR:
0N/A case MINUTE:
0N/A case NANOSECOND:
0N/A case MILLISECOND:
0N/A case MILLISECOND_SINCE_EPOCH:
0N/A case AM_PM:
0N/A case SECONDS_SINCE_EPOCH:
0N/A case SECOND:
0N/A case TIME:
0N/A case ZONE_NUMERIC:
0N/A case ZONE:
0N/A
0N/A // Date
0N/A case NAME_OF_DAY_ABBREV:
0N/A case NAME_OF_DAY:
0N/A case NAME_OF_MONTH_ABBREV:
0N/A case NAME_OF_MONTH:
0N/A case CENTURY:
0N/A case DAY_OF_MONTH_0:
0N/A case DAY_OF_MONTH:
0N/A// * case ISO_WEEK_OF_YEAR_2:
0N/A// * case ISO_WEEK_OF_YEAR_4:
0N/A case NAME_OF_MONTH_ABBREV_X:
0N/A case DAY_OF_YEAR:
0N/A case MONTH:
0N/A// * case DAY_OF_WEEK_1:
0N/A// * case WEEK_OF_YEAR_SUNDAY:
0N/A// * case WEEK_OF_YEAR_MONDAY_01:
0N/A// * case DAY_OF_WEEK_0:
0N/A// * case WEEK_OF_YEAR_MONDAY:
0N/A case YEAR_2:
0N/A case YEAR_4:
0N/A
0N/A // Composites
0N/A case TIME_12_HOUR:
0N/A case TIME_24_HOUR:
0N/A// * case LOCALE_TIME:
0N/A case DATE_TIME:
0N/A case DATE:
0N/A case ISO_STANDARD_DATE:
0N/A// * case LOCALE_DATE:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A}