< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  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.TemporalQueries;

  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


4038                     sb.append(localizedMagnitude(null, i, Flags.NONE, 2, l));
4039                     break;
4040                 }
4041                 case DateTime.HOUR_0:      {  // 'I' (01 - 12)
4042                     int i = t.get(ChronoField.CLOCK_HOUR_OF_AMPM);
4043                     sb.append(localizedMagnitude(null, i, Flags.ZERO_PAD, 2, l));
4044                     break;
4045                 }
4046                 case DateTime.HOUR:        { // 'l' (1 - 12) -- like I
4047                     int i = t.get(ChronoField.CLOCK_HOUR_OF_AMPM);
4048                     sb.append(localizedMagnitude(null, i, Flags.NONE, 2, l));
4049                     break;
4050                 }
4051                 case DateTime.MINUTE:      { // 'M' (00 - 59)
4052                     int i = t.get(ChronoField.MINUTE_OF_HOUR);
4053                     Flags flags = Flags.ZERO_PAD;
4054                     sb.append(localizedMagnitude(null, i, flags, 2, l));
4055                     break;
4056                 }
4057                 case DateTime.NANOSECOND:  { // 'N' (000000000 - 999999999)
4058                     int i = t.get(ChronoField.MILLI_OF_SECOND) * 1000000;





4059                     Flags flags = Flags.ZERO_PAD;
4060                     sb.append(localizedMagnitude(null, i, flags, 9, l));
4061                     break;
4062                 }
4063                 case DateTime.MILLISECOND: { // 'L' (000 - 999)
4064                     int i = t.get(ChronoField.MILLI_OF_SECOND);
4065                     Flags flags = Flags.ZERO_PAD;
4066                     sb.append(localizedMagnitude(null, i, flags, 3, l));
4067                     break;
4068                 }
4069                 case DateTime.MILLISECOND_SINCE_EPOCH: { // 'Q' (0 - 99...?)
4070                     long i = t.getLong(ChronoField.INSTANT_SECONDS) * 1000L +
4071                              t.getLong(ChronoField.MILLI_OF_SECOND);
4072                     Flags flags = Flags.NONE;
4073                     sb.append(localizedMagnitude(null, i, flags, width, l));
4074                     break;
4075                 }
4076                 case DateTime.AM_PM:       { // 'p' (am or pm)
4077                     // Calendar.AM = 0, Calendar.PM = 1, LocaleElements defines upper
4078                     String[] ampm = { "AM", "PM" };


   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  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.TemporalQueries;
  60 import java.time.temporal.UnsupportedTemporalTypeException;
  61 
  62 import sun.misc.DoubleConsts;
  63 import sun.misc.FormattedFloatingDecimal;
  64 
  65 /**
  66  * An interpreter for printf-style format strings.  This class provides support
  67  * for layout justification and alignment, common formats for numeric, string,
  68  * and date/time data, and locale-specific output.  Common Java types such as
  69  * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
  70  * are supported.  Limited formatting customization for arbitrary user types is
  71  * provided through the {@link Formattable} interface.
  72  *
  73  * <p> Formatters are not necessarily safe for multithreaded access.  Thread
  74  * safety is optional and is the responsibility of users of methods in this
  75  * class.
  76  *
  77  * <p> Formatted printing for the Java language is heavily inspired by C's
  78  * {@code printf}.  Although the format strings are similar to C, some
  79  * customizations have been made to accommodate the Java language and exploit
  80  * some of its features.  Also, Java formatting is more strict than C's; for


4039                     sb.append(localizedMagnitude(null, i, Flags.NONE, 2, l));
4040                     break;
4041                 }
4042                 case DateTime.HOUR_0:      {  // 'I' (01 - 12)
4043                     int i = t.get(ChronoField.CLOCK_HOUR_OF_AMPM);
4044                     sb.append(localizedMagnitude(null, i, Flags.ZERO_PAD, 2, l));
4045                     break;
4046                 }
4047                 case DateTime.HOUR:        { // 'l' (1 - 12) -- like I
4048                     int i = t.get(ChronoField.CLOCK_HOUR_OF_AMPM);
4049                     sb.append(localizedMagnitude(null, i, Flags.NONE, 2, l));
4050                     break;
4051                 }
4052                 case DateTime.MINUTE:      { // 'M' (00 - 59)
4053                     int i = t.get(ChronoField.MINUTE_OF_HOUR);
4054                     Flags flags = Flags.ZERO_PAD;
4055                     sb.append(localizedMagnitude(null, i, flags, 2, l));
4056                     break;
4057                 }
4058                 case DateTime.NANOSECOND:  { // 'N' (000000000 - 999999999)
4059                     int i;
4060                     try {
4061                         i = t.get(ChronoField.NANO_OF_SECOND);
4062                     } catch (UnsupportedTemporalTypeException u) {
4063                         i = t.get(ChronoField.MILLI_OF_SECOND) * 1000000;
4064                     }
4065                     Flags flags = Flags.ZERO_PAD;
4066                     sb.append(localizedMagnitude(null, i, flags, 9, l));
4067                     break;
4068                 }
4069                 case DateTime.MILLISECOND: { // 'L' (000 - 999)
4070                     int i = t.get(ChronoField.MILLI_OF_SECOND);
4071                     Flags flags = Flags.ZERO_PAD;
4072                     sb.append(localizedMagnitude(null, i, flags, 3, l));
4073                     break;
4074                 }
4075                 case DateTime.MILLISECOND_SINCE_EPOCH: { // 'Q' (0 - 99...?)
4076                     long i = t.getLong(ChronoField.INSTANT_SECONDS) * 1000L +
4077                              t.getLong(ChronoField.MILLI_OF_SECOND);
4078                     Flags flags = Flags.NONE;
4079                     sb.append(localizedMagnitude(null, i, flags, width, l));
4080                     break;
4081                 }
4082                 case DateTime.AM_PM:       { // 'p' (am or pm)
4083                     // Calendar.AM = 0, Calendar.PM = 1, LocaleElements defines upper
4084                     String[] ampm = { "AM", "PM" };


< prev index next >