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