src/share/classes/java/util/Formatter.java

Print this page




  39 import java.math.BigDecimal;
  40 import java.math.BigInteger;
  41 import java.math.MathContext;
  42 import java.math.RoundingMode;
  43 import java.nio.charset.Charset;
  44 import java.nio.charset.IllegalCharsetNameException;
  45 import java.nio.charset.UnsupportedCharsetException;
  46 import java.text.DateFormatSymbols;
  47 import java.text.DecimalFormat;
  48 import java.text.DecimalFormatSymbols;
  49 import java.text.NumberFormat;
  50 import java.util.regex.Matcher;
  51 import java.util.regex.Pattern;
  52 
  53 import java.time.DateTimeException;
  54 import java.time.Instant;
  55 import java.time.ZoneId;
  56 import java.time.ZoneOffset;
  57 import java.time.temporal.ChronoField;
  58 import java.time.temporal.TemporalAccessor;
  59 import java.time.temporal.Queries;
  60 
  61 import sun.misc.DoubleConsts;
  62 import sun.misc.FormattedFloatingDecimal;
  63 
  64 /**
  65  * An interpreter for printf-style format strings.  This class provides support
  66  * for layout justification and alignment, common formats for numeric, string,
  67  * and date/time data, and locale-specific output.  Common Java types such as
  68  * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
  69  * are supported.  Limited formatting customization for arbitrary user types is
  70  * provided through the {@link Formattable} interface.
  71  *
  72  * <p> Formatters are not necessarily safe for multithreaded access.  Thread
  73  * safety is optional and is the responsibility of users of methods in this
  74  * class.
  75  *
  76  * <p> Formatted printing for the Java language is heavily inspired by C's
  77  * {@code printf}.  Although the format strings are similar to C, some
  78  * customizations have been made to accommodate the Java language and exploit
  79  * some of its features.  Also, Java formatting is more strict than C's; for


4143                 case DateTime.SECOND:      { // 'S' (00 - 60 - leap second)
4144                     int i = t.get(ChronoField.SECOND_OF_MINUTE);
4145                     Flags flags = Flags.ZERO_PAD;
4146                     sb.append(localizedMagnitude(null, i, flags, 2, l));
4147                     break;
4148                 }
4149                 case DateTime.ZONE_NUMERIC: { // 'z' ({-|+}####) - ls minus?
4150                     int i = t.get(ChronoField.OFFSET_SECONDS);
4151                     boolean neg = i < 0;
4152                     sb.append(neg ? '-' : '+');
4153                     if (neg)
4154                         i = -i;
4155                     int min = i / 60;
4156                     // combine minute and hour into a single integer
4157                     int offset = (min / 60) * 100 + (min % 60);
4158                     Flags flags = Flags.ZERO_PAD;
4159                     sb.append(localizedMagnitude(null, offset, flags, 4, l));
4160                     break;
4161                 }
4162                 case DateTime.ZONE:        { // 'Z' (symbol)
4163                     ZoneId zid = t.query(Queries.zone());
4164                     if (zid == null) {
4165                         throw new IllegalFormatConversionException(c, t.getClass());
4166                     }
4167                     if (!(zid instanceof ZoneOffset) &&
4168                         t.isSupported(ChronoField.INSTANT_SECONDS)) {
4169                         Instant instant = Instant.from(t);
4170                         sb.append(TimeZone.getTimeZone(zid.getId())
4171                                           .getDisplayName(zid.getRules().isDaylightSavings(instant),
4172                                                           TimeZone.SHORT,
4173                                                           (l == null) ? Locale.US : l));
4174                         break;
4175                     }
4176                     sb.append(zid.getId());
4177                     break;
4178                 }
4179                 // Date
4180                 case DateTime.NAME_OF_DAY_ABBREV:     // 'a'
4181                 case DateTime.NAME_OF_DAY:          { // 'A'
4182                     int i = t.get(ChronoField.DAY_OF_WEEK) % 7 + 1;
4183                     Locale lt = ((l == null) ? Locale.US : l);




  39 import java.math.BigDecimal;
  40 import java.math.BigInteger;
  41 import java.math.MathContext;
  42 import java.math.RoundingMode;
  43 import java.nio.charset.Charset;
  44 import java.nio.charset.IllegalCharsetNameException;
  45 import java.nio.charset.UnsupportedCharsetException;
  46 import java.text.DateFormatSymbols;
  47 import java.text.DecimalFormat;
  48 import java.text.DecimalFormatSymbols;
  49 import java.text.NumberFormat;
  50 import java.util.regex.Matcher;
  51 import java.util.regex.Pattern;
  52 
  53 import java.time.DateTimeException;
  54 import java.time.Instant;
  55 import java.time.ZoneId;
  56 import java.time.ZoneOffset;
  57 import java.time.temporal.ChronoField;
  58 import java.time.temporal.TemporalAccessor;
  59 import java.time.temporal.TemporalQuery;
  60 
  61 import sun.misc.DoubleConsts;
  62 import sun.misc.FormattedFloatingDecimal;
  63 
  64 /**
  65  * An interpreter for printf-style format strings.  This class provides support
  66  * for layout justification and alignment, common formats for numeric, string,
  67  * and date/time data, and locale-specific output.  Common Java types such as
  68  * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
  69  * are supported.  Limited formatting customization for arbitrary user types is
  70  * provided through the {@link Formattable} interface.
  71  *
  72  * <p> Formatters are not necessarily safe for multithreaded access.  Thread
  73  * safety is optional and is the responsibility of users of methods in this
  74  * class.
  75  *
  76  * <p> Formatted printing for the Java language is heavily inspired by C's
  77  * {@code printf}.  Although the format strings are similar to C, some
  78  * customizations have been made to accommodate the Java language and exploit
  79  * some of its features.  Also, Java formatting is more strict than C's; for


4143                 case DateTime.SECOND:      { // 'S' (00 - 60 - leap second)
4144                     int i = t.get(ChronoField.SECOND_OF_MINUTE);
4145                     Flags flags = Flags.ZERO_PAD;
4146                     sb.append(localizedMagnitude(null, i, flags, 2, l));
4147                     break;
4148                 }
4149                 case DateTime.ZONE_NUMERIC: { // 'z' ({-|+}####) - ls minus?
4150                     int i = t.get(ChronoField.OFFSET_SECONDS);
4151                     boolean neg = i < 0;
4152                     sb.append(neg ? '-' : '+');
4153                     if (neg)
4154                         i = -i;
4155                     int min = i / 60;
4156                     // combine minute and hour into a single integer
4157                     int offset = (min / 60) * 100 + (min % 60);
4158                     Flags flags = Flags.ZERO_PAD;
4159                     sb.append(localizedMagnitude(null, offset, flags, 4, l));
4160                     break;
4161                 }
4162                 case DateTime.ZONE:        { // 'Z' (symbol)
4163                     ZoneId zid = t.query(TemporalQuery.zone());
4164                     if (zid == null) {
4165                         throw new IllegalFormatConversionException(c, t.getClass());
4166                     }
4167                     if (!(zid instanceof ZoneOffset) &&
4168                         t.isSupported(ChronoField.INSTANT_SECONDS)) {
4169                         Instant instant = Instant.from(t);
4170                         sb.append(TimeZone.getTimeZone(zid.getId())
4171                                           .getDisplayName(zid.getRules().isDaylightSavings(instant),
4172                                                           TimeZone.SHORT,
4173                                                           (l == null) ? Locale.US : l));
4174                         break;
4175                     }
4176                     sb.append(zid.getId());
4177                     break;
4178                 }
4179                 // Date
4180                 case DateTime.NAME_OF_DAY_ABBREV:     // 'a'
4181                 case DateTime.NAME_OF_DAY:          { // 'A'
4182                     int i = t.get(ChronoField.DAY_OF_WEEK) % 7 + 1;
4183                     Locale lt = ((l == null) ? Locale.US : l);