src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58086 d52f77f0acb5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
57478
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time.format;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import static java.time.temporal.ChronoField.YEAR;
29877
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
    73
import static java.time.temporal.ChronoField.ERA;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.lang.ref.SoftReference;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.math.BigDecimal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.math.BigInteger;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.math.RoundingMode;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.text.ParsePosition;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.time.DateTimeException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.time.Instant;
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
    82
import java.time.LocalDate;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.time.LocalDateTime;
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
    84
import java.time.LocalTime;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.time.ZoneId;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.time.ZoneOffset;
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
    87
import java.time.chrono.ChronoLocalDate;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    88
import java.time.chrono.Chronology;
29877
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
    89
import java.time.chrono.Era;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    90
import java.time.chrono.IsoChronology;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
import java.time.format.DateTimeTextProvider.LocaleStore;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
import java.time.temporal.ChronoField;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    93
import java.time.temporal.IsoFields;
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
    94
import java.time.temporal.JulianFields;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
    97
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
import java.time.temporal.WeekFields;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
import java.time.zone.ZoneRulesProvider;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
import java.util.AbstractMap.SimpleImmutableEntry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
import java.util.ArrayList;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
import java.util.Arrays;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
import java.util.Collections;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
import java.util.Comparator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
import java.util.HashMap;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   108
import java.util.HashSet;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
import java.util.Iterator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
import java.util.LinkedHashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
import java.util.List;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
import java.util.Locale;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
import java.util.Map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
import java.util.Map.Entry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
import java.util.Set;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
import java.util.TimeZone;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
import java.util.concurrent.ConcurrentHashMap;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   119
import java.util.concurrent.ConcurrentMap;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
42360
5e60b1e45041 8075577: java.time does not support HOST provider
rgoel
parents: 40120
diff changeset
   121
import sun.text.spi.JavaTimeDateTimePatternProvider;
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   122
import sun.util.locale.provider.CalendarDataUtility;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   123
import sun.util.locale.provider.LocaleProviderAdapter;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
import sun.util.locale.provider.TimeZoneNameUtility;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
 * Builder to create date-time formatters.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
 * This allows a {@code DateTimeFormatter} to be created.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
 * All date-time formatters are created ultimately using this builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
 * The basic elements of date-time can all be added:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   133
 * <ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
 * <li>Value - a numeric value</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
 * <li>Fraction - a fractional value including the decimal place. Always use this when
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
 * outputting fractions to ensure that the fraction is parsed correctly</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
 * <li>Text - the textual equivalent for the value</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
 * <li>OffsetId/Offset - the {@linkplain ZoneOffset zone offset}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
 * <li>ZoneId - the {@linkplain ZoneId time-zone} id</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
 * <li>ZoneText - the name of the time-zone</li>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   141
 * <li>ChronologyId - the {@linkplain Chronology chronology} id</li>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   142
 * <li>ChronologyText - the name of the chronology</li>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
 * <li>Literal - a text literal</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
 * <li>Nested and Optional - formats can be nested or made optional</li>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   145
 * </ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
 * In addition, any of the elements may be decorated by padding, either with spaces or any other character.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
 * Finally, a shorthand pattern, mostly compatible with {@code java.text.SimpleDateFormat SimpleDateFormat}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
 * can be used, see {@link #appendPattern(String)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
 * In practice, this simply parses the pattern and calls other methods on the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   152
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
 * This class is a mutable builder intended for use from a single thread.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
public final class DateTimeFormatterBuilder {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * Query for a time-zone that is region-only.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
    private static final TemporalQuery<ZoneId> QUERY_REGION_ONLY = (temporal) -> {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
   163
        ZoneId zone = temporal.query(TemporalQueries.zoneId());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
        return (zone != null && zone instanceof ZoneOffset == false ? zone : null);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     * The currently active builder, used by the outermost builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
    private DateTimeFormatterBuilder active = this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     * The parent builder, null for the outermost builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
    private final DateTimeFormatterBuilder parent;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * The list of printers that will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
    private final List<DateTimePrinterParser> printerParsers = new ArrayList<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * Whether this builder produces an optional formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
    private final boolean optional;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * The width to pad the next field to.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
    private int padNextWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
     * The character to pad the next field with.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
    private char padNextChar;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
     * The index of the last variable width value parser.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
    private int valueParserIndex = -1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   197
     * Gets the formatting pattern for date and time styles for a locale and chronology.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   198
     * The locale and chronology are used to lookup the locale specific format
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   199
     * for the requested dateStyle and/or timeStyle.
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   200
     * <p>
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   201
     * If the locale contains the "rg" (region override)
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   202
     * <a href="../../util/Locale.html#def_locale_extension">Unicode extensions</a>,
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   203
     * the formatting pattern is overridden with the one appropriate for the region.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   204
     *
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
   205
     * @param dateStyle  the FormatStyle for the date, null for time-only pattern
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
   206
     * @param timeStyle  the FormatStyle for the time, null for date-only pattern
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   207
     * @param chrono  the Chronology, non-null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   208
     * @param locale  the locale, non-null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   209
     * @return the locale and Chronology specific formatting pattern
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   210
     * @throws IllegalArgumentException if both dateStyle and timeStyle are null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   211
     */
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   212
    public static String getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   213
            Chronology chrono, Locale locale) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   214
        Objects.requireNonNull(locale, "locale");
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   215
        Objects.requireNonNull(chrono, "chrono");
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   216
        if (dateStyle == null && timeStyle == null) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   217
            throw new IllegalArgumentException("Either dateStyle or timeStyle must be non-null");
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   218
        }
42360
5e60b1e45041 8075577: java.time does not support HOST provider
rgoel
parents: 40120
diff changeset
   219
        LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(JavaTimeDateTimePatternProvider.class, locale);
5e60b1e45041 8075577: java.time does not support HOST provider
rgoel
parents: 40120
diff changeset
   220
        JavaTimeDateTimePatternProvider provider = adapter.getJavaTimeDateTimePatternProvider();
5e60b1e45041 8075577: java.time does not support HOST provider
rgoel
parents: 40120
diff changeset
   221
        String pattern = provider.getJavaTimeDateTimePattern(convertStyle(timeStyle),
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   222
                         convertStyle(dateStyle), chrono.getCalendarType(),
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   223
                         CalendarDataUtility.findRegionOverride(locale));
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   224
        return pattern;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   225
    }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   226
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   227
    /**
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   228
     * Converts the given FormatStyle to the java.text.DateFormat style.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   229
     *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   230
     * @param style  the FormatStyle style
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   231
     * @return the int style, or -1 if style is null, indicating un-required
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   232
     */
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   233
    private static int convertStyle(FormatStyle style) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   234
        if (style == null) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   235
            return -1;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   236
        }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   237
        return style.ordinal();  // indices happen to align
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   238
    }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   239
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   240
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * Constructs a new instance of the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
    public DateTimeFormatterBuilder() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
        super();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
        parent = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
        optional = false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
     * Constructs a new instance of the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * @param parent  the parent builder, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * @param optional  whether the formatter is optional, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
    private DateTimeFormatterBuilder(DateTimeFormatterBuilder parent, boolean optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
        super();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
        this.parent = parent;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
        this.optional = optional;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * Changes the parse style to be case sensitive for the remainder of the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     * Parsing can be case sensitive or insensitive - by default it is case sensitive.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * This method allows the case sensitivity setting of parsing to be changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     * Calling this method changes the state of the builder such that all
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * subsequent builder method calls will parse text in case sensitive mode.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * See {@link #parseCaseInsensitive} for the opposite setting.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * The parse case sensitive/insensitive methods may be called at any point
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     * in the builder, thus the parser can swap between case parsing modes
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * multiple times during the parse.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
     * Since the default is case sensitive, this method should only be used after
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
     * a previous call to {@code #parseCaseInsensitive}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
    public DateTimeFormatterBuilder parseCaseSensitive() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
        appendInternal(SettingsParser.SENSITIVE);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * Changes the parse style to be case insensitive for the remainder of the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
     * Parsing can be case sensitive or insensitive - by default it is case sensitive.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * This method allows the case sensitivity setting of parsing to be changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
     * Calling this method changes the state of the builder such that all
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * subsequent builder method calls will parse text in case insensitive mode.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     * See {@link #parseCaseSensitive()} for the opposite setting.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * The parse case sensitive/insensitive methods may be called at any point
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     * in the builder, thus the parser can swap between case parsing modes
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * multiple times during the parse.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
    public DateTimeFormatterBuilder parseCaseInsensitive() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
        appendInternal(SettingsParser.INSENSITIVE);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
     * Changes the parse style to be strict for the remainder of the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
     * <p>
58086
d52f77f0acb5 8230136: DateTimeFormatterBuilder.FractionPrinterParser#parse fails to verify minWidth
naoto
parents: 57478
diff changeset
   309
     * Parsing can be strict or lenient - by default it is strict.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     * This controls the degree of flexibility in matching the text and sign styles.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     * When used, this method changes the parsing to be strict from this point onwards.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
     * As strict is the default, this is normally only needed after calling {@link #parseLenient()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     * The change will remain in force until the end of the formatter that is eventually
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     * constructed or until {@code parseLenient} is called.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
    public DateTimeFormatterBuilder parseStrict() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
        appendInternal(SettingsParser.STRICT);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     * Changes the parse style to be lenient for the remainder of the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     * Note that case sensitivity is set separately to this method.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
     * <p>
58086
d52f77f0acb5 8230136: DateTimeFormatterBuilder.FractionPrinterParser#parse fails to verify minWidth
naoto
parents: 57478
diff changeset
   328
     * Parsing can be strict or lenient - by default it is strict.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
     * This controls the degree of flexibility in matching the text and sign styles.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
     * Applications calling this method should typically also call {@link #parseCaseInsensitive()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     * When used, this method changes the parsing to be lenient from this point onwards.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
     * The change will remain in force until the end of the formatter that is eventually
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
     * constructed or until {@code parseStrict} is called.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
    public DateTimeFormatterBuilder parseLenient() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
        appendInternal(SettingsParser.LENIENT);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   345
     * Appends a default value for a field to the formatter for use in parsing.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   346
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   347
     * This appends an instruction to the builder to inject a default value
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   348
     * into the parsed result. This is especially useful in conjunction with
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   349
     * optional parts of the formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   350
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   351
     * For example, consider a formatter that parses the year, followed by
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   352
     * an optional month, with a further optional day-of-month. Using such a
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   353
     * formatter would require the calling code to check whether a full date,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   354
     * year-month or just a year had been parsed. This method can be used to
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   355
     * default the month and day-of-month to a sensible value, such as the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   356
     * first of the month, allowing the calling code to always get a date.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   357
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   358
     * During formatting, this method has no effect.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   359
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   360
     * During parsing, the current state of the parse is inspected.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   361
     * If the specified field has no associated value, because it has not been
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   362
     * parsed successfully at that point, then the specified value is injected
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   363
     * into the parse result. Injection is immediate, thus the field-value pair
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   364
     * will be visible to any subsequent elements in the formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   365
     * As such, this method is normally called at the end of the builder.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   366
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   367
     * @param field  the field to default the value of, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   368
     * @param value  the value to default the field to
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   369
     * @return this, for chaining, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   370
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   371
    public DateTimeFormatterBuilder parseDefaulting(TemporalField field, long value) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   372
        Objects.requireNonNull(field, "field");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   373
        appendInternal(new DefaultValueParser(field, value));
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   374
        return this;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   375
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   376
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   377
    //-----------------------------------------------------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   378
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
     * Appends the value of a date-time field to the formatter using a normal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
     * output style.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   382
     * The value of the field will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
     * If the value cannot be obtained then an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   385
     * The value will be printed as per the normal format of an integer value.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     * Only negative numbers will be signed. No padding will be added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     * The parser for a variable width value such as this normally behaves greedily,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
     * requiring one digit, but accepting as many digits as possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
     * This behavior can be affected by 'adjacent value parsing'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
     * See {@link #appendValue(java.time.temporal.TemporalField, int)} for full details.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     * @param field  the field to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
    public DateTimeFormatterBuilder appendValue(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
        Objects.requireNonNull(field, "field");
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   398
        appendValue(new NumberPrinterParser(field, 1, 19, SignStyle.NORMAL));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
     * Appends the value of a date-time field to the formatter using a fixed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
     * width, zero-padded approach.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   406
     * The value of the field will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
     * If the value cannot be obtained then an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * The value will be zero-padded on the left. If the size of the value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * means that it cannot be printed within the width then an exception is thrown.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   411
     * If the value of the field is negative then an exception is thrown during formatting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * This method supports a special technique of parsing known as 'adjacent value parsing'.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   414
     * This technique solves the problem where a value, variable or fixed width, is followed by one or more
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * fixed length values. The standard parser is greedy, and thus it would normally
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * steal the digits that are needed by the fixed width value parsers that follow the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * variable width one.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * No action is required to initiate 'adjacent value parsing'.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   420
     * When a call to {@code appendValue} is made, the builder
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     * enters adjacent value parsing setup mode. If the immediately subsequent method
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   422
     * call or calls on the same builder are for a fixed width value, then the parser will reserve
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
     * space so that the fixed width values can be parsed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     * For example, consider {@code builder.appendValue(YEAR).appendValue(MONTH_OF_YEAR, 2);}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
     * The year is a variable width parse of between 1 and 19 digits.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
     * The month is a fixed width parse of 2 digits.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
     * Because these were appended to the same builder immediately after one another,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
     * the year parser will reserve two digits for the month to parse.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     * Thus, the text '201106' will correctly parse to a year of 2011 and a month of 6.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
     * Without adjacent value parsing, the year would greedily parse all six digits and leave
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
     * nothing for the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     * Adjacent value parsing applies to each set of fixed width not-negative values in the parser
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   435
     * that immediately follow any kind of value, variable or fixed width.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     * Calling any other append method will end the setup of adjacent value parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * Thus, in the unlikely event that you need to avoid adjacent value parsing behavior,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * simply add the {@code appendValue} to another {@code DateTimeFormatterBuilder}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * and add that to this builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     * If adjacent parsing is active, then parsing must match exactly the specified
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     * number of digits in both strict and lenient modes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
     * In addition, no positive or negative sign is permitted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     * @param field  the field to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
     * @param width  the width of the printed field, from 1 to 19
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
     * @throws IllegalArgumentException if the width is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
    public DateTimeFormatterBuilder appendValue(TemporalField field, int width) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
        if (width < 1 || width > 19) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
            throw new IllegalArgumentException("The width must be from 1 to 19 inclusive but was " + width);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
        NumberPrinterParser pp = new NumberPrinterParser(field, width, width, SignStyle.NOT_NEGATIVE);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   456
        appendValue(pp);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   457
        return this;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
     * Appends the value of a date-time field to the formatter providing full
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   462
     * control over formatting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   464
     * The value of the field will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     * If the value cannot be obtained then an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
     * This method provides full control of the numeric formatting, including
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
     * zero-padding and the positive/negative sign.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
     * The parser for a variable width value such as this normally behaves greedily,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
     * accepting as many digits as possible.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
     * This behavior can be affected by 'adjacent value parsing'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
     * See {@link #appendValue(java.time.temporal.TemporalField, int)} for full details.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   475
     * In strict parsing mode, the minimum number of parsed digits is {@code minWidth}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   476
     * and the maximum is {@code maxWidth}.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   477
     * In lenient parsing mode, the minimum number of parsed digits is one
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   478
     * and the maximum is 19 (except as limited by adjacent value parsing).
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
     * If this method is invoked with equal minimum and maximum widths and a sign style of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
     * {@code NOT_NEGATIVE} then it delegates to {@code appendValue(TemporalField,int)}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   482
     * In this scenario, the formatting and parsing behavior described there occur.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
     * @param field  the field to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
     * @param minWidth  the minimum field width of the printed field, from 1 to 19
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
     * @param maxWidth  the maximum field width of the printed field, from 1 to 19
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     * @param signStyle  the positive/negative output style, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
     * @throws IllegalArgumentException if the widths are invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
    public DateTimeFormatterBuilder appendValue(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
            TemporalField field, int minWidth, int maxWidth, SignStyle signStyle) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
        if (minWidth == maxWidth && signStyle == SignStyle.NOT_NEGATIVE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
            return appendValue(field, maxWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
        Objects.requireNonNull(signStyle, "signStyle");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
        if (minWidth < 1 || minWidth > 19) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
            throw new IllegalArgumentException("The minimum width must be from 1 to 19 inclusive but was " + minWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
        if (maxWidth < 1 || maxWidth > 19) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
            throw new IllegalArgumentException("The maximum width must be from 1 to 19 inclusive but was " + maxWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
        if (maxWidth < minWidth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
            throw new IllegalArgumentException("The maximum width must exceed or equal the minimum width but " +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
                    maxWidth + " < " + minWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
        NumberPrinterParser pp = new NumberPrinterParser(field, minWidth, maxWidth, signStyle);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   509
        appendValue(pp);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
    /**
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   515
     * Appends the reduced value of a date-time field to the formatter.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
     * <p>
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   517
     * Since fields such as year vary by chronology, it is recommended to use the
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   518
     * {@link #appendValueReduced(TemporalField, int, int, ChronoLocalDate)} date}
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   519
     * variant of this method in most cases. This variant is suitable for
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   520
     * simple fields or working with only the ISO chronology.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   521
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   522
     * For formatting, the {@code width} and {@code maxWidth} are used to
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   523
     * determine the number of characters to format.
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   524
     * If they are equal then the format is fixed width.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   525
     * If the value of the field is within the range of the {@code baseValue} using
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   526
     * {@code width} characters then the reduced value is formatted otherwise the value is
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   527
     * truncated to fit {@code maxWidth}.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   528
     * The rightmost characters are output to match the width, left padding with zero.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   529
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   530
     * For strict parsing, the number of characters allowed by {@code width} to {@code maxWidth} are parsed.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   531
     * For lenient parsing, the number of characters must be at least 1 and less than 10.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   532
     * If the number of digits parsed is equal to {@code width} and the value is positive,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   533
     * the value of the field is computed to be the first number greater than
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   534
     * or equal to the {@code baseValue} with the same least significant characters,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   535
     * otherwise the value parsed is the field value.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   536
     * This allows a reduced value to be entered for values in range of the baseValue
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   537
     * and width and absolute values can be entered for values outside the range.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   538
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   539
     * For example, a base value of {@code 1980} and a width of {@code 2} will have
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   540
     * valid values from {@code 1980} to {@code 2079}.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   541
     * During parsing, the text {@code "12"} will result in the value {@code 2012} as that
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   542
     * is the value within the range where the last two characters are "12".
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   543
     * By contrast, parsing the text {@code "1915"} will result in the value {@code 1915}.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   544
     *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   545
     * @param field  the field to append, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   546
     * @param width  the field width of the printed and parsed field, from 1 to 10
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   547
     * @param maxWidth  the maximum field width of the printed field, from 1 to 10
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
     * @param baseValue  the base value of the range of valid values
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   550
     * @throws IllegalArgumentException if the width or base value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
     */
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   552
    public DateTimeFormatterBuilder appendValueReduced(TemporalField field,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   553
            int width, int maxWidth, int baseValue) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
        Objects.requireNonNull(field, "field");
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   555
        ReducedPrinterParser pp = new ReducedPrinterParser(field, width, maxWidth, baseValue, null);
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   556
        appendValue(pp);
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   557
        return this;
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   558
    }
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   559
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   560
    /**
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   561
     * Appends the reduced value of a date-time field to the formatter.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   562
     * <p>
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   563
     * This is typically used for formatting and parsing a two digit year.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   564
     * <p>
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   565
     * The base date is used to calculate the full value during parsing.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   566
     * For example, if the base date is 1950-01-01 then parsed values for
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   567
     * a two digit year parse will be in the range 1950-01-01 to 2049-12-31.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   568
     * Only the year would be extracted from the date, thus a base date of
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   569
     * 1950-08-25 would also parse to the range 1950-01-01 to 2049-12-31.
21296
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
   570
     * This behavior is necessary to support fields such as week-based-year
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   571
     * or other calendar systems where the parsed value does not align with
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   572
     * standard ISO years.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   573
     * <p>
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   574
     * The exact behavior is as follows. Parse the full set of fields and
21296
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
   575
     * determine the effective chronology using the last chronology if
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
   576
     * it appears more than once. Then convert the base date to the
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   577
     * effective chronology. Then extract the specified field from the
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   578
     * chronology-specific base date and use it to determine the
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   579
     * {@code baseValue} used below.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   580
     * <p>
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   581
     * For formatting, the {@code width} and {@code maxWidth} are used to
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   582
     * determine the number of characters to format.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   583
     * If they are equal then the format is fixed width.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   584
     * If the value of the field is within the range of the {@code baseValue} using
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   585
     * {@code width} characters then the reduced value is formatted otherwise the value is
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   586
     * truncated to fit {@code maxWidth}.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   587
     * The rightmost characters are output to match the width, left padding with zero.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   588
     * <p>
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   589
     * For strict parsing, the number of characters allowed by {@code width} to {@code maxWidth} are parsed.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   590
     * For lenient parsing, the number of characters must be at least 1 and less than 10.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   591
     * If the number of digits parsed is equal to {@code width} and the value is positive,
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   592
     * the value of the field is computed to be the first number greater than
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   593
     * or equal to the {@code baseValue} with the same least significant characters,
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   594
     * otherwise the value parsed is the field value.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   595
     * This allows a reduced value to be entered for values in range of the baseValue
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   596
     * and width and absolute values can be entered for values outside the range.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   597
     * <p>
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   598
     * For example, a base value of {@code 1980} and a width of {@code 2} will have
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   599
     * valid values from {@code 1980} to {@code 2079}.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   600
     * During parsing, the text {@code "12"} will result in the value {@code 2012} as that
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   601
     * is the value within the range where the last two characters are "12".
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   602
     * By contrast, parsing the text {@code "1915"} will result in the value {@code 1915}.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   603
     *
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   604
     * @param field  the field to append, not null
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   605
     * @param width  the field width of the printed and parsed field, from 1 to 10
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   606
     * @param maxWidth  the maximum field width of the printed field, from 1 to 10
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   607
     * @param baseDate  the base date used to calculate the base value for the range
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   608
     *  of valid values in the parsed chronology, not null
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   609
     * @return this, for chaining, not null
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   610
     * @throws IllegalArgumentException if the width or base value is invalid
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   611
     */
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   612
    public DateTimeFormatterBuilder appendValueReduced(
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   613
            TemporalField field, int width, int maxWidth, ChronoLocalDate baseDate) {
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   614
        Objects.requireNonNull(field, "field");
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   615
        Objects.requireNonNull(baseDate, "baseDate");
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
   616
        ReducedPrinterParser pp = new ReducedPrinterParser(field, width, maxWidth, 0, baseDate);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   617
        appendValue(pp);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   622
     * Appends a fixed or variable width printer-parser handling adjacent value mode.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   623
     * If a PrinterParser is not active then the new PrinterParser becomes
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   624
     * the active PrinterParser.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   625
     * Otherwise, the active PrinterParser is modified depending on the new PrinterParser.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   626
     * If the new PrinterParser is fixed width and has sign style {@code NOT_NEGATIVE}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   627
     * then its width is added to the active PP and
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   628
     * the new PrinterParser is forced to be fixed width.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   629
     * If the new PrinterParser is variable width, the active PrinterParser is changed
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   630
     * to be fixed width and the new PrinterParser becomes the active PP.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
     * @param pp  the printer-parser, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
     */
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   635
    private DateTimeFormatterBuilder appendValue(NumberPrinterParser pp) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   636
        if (active.valueParserIndex >= 0) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   637
            final int activeValueParser = active.valueParserIndex;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   638
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
            // adjacent parsing mode, update setting in previous parsers
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   640
            NumberPrinterParser basePP = (NumberPrinterParser) active.printerParsers.get(activeValueParser);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   641
            if (pp.minWidth == pp.maxWidth && pp.signStyle == SignStyle.NOT_NEGATIVE) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   642
                // Append the width to the subsequentWidth of the active parser
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   643
                basePP = basePP.withSubsequentWidth(pp.maxWidth);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   644
                // Append the new parser as a fixed width
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   645
                appendInternal(pp.withFixedWidth());
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   646
                // Retain the previous active parser
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   647
                active.valueParserIndex = activeValueParser;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   648
            } else {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   649
                // Modify the active parser to be fixed width
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   650
                basePP = basePP.withFixedWidth();
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   651
                // The new parser becomes the mew active parser
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   652
                active.valueParserIndex = appendInternal(pp);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   653
            }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   654
            // Replace the modified parser with the updated one
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   655
            active.printerParsers.set(activeValueParser, basePP);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
        } else {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   657
            // The new Parser becomes the active parser
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   658
            active.valueParserIndex = appendInternal(pp);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * Appends the fractional value of a date-time field to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
     * The fractional value of the field will be output including the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
     * preceding decimal point. The preceding value is not output.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     * For example, the second-of-minute value of 15 would be output as {@code .25}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
     * The width of the printed fraction can be controlled. Setting the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
     * minimum width to zero will cause no output to be generated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
     * The printed fraction will have the minimum width necessary between
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
     * the minimum and maximum widths - trailing zeroes are omitted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
     * No rounding occurs due to the maximum width - digits are simply dropped.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
     * When parsing in strict mode, the number of parsed digits must be between
37610
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   678
     * the minimum and maximum width. In strict mode, if the minimum and maximum widths
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   679
     * are equal and there is no decimal point then the parser will
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   680
     * participate in adjacent value parsing, see
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   681
     * {@link appendValue(java.time.temporal.TemporalField, int)}. When parsing in lenient mode,
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   682
     * the minimum width is considered to be zero and the maximum is nine.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
     * If the value cannot be obtained then an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
     * If the value is negative an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
     * If the field does not have a fixed set of valid values then an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   687
     * exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
     * If the field value in the date-time to be printed is invalid it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
     * cannot be printed and an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
     * @param field  the field to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
     * @param minWidth  the minimum width of the field excluding the decimal point, from 0 to 9
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
     * @param maxWidth  the maximum width of the field excluding the decimal point, from 1 to 9
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
     * @param decimalPoint  whether to output the localized decimal point symbol
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
     * @throws IllegalArgumentException if the field has a variable set of valid values or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
     *  either width is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
    public DateTimeFormatterBuilder appendFraction(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   700
            TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) {
37610
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   701
        if (minWidth == maxWidth && decimalPoint == false) {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   702
            // adjacent parsing
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   703
            appendValue(new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint));
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   704
        } else {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   705
            appendInternal(new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint));
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
   706
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   712
     * Appends the text of a date-time field to the formatter using the full
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
     * text style.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   715
     * The text of the field will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
     * The value must be within the valid range of the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
     * If the value cannot be obtained then an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
     * If the field has no textual representation, then the numeric value will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   719
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   720
     * The value will be printed as per the normal format of an integer value.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   721
     * Only negative numbers will be signed. No padding will be added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   722
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   723
     * @param field  the field to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   724
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   725
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   726
    public DateTimeFormatterBuilder appendText(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   727
        return appendText(field, TextStyle.FULL);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   728
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   729
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   730
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   731
     * Appends the text of a date-time field to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   732
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   733
     * The text of the field will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   734
     * The value must be within the valid range of the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   735
     * If the value cannot be obtained then an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   736
     * If the field has no textual representation, then the numeric value will be used.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   737
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   738
     * The value will be printed as per the normal format of an integer value.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   739
     * Only negative numbers will be signed. No padding will be added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   740
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   741
     * @param field  the field to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   742
     * @param textStyle  the text style to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   743
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   744
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   745
    public DateTimeFormatterBuilder appendText(TemporalField field, TextStyle textStyle) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   746
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   747
        Objects.requireNonNull(textStyle, "textStyle");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   748
        appendInternal(new TextPrinterParser(field, textStyle, DateTimeTextProvider.getInstance()));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   749
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   750
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   751
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   752
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   753
     * Appends the text of a date-time field to the formatter using the specified
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   754
     * map to supply the text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   755
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
     * The standard text outputting methods use the localized text in the JDK.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
     * This method allows that text to be specified directly.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   758
     * The supplied map is not validated by the builder to ensure that formatting or
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   759
     * parsing is possible, thus an invalid map may throw an error during later use.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   761
     * Supplying the map of text provides considerable flexibility in formatting and parsing.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
     * For example, a legacy application might require or supply the months of the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
     * year as "JNY", "FBY", "MCH" etc. These do not match the standard set of text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
     * for localized month names. Using this method, a map can be created which
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
     * defines the connection between each value and the text:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
     * Map&lt;Long, String&gt; map = new HashMap&lt;&gt;();
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
   768
     * map.put(1L, "JNY");
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
   769
     * map.put(2L, "FBY");
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
   770
     * map.put(3L, "MCH");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   771
     * ...
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   772
     * builder.appendText(MONTH_OF_YEAR, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   775
     * Other uses might be to output the value with a suffix, such as "1st", "2nd", "3rd",
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   776
     * or as Roman numerals "I", "II", "III", "IV".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   777
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   778
     * During formatting, the value is obtained and checked that it is in the valid range.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
     * If text is not available for the value then it is output as a number.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   780
     * During parsing, the parser will match against the map of text and numeric values.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   781
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
     * @param field  the field to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
     * @param textLookup  the map from the value to the text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   784
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
    public DateTimeFormatterBuilder appendText(TemporalField field, Map<Long, String> textLookup) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
        Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
        Objects.requireNonNull(textLookup, "textLookup");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
        Map<Long, String> copy = new LinkedHashMap<>(textLookup);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
        Map<TextStyle, Map<Long, String>> map = Collections.singletonMap(TextStyle.FULL, copy);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
        final LocaleStore store = new LocaleStore(map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
        DateTimeTextProvider provider = new DateTimeTextProvider() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
            @Override
50643
f2d94a0619a2 8042131: DateTimeFormatterBuilder Mapped-values do not work for JapaneseDate
naoto
parents: 50473
diff changeset
   794
            public String getText(Chronology chrono, TemporalField field,
f2d94a0619a2 8042131: DateTimeFormatterBuilder Mapped-values do not work for JapaneseDate
naoto
parents: 50473
diff changeset
   795
                                  long value, TextStyle style, Locale locale) {
f2d94a0619a2 8042131: DateTimeFormatterBuilder Mapped-values do not work for JapaneseDate
naoto
parents: 50473
diff changeset
   796
                return store.getText(value, style);
f2d94a0619a2 8042131: DateTimeFormatterBuilder Mapped-values do not work for JapaneseDate
naoto
parents: 50473
diff changeset
   797
            }
f2d94a0619a2 8042131: DateTimeFormatterBuilder Mapped-values do not work for JapaneseDate
naoto
parents: 50473
diff changeset
   798
            @Override
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   799
            public String getText(TemporalField field, long value, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   800
                return store.getText(value, style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   802
            @Override
51874
a0426bc28519 8210633: Cannot parse JapaneseDate string with DateTimeFormatterBuilder Mapped-values
naoto
parents: 50643
diff changeset
   803
            public Iterator<Entry<String, Long>> getTextIterator(Chronology chrono,
a0426bc28519 8210633: Cannot parse JapaneseDate string with DateTimeFormatterBuilder Mapped-values
naoto
parents: 50643
diff changeset
   804
                    TemporalField field, TextStyle style, Locale locale) {
a0426bc28519 8210633: Cannot parse JapaneseDate string with DateTimeFormatterBuilder Mapped-values
naoto
parents: 50643
diff changeset
   805
                return store.getTextIterator(style);
a0426bc28519 8210633: Cannot parse JapaneseDate string with DateTimeFormatterBuilder Mapped-values
naoto
parents: 50643
diff changeset
   806
            }
a0426bc28519 8210633: Cannot parse JapaneseDate string with DateTimeFormatterBuilder Mapped-values
naoto
parents: 50643
diff changeset
   807
            @Override
a0426bc28519 8210633: Cannot parse JapaneseDate string with DateTimeFormatterBuilder Mapped-values
naoto
parents: 50643
diff changeset
   808
            public Iterator<Entry<String, Long>> getTextIterator(TemporalField field,
a0426bc28519 8210633: Cannot parse JapaneseDate string with DateTimeFormatterBuilder Mapped-values
naoto
parents: 50643
diff changeset
   809
                    TextStyle style, Locale locale) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   810
                return store.getTextIterator(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   811
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
        };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
        appendInternal(new TextPrinterParser(field, TextStyle.FULL, provider));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   819
     * Appends an instant using ISO-8601 to the formatter, formatting fractional
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   820
     * digits in groups of three.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   821
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   822
     * Instants have a fixed output format.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   823
     * They are converted to a date-time with a zone-offset of UTC and formatted
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   824
     * using the standard ISO-8601 format.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   825
     * With this method, formatting nano-of-second outputs zero, three, six
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   826
     * or nine digits as necessary.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   827
     * The localized decimal style is not used.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   828
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   829
     * The instant is obtained using {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
40120
ac96fa31cdf3 8163180: Typos around @code javadoc tag usage
igerasim
parents: 39831
diff changeset
   830
     * and optionally {@code NANO_OF_SECOND}. The value of {@code INSTANT_SECONDS}
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   831
     * may be outside the maximum range of {@code LocalDateTime}.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   832
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   833
     * The {@linkplain ResolverStyle resolver style} has no effect on instant parsing.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   834
     * The end-of-day time of '24:00' is handled as midnight at the start of the following day.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   835
     * The leap-second time of '23:59:59' is handled to some degree, see
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   836
     * {@link DateTimeFormatter#parsedLeapSecond()} for full details.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   837
     * <p>
52012
6f58ecdb060a 8166138: DateTimeFormatter.ISO_INSTANT should handle offsets
nishjain
parents: 51874
diff changeset
   838
     * When formatting, the instant will always be suffixed by 'Z' to indicate UTC.
6f58ecdb060a 8166138: DateTimeFormatter.ISO_INSTANT should handle offsets
nishjain
parents: 51874
diff changeset
   839
     * When parsing, the behaviour of {@link DateTimeFormatterBuilder#appendOffsetId()}
6f58ecdb060a 8166138: DateTimeFormatter.ISO_INSTANT should handle offsets
nishjain
parents: 51874
diff changeset
   840
     * will be used to parse the offset, converting the instant to UTC as necessary.
6f58ecdb060a 8166138: DateTimeFormatter.ISO_INSTANT should handle offsets
nishjain
parents: 51874
diff changeset
   841
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   842
     * An alternative to this method is to format/parse the instant as a single
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
     * epoch-seconds value. That is achieved using {@code appendValue(INSTANT_SECONDS)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   844
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
    public DateTimeFormatterBuilder appendInstant() {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   848
        appendInternal(new InstantPrinterParser(-2));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   849
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   850
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   853
     * Appends an instant using ISO-8601 to the formatter with control over
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   854
     * the number of fractional digits.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   855
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   856
     * Instants have a fixed output format, although this method provides some
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   857
     * control over the fractional digits. They are converted to a date-time
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   858
     * with a zone-offset of UTC and printed using the standard ISO-8601 format.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   859
     * The localized decimal style is not used.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   860
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   861
     * The {@code fractionalDigits} parameter allows the output of the fractional
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   862
     * second to be controlled. Specifying zero will cause no fractional digits
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   863
     * to be output. From 1 to 9 will output an increasing number of digits, using
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   864
     * zero right-padding if necessary. The special value -1 is used to output as
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   865
     * many digits as necessary to avoid any trailing zeroes.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   866
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   867
     * When parsing in strict mode, the number of parsed digits must match the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   868
     * fractional digits. When parsing in lenient mode, any number of fractional
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   869
     * digits from zero to nine are accepted.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   870
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   871
     * The instant is obtained using {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
40120
ac96fa31cdf3 8163180: Typos around @code javadoc tag usage
igerasim
parents: 39831
diff changeset
   872
     * and optionally {@code NANO_OF_SECOND}. The value of {@code INSTANT_SECONDS}
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   873
     * may be outside the maximum range of {@code LocalDateTime}.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   874
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   875
     * The {@linkplain ResolverStyle resolver style} has no effect on instant parsing.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   876
     * The end-of-day time of '24:00' is handled as midnight at the start of the following day.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
   877
     * The leap-second time of '23:59:60' is handled to some degree, see
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   878
     * {@link DateTimeFormatter#parsedLeapSecond()} for full details.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   879
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   880
     * An alternative to this method is to format/parse the instant as a single
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   881
     * epoch-seconds value. That is achieved using {@code appendValue(INSTANT_SECONDS)}.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   882
     *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   883
     * @param fractionalDigits  the number of fractional second digits to format with,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   884
     *  from 0 to 9, or -1 to use as many digits as necessary
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   885
     * @return this, for chaining, not null
28751
47403deaec44 8068284: Missing @throws in DateTimeFormatterBuilder.appendOffset
rriggs
parents: 28059
diff changeset
   886
     * @throws IllegalArgumentException if the number of fractional digits is invalid
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   887
     */
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   888
    public DateTimeFormatterBuilder appendInstant(int fractionalDigits) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   889
        if (fractionalDigits < -1 || fractionalDigits > 9) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   890
            throw new IllegalArgumentException("The fractional digits must be from -1 to 9 inclusive but was " + fractionalDigits);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   891
        }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   892
        appendInternal(new InstantPrinterParser(fractionalDigits));
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   893
        return this;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   894
    }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   895
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   896
    //-----------------------------------------------------------------------
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   897
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
     * Appends the zone offset, such as '+01:00', to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   900
     * This appends an instruction to format/parse the offset ID to the builder.
36646
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   901
     * This is equivalent to calling {@code appendOffset("+HH:mm:ss", "Z")}.
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   902
     * See {@link #appendOffset(String, String)} for details on formatting
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   903
     * and parsing.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
    public DateTimeFormatterBuilder appendOffsetId() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   908
        appendInternal(OffsetIdPrinterParser.INSTANCE_ID_Z);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
     * Appends the zone offset, such as '+01:00', to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   915
     * This appends an instruction to format/parse the offset ID to the builder.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   917
     * During formatting, the offset is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
   918
     * to querying the temporal with {@link TemporalQueries#offset()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
     * It will be printed using the format defined below.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
     * If the offset cannot be obtained then an exception is thrown unless the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
     * section of the formatter is optional.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
     * <p>
36646
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   923
     * When parsing in strict mode, the input must contain the mandatory
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   924
     * and optional elements are defined by the specified pattern.
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   925
     * If the offset cannot be parsed then an exception is thrown unless
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   926
     * the section of the formatter is optional.
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   927
     * <p>
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   928
     * When parsing in lenient mode, only the hours are mandatory - minutes
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   929
     * and seconds are optional. The colons are required if the specified
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   930
     * pattern contains a colon. If the specified pattern is "+HH", the
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   931
     * presence of colons is determined by whether the character after the
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   932
     * hour digits is a colon or not.
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   933
     * If the offset cannot be parsed then an exception is thrown unless
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   934
     * the section of the formatter is optional.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   935
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   936
     * The format of the offset is controlled by a pattern which must be one
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   937
     * of the following:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   938
     * <ul>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   939
     * <li>{@code +HH} - hour only, ignoring minute and second
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   940
     * <li>{@code +HHmm} - hour, with minute if non-zero, ignoring second, no colon
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   941
     * <li>{@code +HH:mm} - hour, with minute if non-zero, ignoring second, with colon
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   942
     * <li>{@code +HHMM} - hour and minute, ignoring second, no colon
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   943
     * <li>{@code +HH:MM} - hour and minute, ignoring second, with colon
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   944
     * <li>{@code +HHMMss} - hour and minute, with second if non-zero, no colon
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   945
     * <li>{@code +HH:MM:ss} - hour and minute, with second if non-zero, with colon
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   946
     * <li>{@code +HHMMSS} - hour, minute and second, no colon
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   947
     * <li>{@code +HH:MM:SS} - hour, minute and second, with colon
36646
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   948
     * <li>{@code +HHmmss} - hour, with minute if non-zero or with minute and
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   949
     * second if non-zero, no colon
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   950
     * <li>{@code +HH:mm:ss} - hour, with minute if non-zero or with minute and
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
   951
     * second if non-zero, with colon
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   952
     * <li>{@code +H} - hour only, ignoring minute and second
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   953
     * <li>{@code +Hmm} - hour, with minute if non-zero, ignoring second, no colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   954
     * <li>{@code +H:mm} - hour, with minute if non-zero, ignoring second, with colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   955
     * <li>{@code +HMM} - hour and minute, ignoring second, no colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   956
     * <li>{@code +H:MM} - hour and minute, ignoring second, with colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   957
     * <li>{@code +HMMss} - hour and minute, with second if non-zero, no colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   958
     * <li>{@code +H:MM:ss} - hour and minute, with second if non-zero, with colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   959
     * <li>{@code +HMMSS} - hour, minute and second, no colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   960
     * <li>{@code +H:MM:SS} - hour, minute and second, with colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   961
     * <li>{@code +Hmmss} - hour, with minute if non-zero or with minute and
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   962
     * second if non-zero, no colon
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   963
     * <li>{@code +H:mm:ss} - hour, with minute if non-zero or with minute and
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   964
     * second if non-zero, with colon
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   965
     * </ul>
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   966
     * Patterns containing "HH" will format and parse a two digit hour,
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   967
     * zero-padded if necessary. Patterns containing "H" will format with no
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   968
     * zero-padding, and parse either one or two digits.
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
   969
     * In lenient mode, the parser will be greedy and parse the maximum digits possible.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   970
     * The "no offset" text controls what text is printed when the total amount of
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   971
     * the offset fields to be output is zero.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   972
     * Example values would be 'Z', '+00:00', 'UTC' or 'GMT'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   973
     * Three formats are accepted for parsing UTC - the "no offset" text, and the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
     * plus and minus versions of zero defined by the pattern.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
     * @param pattern  the pattern to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
     * @param noOffsetText  the text to use when the offset is zero, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
     * @return this, for chaining, not null
28751
47403deaec44 8068284: Missing @throws in DateTimeFormatterBuilder.appendOffset
rriggs
parents: 28059
diff changeset
   979
     * @throws IllegalArgumentException if the pattern is invalid
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
    public DateTimeFormatterBuilder appendOffset(String pattern, String noOffsetText) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   982
        appendInternal(new OffsetIdPrinterParser(pattern, noOffsetText));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   986
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   987
     * Appends the localized zone offset, such as 'GMT+01:00', to the formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   988
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   989
     * This appends a localized zone offset to the builder, the format of the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   990
     * localized offset is controlled by the specified {@link FormatStyle style}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   991
     * to this method:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   992
     * <ul>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   993
     * <li>{@link TextStyle#FULL full} - formats with localized offset text, such
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   994
     * as 'GMT, 2-digit hour and minute field, optional second field if non-zero,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   995
     * and colon.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   996
     * <li>{@link TextStyle#SHORT short} - formats with localized offset text,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   997
     * such as 'GMT, hour without leading zero, optional 2-digit minute and
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
   998
     * second if non-zero, and colon.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   999
     * </ul>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1000
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1001
     * During formatting, the offset is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1002
     * to querying the temporal with {@link TemporalQueries#offset()}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1003
     * If the offset cannot be obtained then an exception is thrown unless the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1004
     * section of the formatter is optional.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1005
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1006
     * During parsing, the offset is parsed using the format defined above.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1007
     * If the offset cannot be parsed then an exception is thrown unless the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1008
     * section of the formatter is optional.
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 23595
diff changeset
  1009
     *
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1010
     * @param style  the format style to use, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1011
     * @return this, for chaining, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1012
     * @throws IllegalArgumentException if style is neither {@link TextStyle#FULL
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1013
     * full} nor {@link TextStyle#SHORT short}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1014
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1015
    public DateTimeFormatterBuilder appendLocalizedOffset(TextStyle style) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1016
        Objects.requireNonNull(style, "style");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1017
        if (style != TextStyle.FULL && style != TextStyle.SHORT) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1018
            throw new IllegalArgumentException("Style must be either full or short");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1019
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1020
        appendInternal(new LocalizedOffsetIdPrinterParser(style));
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1021
        return this;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1022
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1023
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
     * Appends the time-zone ID, such as 'Europe/Paris' or '+02:00', to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1028
     * This appends an instruction to format/parse the zone ID to the builder.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
     * The zone ID is obtained in a strict manner suitable for {@code ZonedDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
     * By contrast, {@code OffsetDateTime} does not have a zone ID suitable
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
     * for use with this method, see {@link #appendZoneOrOffsetId()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1033
     * During formatting, the zone is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1034
     * to querying the temporal with {@link TemporalQueries#zoneId()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
     * It will be printed using the result of {@link ZoneId#getId()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
     * If the zone cannot be obtained then an exception is thrown unless the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
     * section of the formatter is optional.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1039
     * During parsing, the text must match a known zone or offset.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1040
     * There are two types of zone ID, offset-based, such as '+01:30' and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1041
     * region-based, such as 'Europe/London'. These are parsed differently.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1042
     * If the parse starts with '+', '-', 'UT', 'UTC' or 'GMT', then the parser
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1043
     * expects an offset-based zone and will not match region-based zones.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1044
     * The offset ID, such as '+02:30', may be at the start of the parse,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1045
     * or prefixed by  'UT', 'UTC' or 'GMT'. The offset ID parsing is
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1046
     * equivalent to using {@link #appendOffset(String, String)} using the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1047
     * arguments 'HH:MM:ss' and the no offset string '0'.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1048
     * If the parse starts with 'UT', 'UTC' or 'GMT', and the parser cannot
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1049
     * match a following offset ID, then {@link ZoneOffset#UTC} is selected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1050
     * In all other cases, the list of known region-based zones is used to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1051
     * find the longest available match. If no match is found, and the parse
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1052
     * starts with 'Z', then {@code ZoneOffset.UTC} is selected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1053
     * The parser uses the {@linkplain #parseCaseInsensitive() case sensitive} setting.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1054
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1055
     * For example, the following will parse:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1056
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1057
     *   "Europe/London"           -- ZoneId.of("Europe/London")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1058
     *   "Z"                       -- ZoneOffset.UTC
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1059
     *   "UT"                      -- ZoneId.of("UT")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1060
     *   "UTC"                     -- ZoneId.of("UTC")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1061
     *   "GMT"                     -- ZoneId.of("GMT")
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1062
     *   "+01:30"                  -- ZoneOffset.of("+01:30")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1063
     *   "UT+01:30"                -- ZoneOffset.of("+01:30")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1064
     *   "UTC+01:30"               -- ZoneOffset.of("+01:30")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1065
     *   "GMT+01:30"               -- ZoneOffset.of("+01:30")
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1066
     * </pre>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
     * @see #appendZoneRegionId()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
    public DateTimeFormatterBuilder appendZoneId() {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1072
        appendInternal(new ZoneIdPrinterParser(TemporalQueries.zoneId(), "ZoneId()"));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1073
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1074
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
     * Appends the time-zone region ID, such as 'Europe/Paris', to the formatter,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
     * rejecting the zone ID if it is a {@code ZoneOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1080
     * This appends an instruction to format/parse the zone ID to the builder
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
     * only if it is a region-based ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1083
     * During formatting, the zone is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1084
     * to querying the temporal with {@link TemporalQueries#zoneId()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
     * If the zone is a {@code ZoneOffset} or it cannot be obtained then
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1086
     * an exception is thrown unless the section of the formatter is optional.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
     * If the zone is not an offset, then the zone will be printed using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1088
     * the zone ID from {@link ZoneId#getId()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1090
     * During parsing, the text must match a known zone or offset.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1091
     * There are two types of zone ID, offset-based, such as '+01:30' and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1092
     * region-based, such as 'Europe/London'. These are parsed differently.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1093
     * If the parse starts with '+', '-', 'UT', 'UTC' or 'GMT', then the parser
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1094
     * expects an offset-based zone and will not match region-based zones.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1095
     * The offset ID, such as '+02:30', may be at the start of the parse,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1096
     * or prefixed by  'UT', 'UTC' or 'GMT'. The offset ID parsing is
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1097
     * equivalent to using {@link #appendOffset(String, String)} using the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1098
     * arguments 'HH:MM:ss' and the no offset string '0'.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1099
     * If the parse starts with 'UT', 'UTC' or 'GMT', and the parser cannot
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1100
     * match a following offset ID, then {@link ZoneOffset#UTC} is selected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1101
     * In all other cases, the list of known region-based zones is used to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1102
     * find the longest available match. If no match is found, and the parse
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1103
     * starts with 'Z', then {@code ZoneOffset.UTC} is selected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1104
     * The parser uses the {@linkplain #parseCaseInsensitive() case sensitive} setting.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1105
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1106
     * For example, the following will parse:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1107
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1108
     *   "Europe/London"           -- ZoneId.of("Europe/London")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1109
     *   "Z"                       -- ZoneOffset.UTC
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1110
     *   "UT"                      -- ZoneId.of("UT")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1111
     *   "UTC"                     -- ZoneId.of("UTC")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1112
     *   "GMT"                     -- ZoneId.of("GMT")
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1113
     *   "+01:30"                  -- ZoneOffset.of("+01:30")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1114
     *   "UT+01:30"                -- ZoneOffset.of("+01:30")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1115
     *   "UTC+01:30"               -- ZoneOffset.of("+01:30")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1116
     *   "GMT+01:30"               -- ZoneOffset.of("+01:30")
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1117
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1118
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
  1119
     * Note that this method is identical to {@code appendZoneId()} except
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1120
     * in the mechanism used to obtain the zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1121
     * Note also that parsing accepts offsets, whereas formatting will never
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1122
     * produce one.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1123
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1124
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1125
     * @see #appendZoneId()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1126
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
    public DateTimeFormatterBuilder appendZoneRegionId() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
        appendInternal(new ZoneIdPrinterParser(QUERY_REGION_ONLY, "ZoneRegionId()"));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1129
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1130
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1131
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1132
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1133
     * Appends the time-zone ID, such as 'Europe/Paris' or '+02:00', to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1134
     * the formatter, using the best available zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1135
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1136
     * This appends an instruction to format/parse the best available
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1137
     * zone or offset ID to the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1138
     * The zone ID is obtained in a lenient manner that first attempts to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1139
     * find a true zone ID, such as that on {@code ZonedDateTime}, and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1140
     * then attempts to find an offset, such as that on {@code OffsetDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1142
     * During formatting, the zone is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1143
     * to querying the temporal with {@link TemporalQueries#zone()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1144
     * It will be printed using the result of {@link ZoneId#getId()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1145
     * If the zone cannot be obtained then an exception is thrown unless the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1146
     * section of the formatter is optional.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1147
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1148
     * During parsing, the text must match a known zone or offset.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1149
     * There are two types of zone ID, offset-based, such as '+01:30' and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1150
     * region-based, such as 'Europe/London'. These are parsed differently.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1151
     * If the parse starts with '+', '-', 'UT', 'UTC' or 'GMT', then the parser
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1152
     * expects an offset-based zone and will not match region-based zones.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1153
     * The offset ID, such as '+02:30', may be at the start of the parse,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1154
     * or prefixed by  'UT', 'UTC' or 'GMT'. The offset ID parsing is
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1155
     * equivalent to using {@link #appendOffset(String, String)} using the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1156
     * arguments 'HH:MM:ss' and the no offset string '0'.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1157
     * If the parse starts with 'UT', 'UTC' or 'GMT', and the parser cannot
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1158
     * match a following offset ID, then {@link ZoneOffset#UTC} is selected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1159
     * In all other cases, the list of known region-based zones is used to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1160
     * find the longest available match. If no match is found, and the parse
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1161
     * starts with 'Z', then {@code ZoneOffset.UTC} is selected.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1162
     * The parser uses the {@linkplain #parseCaseInsensitive() case sensitive} setting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1163
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1164
     * For example, the following will parse:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1165
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1166
     *   "Europe/London"           -- ZoneId.of("Europe/London")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1167
     *   "Z"                       -- ZoneOffset.UTC
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1168
     *   "UT"                      -- ZoneId.of("UT")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1169
     *   "UTC"                     -- ZoneId.of("UTC")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1170
     *   "GMT"                     -- ZoneId.of("GMT")
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1171
     *   "+01:30"                  -- ZoneOffset.of("+01:30")
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1172
     *   "UT+01:30"                -- ZoneOffset.of("UT+01:30")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1173
     *   "UTC+01:30"               -- ZoneOffset.of("UTC+01:30")
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1174
     *   "GMT+01:30"               -- ZoneOffset.of("GMT+01:30")
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1175
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1176
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
  1177
     * Note that this method is identical to {@code appendZoneId()} except
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1178
     * in the mechanism used to obtain the zone.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1179
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1180
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1181
     * @see #appendZoneId()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1182
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1183
    public DateTimeFormatterBuilder appendZoneOrOffsetId() {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1184
        appendInternal(new ZoneIdPrinterParser(TemporalQueries.zone(), "ZoneOrOffsetId()"));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1185
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1186
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1187
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1188
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1189
     * Appends the time-zone name, such as 'British Summer Time', to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1190
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1191
     * This appends an instruction to format/parse the textual name of the zone to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1192
     * the builder.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1193
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1194
     * During formatting, the zone is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1195
     * to querying the temporal with {@link TemporalQueries#zoneId()}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1196
     * If the zone is a {@code ZoneOffset} it will be printed using the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1197
     * result of {@link ZoneOffset#getId()}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1198
     * If the zone is not an offset, the textual name will be looked up
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1199
     * for the locale set in the {@link DateTimeFormatter}.
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1200
     * If the temporal object being printed represents an instant, or if it is a
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1201
     * local date-time that is not in a daylight saving gap or overlap then
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1202
     * the text will be the summer or winter time text as appropriate.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
  1203
     * If the lookup for text does not find any suitable result, then the
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1204
     * {@link ZoneId#getId() ID} will be printed.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1205
     * If the zone cannot be obtained then an exception is thrown unless the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1206
     * section of the formatter is optional.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1207
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1208
     * During parsing, either the textual zone name, the zone ID or the offset
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1209
     * is accepted. Many textual zone names are not unique, such as CST can be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1210
     * for both "Central Standard Time" and "China Standard Time". In this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1211
     * situation, the zone id will be determined by the region information from
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1212
     * formatter's  {@link DateTimeFormatter#getLocale() locale} and the standard
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1213
     * zone id for that area, for example, America/New_York for the America Eastern
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1214
     * zone. The {@link #appendZoneText(TextStyle, Set)} may be used
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1215
     * to specify a set of preferred {@link ZoneId} in this situation.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1216
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1217
     * @param textStyle  the text style to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1218
     * @return this, for chaining, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1219
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1220
    public DateTimeFormatterBuilder appendZoneText(TextStyle textStyle) {
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1221
        appendInternal(new ZoneTextPrinterParser(textStyle, null, false));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1222
        return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1223
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1224
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1225
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1226
     * Appends the time-zone name, such as 'British Summer Time', to the formatter.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1227
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1228
     * This appends an instruction to format/parse the textual name of the zone to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1229
     * the builder.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1230
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1231
     * During formatting, the zone is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1232
     * to querying the temporal with {@link TemporalQueries#zoneId()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1233
     * If the zone is a {@code ZoneOffset} it will be printed using the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1234
     * result of {@link ZoneOffset#getId()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1235
     * If the zone is not an offset, the textual name will be looked up
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1236
     * for the locale set in the {@link DateTimeFormatter}.
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1237
     * If the temporal object being printed represents an instant, or if it is a
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1238
     * local date-time that is not in a daylight saving gap or overlap, then the text
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1239
     * will be the summer or winter time text as appropriate.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
  1240
     * If the lookup for text does not find any suitable result, then the
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1241
     * {@link ZoneId#getId() ID} will be printed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1242
     * If the zone cannot be obtained then an exception is thrown unless the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1243
     * section of the formatter is optional.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1244
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1245
     * During parsing, either the textual zone name, the zone ID or the offset
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1246
     * is accepted. Many textual zone names are not unique, such as CST can be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1247
     * for both "Central Standard Time" and "China Standard Time". In this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1248
     * situation, the zone id will be determined by the region information from
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1249
     * formatter's  {@link DateTimeFormatter#getLocale() locale} and the standard
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1250
     * zone id for that area, for example, America/New_York for the America Eastern
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1251
     * zone. This method also allows a set of preferred {@link ZoneId} to be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1252
     * specified for parsing. The matched preferred zone id will be used if the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1253
     * textural zone name being parsed is not unique.
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
  1254
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1255
     * If the zone cannot be parsed then an exception is thrown unless the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1256
     * section of the formatter is optional.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1257
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1258
     * @param textStyle  the text style to use, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1259
     * @param preferredZones  the set of preferred zone ids, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1260
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1261
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1262
    public DateTimeFormatterBuilder appendZoneText(TextStyle textStyle,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1263
                                                   Set<ZoneId> preferredZones) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1264
        Objects.requireNonNull(preferredZones, "preferredZones");
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1265
        appendInternal(new ZoneTextPrinterParser(textStyle, preferredZones, false));
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1266
        return this;
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1267
    }
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1268
    //----------------------------------------------------------------------
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1269
    /**
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1270
     * Appends the generic time-zone name, such as 'Pacific Time', to the formatter.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1271
     * <p>
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1272
     * This appends an instruction to format/parse the generic textual
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1273
     * name of the zone to the builder. The generic name is the same throughout the whole
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1274
     * year, ignoring any daylight saving changes. For example, 'Pacific Time' is the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1275
     * generic name, whereas 'Pacific Standard Time' and 'Pacific Daylight Time' are the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1276
     * specific names, see {@link #appendZoneText(TextStyle)}.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1277
     * <p>
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1278
     * During formatting, the zone is obtained using a mechanism equivalent
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1279
     * to querying the temporal with {@link TemporalQueries#zoneId()}.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1280
     * If the zone is a {@code ZoneOffset} it will be printed using the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1281
     * result of {@link ZoneOffset#getId()}.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1282
     * If the zone is not an offset, the textual name will be looked up
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1283
     * for the locale set in the {@link DateTimeFormatter}.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1284
     * If the lookup for text does not find any suitable result, then the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1285
     * {@link ZoneId#getId() ID} will be printed.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1286
     * If the zone cannot be obtained then an exception is thrown unless the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1287
     * section of the formatter is optional.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1288
     * <p>
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1289
     * During parsing, either the textual zone name, the zone ID or the offset
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1290
     * is accepted. Many textual zone names are not unique, such as CST can be
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1291
     * for both "Central Standard Time" and "China Standard Time". In this
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1292
     * situation, the zone id will be determined by the region information from
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1293
     * formatter's  {@link DateTimeFormatter#getLocale() locale} and the standard
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1294
     * zone id for that area, for example, America/New_York for the America Eastern zone.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1295
     * The {@link #appendGenericZoneText(TextStyle, Set)} may be used
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1296
     * to specify a set of preferred {@link ZoneId} in this situation.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1297
     *
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1298
     * @param textStyle  the text style to use, not null
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1299
     * @return this, for chaining, not null
44263
7a9297d467e7 8176563: @since value errors in apis of java.base/java.logging module
mli
parents: 43007
diff changeset
  1300
     * @since 9
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1301
     */
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1302
    public DateTimeFormatterBuilder appendGenericZoneText(TextStyle textStyle) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1303
        appendInternal(new ZoneTextPrinterParser(textStyle, null, true));
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1304
        return this;
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1305
    }
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1306
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1307
    /**
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1308
     * Appends the generic time-zone name, such as 'Pacific Time', to the formatter.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1309
     * <p>
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1310
     * This appends an instruction to format/parse the generic textual
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1311
     * name of the zone to the builder. The generic name is the same throughout the whole
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1312
     * year, ignoring any daylight saving changes. For example, 'Pacific Time' is the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1313
     * generic name, whereas 'Pacific Standard Time' and 'Pacific Daylight Time' are the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1314
     * specific names, see {@link #appendZoneText(TextStyle)}.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1315
     * <p>
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1316
     * This method also allows a set of preferred {@link ZoneId} to be
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1317
     * specified for parsing. The matched preferred zone id will be used if the
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1318
     * textural zone name being parsed is not unique.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1319
     * <p>
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1320
     * See {@link #appendGenericZoneText(TextStyle)} for details about
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1321
     * formatting and parsing.
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1322
     *
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1323
     * @param textStyle  the text style to use, not null
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1324
     * @param preferredZones  the set of preferred zone ids, not null
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1325
     * @return this, for chaining, not null
44263
7a9297d467e7 8176563: @since value errors in apis of java.base/java.logging module
mli
parents: 43007
diff changeset
  1326
     * @since 9
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1327
     */
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1328
    public DateTimeFormatterBuilder appendGenericZoneText(TextStyle textStyle,
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1329
                                                          Set<ZoneId> preferredZones) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1330
        appendInternal(new ZoneTextPrinterParser(textStyle, preferredZones, true));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1331
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1332
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1333
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1334
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1335
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1336
     * Appends the chronology ID, such as 'ISO' or 'ThaiBuddhist', to the formatter.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1337
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1338
     * This appends an instruction to format/parse the chronology ID to the builder.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1339
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1340
     * During formatting, the chronology is obtained using a mechanism equivalent
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  1341
     * to querying the temporal with {@link TemporalQueries#chronology()}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1342
     * It will be printed using the result of {@link Chronology#getId()}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1343
     * If the chronology cannot be obtained then an exception is thrown unless the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1344
     * section of the formatter is optional.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1345
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1346
     * During parsing, the chronology is parsed and must match one of the chronologies
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1347
     * in {@link Chronology#getAvailableChronologies()}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1348
     * If the chronology cannot be parsed then an exception is thrown unless the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1349
     * section of the formatter is optional.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1350
     * The parser uses the {@linkplain #parseCaseInsensitive() case sensitive} setting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1351
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1352
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1353
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1354
    public DateTimeFormatterBuilder appendChronologyId() {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1355
        appendInternal(new ChronoPrinterParser(null));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1356
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1357
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1358
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1359
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1360
     * Appends the chronology name to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1361
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1362
     * The calendar system name will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1363
     * If the chronology cannot be obtained then an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1364
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1365
     * @param textStyle  the text style to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1366
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1367
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1368
    public DateTimeFormatterBuilder appendChronologyText(TextStyle textStyle) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1369
        Objects.requireNonNull(textStyle, "textStyle");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1370
        appendInternal(new ChronoPrinterParser(textStyle));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1371
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1372
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1373
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1374
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1375
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1376
     * Appends a localized date-time pattern to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1377
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1378
     * This appends a localized section to the builder, suitable for outputting
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1379
     * a date, time or date-time combination. The format of the localized
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1380
     * section is lazily looked up based on four items:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
  1381
     * <ul>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1382
     * <li>the {@code dateStyle} specified to this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1383
     * <li>the {@code timeStyle} specified to this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1384
     * <li>the {@code Locale} of the {@code DateTimeFormatter}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1385
     * <li>the {@code Chronology}, selecting the best available
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
  1386
     * </ul>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1387
     * During formatting, the chronology is obtained from the temporal object
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1388
     * being formatted, which may have been overridden by
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1389
     * {@link DateTimeFormatter#withChronology(Chronology)}.
36638
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
  1390
     * The {@code FULL} and {@code LONG} styles typically require a time-zone.
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
  1391
     * When formatting using these styles, a {@code ZoneId} must be available,
bc1438c48f1b 8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception
rriggs
parents: 33675
diff changeset
  1392
     * either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1393
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1394
     * During parsing, if a chronology has already been parsed, then it is used.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1395
     * Otherwise the default from {@code DateTimeFormatter.withChronology(Chronology)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1396
     * is used, with {@code IsoChronology} as the fallback.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1397
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1398
     * Note that this method provides similar functionality to methods on
16005
b480157c22fe 8008434: Misc javadoc warning fixes in DateTimeFormatterBuilder and TimeZone
darcy
parents: 15658
diff changeset
  1399
     * {@code DateFormat} such as {@link java.text.DateFormat#getDateTimeInstance(int, int)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1400
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1401
     * @param dateStyle  the date style to use, null means no date required
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1402
     * @param timeStyle  the time style to use, null means no time required
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1403
     * @return this, for chaining, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1404
     * @throws IllegalArgumentException if both the date and time styles are null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1405
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1406
    public DateTimeFormatterBuilder appendLocalized(FormatStyle dateStyle, FormatStyle timeStyle) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1407
        if (dateStyle == null && timeStyle == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1408
            throw new IllegalArgumentException("Either the date or time style must be non-null");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1409
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1410
        appendInternal(new LocalizedPrinterParser(dateStyle, timeStyle));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1411
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1412
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1413
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1414
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1415
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1416
     * Appends a character literal to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1417
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1418
     * This character will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1419
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1420
     * @param literal  the literal to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1421
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1422
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1423
    public DateTimeFormatterBuilder appendLiteral(char literal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1424
        appendInternal(new CharLiteralPrinterParser(literal));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1425
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1426
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1427
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1428
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1429
     * Appends a string literal to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1430
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1431
     * This string will be output during a format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1432
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1433
     * If the literal is empty, nothing is added to the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1434
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1435
     * @param literal  the literal to append, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1436
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1437
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1438
    public DateTimeFormatterBuilder appendLiteral(String literal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1439
        Objects.requireNonNull(literal, "literal");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52012
diff changeset
  1440
        if (!literal.isEmpty()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1441
            if (literal.length() == 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1442
                appendInternal(new CharLiteralPrinterParser(literal.charAt(0)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1443
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1444
                appendInternal(new StringLiteralPrinterParser(literal));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1445
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1446
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1447
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1448
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1449
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1450
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1451
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1452
     * Appends all the elements of a formatter to the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1453
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1454
     * This method has the same effect as appending each of the constituent
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1455
     * parts of the formatter directly to this builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1456
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1457
     * @param formatter  the formatter to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1458
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1459
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1460
    public DateTimeFormatterBuilder append(DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1461
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1462
        appendInternal(formatter.toPrinterParser(false));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1463
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1464
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1465
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1466
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1467
     * Appends a formatter to the builder which will optionally format/parse.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1468
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1469
     * This method has the same effect as appending each of the constituent
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1470
     * parts directly to this builder surrounded by an {@link #optionalStart()} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1471
     * {@link #optionalEnd()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1472
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1473
     * The formatter will format if data is available for all the fields contained within it.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1474
     * The formatter will parse if the string matches, otherwise no error is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1475
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1476
     * @param formatter  the formatter to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1477
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1478
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1479
    public DateTimeFormatterBuilder appendOptional(DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1480
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1481
        appendInternal(formatter.toPrinterParser(true));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1482
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1483
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1484
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1485
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1486
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1487
     * Appends the elements defined by the specified pattern to the builder.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1488
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1489
     * All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1490
     * The characters '#', '{' and '}' are reserved for future use.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1491
     * The characters '[' and ']' indicate optional patterns.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1492
     * The following pattern letters are defined:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1493
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1494
     *  Symbol  Meaning                     Presentation      Examples
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1495
     *  ------  -------                     ------------      -------
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1496
     *   G       era                         text              AD; Anno Domini; A
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1497
     *   u       year                        year              2004; 04
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1498
     *   y       year-of-era                 year              2004; 04
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1499
     *   D       day-of-year                 number            189
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1500
     *   M/L     month-of-year               number/text       7; 07; Jul; July; J
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1501
     *   d       day-of-month                number            10
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1502
     *   g       modified-julian-day         number            2451334
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1503
     *
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1504
     *   Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1505
     *   Y       week-based-year             year              1996; 96
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1506
     *   w       week-of-week-based-year     number            27
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1507
     *   W       week-of-month               number            4
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1508
     *   E       day-of-week                 text              Tue; Tuesday; T
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1509
     *   e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
43007
4de62a95642c 8171348: Incorrect documentation for DateTimeFormatter letter 'k'
rriggs
parents: 42924
diff changeset
  1510
     *   F       day-of-week-in-month        number            3
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1511
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1512
     *   a       am-pm-of-day                text              PM
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1513
     *   h       clock-hour-of-am-pm (1-12)  number            12
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1514
     *   K       hour-of-am-pm (0-11)        number            0
43007
4de62a95642c 8171348: Incorrect documentation for DateTimeFormatter letter 'k'
rriggs
parents: 42924
diff changeset
  1515
     *   k       clock-hour-of-day (1-24)    number            24
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1516
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1517
     *   H       hour-of-day (0-23)          number            0
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1518
     *   m       minute-of-hour              number            30
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1519
     *   s       second-of-minute            number            55
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1520
     *   S       fraction-of-second          fraction          978
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1521
     *   A       milli-of-day                number            1234
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1522
     *   n       nano-of-second              number            987654321
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1523
     *   N       nano-of-day                 number            1234000000
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1524
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1525
     *   V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1526
     *   v       generic time-zone name      zone-name         PT, Pacific Time
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1527
     *   z       time-zone name              zone-name         Pacific Standard Time; PST
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1528
     *   O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1529
     *   X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1530
     *   x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1531
     *   Z       zone-offset                 offset-Z          +0000; -0800; -08:00
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1532
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1533
     *   p       pad next                    pad modifier      1
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1534
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1535
     *   '       escape for text             delimiter
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1536
     *   ''      single quote                literal           '
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1537
     *   [       optional section start
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1538
     *   ]       optional section end
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1539
     *   #       reserved for future use
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1540
     *   {       reserved for future use
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1541
     *   }       reserved for future use
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1542
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1543
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1544
     * The count of pattern letters determine the format.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1545
     * See <a href="DateTimeFormatter.html#patterns">DateTimeFormatter</a> for a user-focused description of the patterns.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1546
     * The following tables define how the pattern letters map to the builder.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1547
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1548
     * <b>Date fields</b>: Pattern letters to output a date.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1549
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1550
     *  Pattern  Count  Equivalent builder methods
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1551
     *  -------  -----  --------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1552
     *    G       1      appendText(ChronoField.ERA, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1553
     *    GG      2      appendText(ChronoField.ERA, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1554
     *    GGG     3      appendText(ChronoField.ERA, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1555
     *    GGGG    4      appendText(ChronoField.ERA, TextStyle.FULL)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1556
     *    GGGGG   5      appendText(ChronoField.ERA, TextStyle.NARROW)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1557
     *
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1558
     *    u       1      appendValue(ChronoField.YEAR, 1, 19, SignStyle.NORMAL)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1559
     *    uu      2      appendValueReduced(ChronoField.YEAR, 2, 2000)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1560
     *    uuu     3      appendValue(ChronoField.YEAR, 3, 19, SignStyle.NORMAL)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1561
     *    u..u    4..n   appendValue(ChronoField.YEAR, n, 19, SignStyle.EXCEEDS_PAD)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1562
     *    y       1      appendValue(ChronoField.YEAR_OF_ERA, 1, 19, SignStyle.NORMAL)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1563
     *    yy      2      appendValueReduced(ChronoField.YEAR_OF_ERA, 2, 2000)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1564
     *    yyy     3      appendValue(ChronoField.YEAR_OF_ERA, 3, 19, SignStyle.NORMAL)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1565
     *    y..y    4..n   appendValue(ChronoField.YEAR_OF_ERA, n, 19, SignStyle.EXCEEDS_PAD)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1566
     *    Y       1      append special localized WeekFields element for numeric week-based-year
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1567
     *    YY      2      append special localized WeekFields element for reduced numeric week-based-year 2 digits
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1568
     *    YYY     3      append special localized WeekFields element for numeric week-based-year (3, 19, SignStyle.NORMAL)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1569
     *    Y..Y    4..n   append special localized WeekFields element for numeric week-based-year (n, 19, SignStyle.EXCEEDS_PAD)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1570
     *
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1571
     *    Q       1      appendValue(IsoFields.QUARTER_OF_YEAR)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1572
     *    QQ      2      appendValue(IsoFields.QUARTER_OF_YEAR, 2)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1573
     *    QQQ     3      appendText(IsoFields.QUARTER_OF_YEAR, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1574
     *    QQQQ    4      appendText(IsoFields.QUARTER_OF_YEAR, TextStyle.FULL)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1575
     *    QQQQQ   5      appendText(IsoFields.QUARTER_OF_YEAR, TextStyle.NARROW)
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1576
     *    q       1      appendValue(IsoFields.QUARTER_OF_YEAR)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1577
     *    qq      2      appendValue(IsoFields.QUARTER_OF_YEAR, 2)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1578
     *    qqq     3      appendText(IsoFields.QUARTER_OF_YEAR, TextStyle.SHORT_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1579
     *    qqqq    4      appendText(IsoFields.QUARTER_OF_YEAR, TextStyle.FULL_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1580
     *    qqqqq   5      appendText(IsoFields.QUARTER_OF_YEAR, TextStyle.NARROW_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1581
     *
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1582
     *    M       1      appendValue(ChronoField.MONTH_OF_YEAR)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1583
     *    MM      2      appendValue(ChronoField.MONTH_OF_YEAR, 2)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1584
     *    MMM     3      appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1585
     *    MMMM    4      appendText(ChronoField.MONTH_OF_YEAR, TextStyle.FULL)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1586
     *    MMMMM   5      appendText(ChronoField.MONTH_OF_YEAR, TextStyle.NARROW)
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1587
     *    L       1      appendValue(ChronoField.MONTH_OF_YEAR)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1588
     *    LL      2      appendValue(ChronoField.MONTH_OF_YEAR, 2)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1589
     *    LLL     3      appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1590
     *    LLLL    4      appendText(ChronoField.MONTH_OF_YEAR, TextStyle.FULL_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1591
     *    LLLLL   5      appendText(ChronoField.MONTH_OF_YEAR, TextStyle.NARROW_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1592
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1593
     *    w       1      append special localized WeekFields element for numeric week-of-year
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 23720
diff changeset
  1594
     *    ww      2      append special localized WeekFields element for numeric week-of-year, zero-padded
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1595
     *    W       1      append special localized WeekFields element for numeric week-of-month
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1596
     *    d       1      appendValue(ChronoField.DAY_OF_MONTH)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1597
     *    dd      2      appendValue(ChronoField.DAY_OF_MONTH, 2)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1598
     *    D       1      appendValue(ChronoField.DAY_OF_YEAR)
37812
6fd749bfad2d 8079628: java.time: DateTimeFormatter containing "DD" fails on 3-digit day-of-year value`
ntv
parents: 37801
diff changeset
  1599
     *    DD      2      appendValue(ChronoField.DAY_OF_YEAR, 2, 3, SignStyle.NOT_NEGATIVE)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1600
     *    DDD     3      appendValue(ChronoField.DAY_OF_YEAR, 3)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1601
     *    F       1      appendValue(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH)
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1602
     *    g..g    1..n   appendValue(JulianFields.MODIFIED_JULIAN_DAY, n, 19, SignStyle.NORMAL)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1603
     *    E       1      appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1604
     *    EE      2      appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1605
     *    EEE     3      appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1606
     *    EEEE    4      appendText(ChronoField.DAY_OF_WEEK, TextStyle.FULL)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1607
     *    EEEEE   5      appendText(ChronoField.DAY_OF_WEEK, TextStyle.NARROW)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1608
     *    e       1      append special localized WeekFields element for numeric day-of-week
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1609
     *    ee      2      append special localized WeekFields element for numeric day-of-week, zero-padded
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1610
     *    eee     3      appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1611
     *    eeee    4      appendText(ChronoField.DAY_OF_WEEK, TextStyle.FULL)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1612
     *    eeeee   5      appendText(ChronoField.DAY_OF_WEEK, TextStyle.NARROW)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1613
     *    c       1      append special localized WeekFields element for numeric day-of-week
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1614
     *    ccc     3      appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1615
     *    cccc    4      appendText(ChronoField.DAY_OF_WEEK, TextStyle.FULL_STANDALONE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1616
     *    ccccc   5      appendText(ChronoField.DAY_OF_WEEK, TextStyle.NARROW_STANDALONE)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1617
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1618
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1619
     * <b>Time fields</b>: Pattern letters to output a time.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1620
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1621
     *  Pattern  Count  Equivalent builder methods
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1622
     *  -------  -----  --------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1623
     *    a       1      appendText(ChronoField.AMPM_OF_DAY, TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1624
     *    h       1      appendValue(ChronoField.CLOCK_HOUR_OF_AMPM)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1625
     *    hh      2      appendValue(ChronoField.CLOCK_HOUR_OF_AMPM, 2)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1626
     *    H       1      appendValue(ChronoField.HOUR_OF_DAY)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1627
     *    HH      2      appendValue(ChronoField.HOUR_OF_DAY, 2)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1628
     *    k       1      appendValue(ChronoField.CLOCK_HOUR_OF_DAY)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1629
     *    kk      2      appendValue(ChronoField.CLOCK_HOUR_OF_DAY, 2)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1630
     *    K       1      appendValue(ChronoField.HOUR_OF_AMPM)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1631
     *    KK      2      appendValue(ChronoField.HOUR_OF_AMPM, 2)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1632
     *    m       1      appendValue(ChronoField.MINUTE_OF_HOUR)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1633
     *    mm      2      appendValue(ChronoField.MINUTE_OF_HOUR, 2)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1634
     *    s       1      appendValue(ChronoField.SECOND_OF_MINUTE)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1635
     *    ss      2      appendValue(ChronoField.SECOND_OF_MINUTE, 2)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1636
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1637
     *    S..S    1..n   appendFraction(ChronoField.NANO_OF_SECOND, n, n, false)
37801
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1638
     *    A..A    1..n   appendValue(ChronoField.MILLI_OF_DAY, n, 19, SignStyle.NOT_NEGATIVE)
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1639
     *    n..n    1..n   appendValue(ChronoField.NANO_OF_SECOND, n, 19, SignStyle.NOT_NEGATIVE)
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1640
     *    N..N    1..n   appendValue(ChronoField.NANO_OF_DAY, n, 19, SignStyle.NOT_NEGATIVE)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1641
     * </pre>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1642
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1643
     * <b>Zone ID</b>: Pattern letters to output {@code ZoneId}.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1644
     * <pre>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1645
     *  Pattern  Count  Equivalent builder methods
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1646
     *  -------  -----  --------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1647
     *    VV      2      appendZoneId()
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1648
     *    v       1      appendGenericZoneText(TextStyle.SHORT)
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1649
     *    vvvv    4      appendGenericZoneText(TextStyle.FULL)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1650
     *    z       1      appendZoneText(TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1651
     *    zz      2      appendZoneText(TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1652
     *    zzz     3      appendZoneText(TextStyle.SHORT)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1653
     *    zzzz    4      appendZoneText(TextStyle.FULL)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1654
     * </pre>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1655
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1656
     * <b>Zone offset</b>: Pattern letters to output {@code ZoneOffset}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1657
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1658
     *  Pattern  Count  Equivalent builder methods
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1659
     *  -------  -----  --------------------------
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1660
     *    O       1      appendLocalizedOffset(TextStyle.SHORT)
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1661
     *    OOOO    4      appendLocalizedOffset(TextStyle.FULL)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1662
     *    X       1      appendOffset("+HHmm","Z")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1663
     *    XX      2      appendOffset("+HHMM","Z")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1664
     *    XXX     3      appendOffset("+HH:MM","Z")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1665
     *    XXXX    4      appendOffset("+HHMMss","Z")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1666
     *    XXXXX   5      appendOffset("+HH:MM:ss","Z")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1667
     *    x       1      appendOffset("+HHmm","+00")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1668
     *    xx      2      appendOffset("+HHMM","+0000")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1669
     *    xxx     3      appendOffset("+HH:MM","+00:00")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1670
     *    xxxx    4      appendOffset("+HHMMss","+0000")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1671
     *    xxxxx   5      appendOffset("+HH:MM:ss","+00:00")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1672
     *    Z       1      appendOffset("+HHMM","+0000")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1673
     *    ZZ      2      appendOffset("+HHMM","+0000")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1674
     *    ZZZ     3      appendOffset("+HHMM","+0000")
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1675
     *    ZZZZ    4      appendLocalizedOffset(TextStyle.FULL)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1676
     *    ZZZZZ   5      appendOffset("+HH:MM:ss","Z")
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1677
     * </pre>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1678
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1679
     * <b>Modifiers</b>: Pattern letters that modify the rest of the pattern:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1680
     * <pre>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1681
     *  Pattern  Count  Equivalent builder methods
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1682
     *  -------  -----  --------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1683
     *    [       1      optionalStart()
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1684
     *    ]       1      optionalEnd()
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1685
     *    p..p    1..n   padNext(n)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1686
     * </pre>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1687
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1688
     * Any sequence of letters not specified above, unrecognized letter or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1689
     * reserved character will throw an exception.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1690
     * Future versions may add to the set of patterns.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1691
     * It is recommended to use single quotes around all characters that you want
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1692
     * to output directly to ensure that future changes do not break your application.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1693
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1694
     * Note that the pattern string is similar, but not identical, to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1695
     * {@link java.text.SimpleDateFormat SimpleDateFormat}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1696
     * The pattern string is also similar, but not identical, to that defined by the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1697
     * Unicode Common Locale Data Repository (CLDR/LDML).
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1698
     * Pattern letters 'X' and 'u' are aligned with Unicode CLDR/LDML.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1699
     * By contrast, {@code SimpleDateFormat} uses 'u' for the numeric day of week.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1700
     * Pattern letters 'y' and 'Y' parse years of two digits and more than 4 digits differently.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1701
     * Pattern letters 'n', 'A', 'N', and 'p' are added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1702
     * Number types will reject large numbers.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1703
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1704
     * @param pattern  the pattern to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1705
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1706
     * @throws IllegalArgumentException if the pattern is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1707
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1708
    public DateTimeFormatterBuilder appendPattern(String pattern) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1709
        Objects.requireNonNull(pattern, "pattern");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1710
        parsePattern(pattern);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1711
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1712
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1713
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1714
    private void parsePattern(String pattern) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1715
        for (int pos = 0; pos < pattern.length(); pos++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1716
            char cur = pattern.charAt(pos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1717
            if ((cur >= 'A' && cur <= 'Z') || (cur >= 'a' && cur <= 'z')) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1718
                int start = pos++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1719
                for ( ; pos < pattern.length() && pattern.charAt(pos) == cur; pos++);  // short loop
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1720
                int count = pos - start;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1721
                // padding
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1722
                if (cur == 'p') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1723
                    int pad = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1724
                    if (pos < pattern.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1725
                        cur = pattern.charAt(pos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1726
                        if ((cur >= 'A' && cur <= 'Z') || (cur >= 'a' && cur <= 'z')) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1727
                            pad = count;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1728
                            start = pos++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1729
                            for ( ; pos < pattern.length() && pattern.charAt(pos) == cur; pos++);  // short loop
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1730
                            count = pos - start;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1731
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1732
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1733
                    if (pad == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1734
                        throw new IllegalArgumentException(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1735
                                "Pad letter 'p' must be followed by valid pad pattern: " + pattern);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1736
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1737
                    padNext(pad); // pad and continue parsing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1738
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1739
                // main rules
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1740
                TemporalField field = FIELD_MAP.get(cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1741
                if (field != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1742
                    parseField(cur, count, field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1743
                } else if (cur == 'z') {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1744
                    if (count > 4) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1745
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1746
                    } else if (count == 4) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1747
                        appendZoneText(TextStyle.FULL);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1748
                    } else {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1749
                        appendZoneText(TextStyle.SHORT);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1750
                    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1751
                } else if (cur == 'V') {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1752
                    if (count != 2) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1753
                        throw new IllegalArgumentException("Pattern letter count must be 2: " + cur);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1754
                    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1755
                    appendZoneId();
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1756
                } else if (cur == 'v') {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1757
                    if (count == 1) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1758
                        appendGenericZoneText(TextStyle.SHORT);
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1759
                    } else if (count == 4) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1760
                        appendGenericZoneText(TextStyle.FULL);
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1761
                    } else {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1762
                        throw new IllegalArgumentException("Wrong number of  pattern letters: " + cur);
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  1763
                    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1764
                } else if (cur == 'Z') {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1765
                    if (count < 4) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1766
                        appendOffset("+HHMM", "+0000");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1767
                    } else if (count == 4) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1768
                        appendLocalizedOffset(TextStyle.FULL);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1769
                    } else if (count == 5) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1770
                        appendOffset("+HH:MM:ss","Z");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1771
                    } else {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1772
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1773
                    }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1774
                } else if (cur == 'O') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1775
                    if (count == 1) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1776
                        appendLocalizedOffset(TextStyle.SHORT);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1777
                    } else if (count == 4) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1778
                        appendLocalizedOffset(TextStyle.FULL);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1779
                    } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1780
                        throw new IllegalArgumentException("Pattern letter count must be 1 or 4: " + cur);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1781
                    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1782
                } else if (cur == 'X') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1783
                    if (count > 5) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1784
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1785
                    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1786
                    appendOffset(OffsetIdPrinterParser.PATTERNS[count + (count == 1 ? 0 : 1)], "Z");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1787
                } else if (cur == 'x') {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1788
                    if (count > 5) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1789
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1790
                    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1791
                    String zero = (count == 1 ? "+00" : (count % 2 == 0 ? "+0000" : "+00:00"));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1792
                    appendOffset(OffsetIdPrinterParser.PATTERNS[count + (count == 1 ? 0 : 1)], zero);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1793
                } else if (cur == 'W') {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1794
                    // Fields defined by Locale
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1795
                    if (count > 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1796
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1797
                    }
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1798
                    appendValue(new WeekBasedFieldPrinterParser(cur, count, count, count));
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1799
                } else if (cur == 'w') {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1800
                    // Fields defined by Locale
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1801
                    if (count > 2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1802
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1803
                    }
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1804
                    appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 2));
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1805
                } else if (cur == 'Y') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1806
                    // Fields defined by Locale
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1807
                    if (count == 2) {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1808
                        appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 2));
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1809
                    } else {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1810
                        appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 19));
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1811
                    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1812
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1813
                    throw new IllegalArgumentException("Unknown pattern letter: " + cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1814
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1815
                pos--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1816
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1817
            } else if (cur == '\'') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1818
                // parse literals
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1819
                int start = pos++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1820
                for ( ; pos < pattern.length(); pos++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1821
                    if (pattern.charAt(pos) == '\'') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1822
                        if (pos + 1 < pattern.length() && pattern.charAt(pos + 1) == '\'') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1823
                            pos++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1824
                        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1825
                            break;  // end of literal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1826
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1827
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1828
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1829
                if (pos >= pattern.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1830
                    throw new IllegalArgumentException("Pattern ends with an incomplete string literal: " + pattern);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1831
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1832
                String str = pattern.substring(start + 1, pos);
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52012
diff changeset
  1833
                if (str.isEmpty()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1834
                    appendLiteral('\'');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1835
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1836
                    appendLiteral(str.replace("''", "'"));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1837
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1838
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1839
            } else if (cur == '[') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1840
                optionalStart();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1841
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1842
            } else if (cur == ']') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1843
                if (active.parent == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1844
                    throw new IllegalArgumentException("Pattern invalid as it contains ] without previous [");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1845
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1846
                optionalEnd();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1847
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1848
            } else if (cur == '{' || cur == '}' || cur == '#') {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1849
                throw new IllegalArgumentException("Pattern includes reserved character: '" + cur + "'");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1850
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1851
                appendLiteral(cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1852
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1853
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1854
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1855
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1856
    @SuppressWarnings("fallthrough")
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1857
    private void parseField(char cur, int count, TemporalField field) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1858
        boolean standalone = false;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1859
        switch (cur) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1860
            case 'u':
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1861
            case 'y':
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1862
                if (count == 2) {
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  1863
                    appendValueReduced(field, 2, 2, ReducedPrinterParser.BASE_DATE);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1864
                } else if (count < 4) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1865
                    appendValue(field, count, 19, SignStyle.NORMAL);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1866
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1867
                    appendValue(field, count, 19, SignStyle.EXCEEDS_PAD);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1868
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1869
                break;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1870
            case 'c':
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1871
                if (count == 1) {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1872
                    appendValue(new WeekBasedFieldPrinterParser(cur, count, count, count));
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1873
                    break;
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1874
                } else if (count == 2) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1875
                    throw new IllegalArgumentException("Invalid pattern \"cc\"");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1876
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1877
                /*fallthrough*/
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1878
            case 'L':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1879
            case 'q':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1880
                standalone = true;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1881
                /*fallthrough*/
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1882
            case 'M':
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1883
            case 'Q':
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1884
            case 'E':
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1885
            case 'e':
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1886
                switch (count) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1887
                    case 1:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1888
                    case 2:
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1889
                        if (cur == 'e') {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  1890
                            appendValue(new WeekBasedFieldPrinterParser(cur, count, count, count));
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1891
                        } else if (cur == 'E') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1892
                            appendText(field, TextStyle.SHORT);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1893
                        } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1894
                            if (count == 1) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1895
                                appendValue(field);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1896
                            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1897
                                appendValue(field, 2);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1898
                            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1899
                        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1900
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1901
                    case 3:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1902
                        appendText(field, standalone ? TextStyle.SHORT_STANDALONE : TextStyle.SHORT);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1903
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1904
                    case 4:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1905
                        appendText(field, standalone ? TextStyle.FULL_STANDALONE : TextStyle.FULL);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1906
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1907
                    case 5:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1908
                        appendText(field, standalone ? TextStyle.NARROW_STANDALONE : TextStyle.NARROW);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1909
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1910
                    default:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1911
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1912
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1913
                break;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1914
            case 'a':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1915
                if (count == 1) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1916
                    appendText(field, TextStyle.SHORT);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1917
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1918
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1919
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1920
                break;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1921
            case 'G':
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1922
                switch (count) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1923
                    case 1:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1924
                    case 2:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1925
                    case 3:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1926
                        appendText(field, TextStyle.SHORT);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1927
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1928
                    case 4:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1929
                        appendText(field, TextStyle.FULL);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1930
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1931
                    case 5:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1932
                        appendText(field, TextStyle.NARROW);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1933
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1934
                    default:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1935
                        throw new IllegalArgumentException("Too many pattern letters: " + cur);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1936
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1937
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1938
            case 'S':
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1939
                appendFraction(NANO_OF_SECOND, count, count, false);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1940
                break;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1941
            case 'F':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1942
                if (count == 1) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1943
                    appendValue(field);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1944
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1945
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1946
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1947
                break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1948
            case 'd':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1949
            case 'h':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1950
            case 'H':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1951
            case 'k':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1952
            case 'K':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1953
            case 'm':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1954
            case 's':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1955
                if (count == 1) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1956
                    appendValue(field);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1957
                } else if (count == 2) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1958
                    appendValue(field, count);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1959
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1960
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1961
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1962
                break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1963
            case 'D':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1964
                if (count == 1) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1965
                    appendValue(field);
37812
6fd749bfad2d 8079628: java.time: DateTimeFormatter containing "DD" fails on 3-digit day-of-year value`
ntv
parents: 37801
diff changeset
  1966
                } else if (count == 2 || count == 3) {
6fd749bfad2d 8079628: java.time: DateTimeFormatter containing "DD" fails on 3-digit day-of-year value`
ntv
parents: 37801
diff changeset
  1967
                    appendValue(field, count, 3, SignStyle.NOT_NEGATIVE);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1968
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1969
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1970
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1971
                break;
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1972
            case 'g':
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1973
                appendValue(field, count, 19, SignStyle.NORMAL);
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  1974
                break;
37801
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1975
            case 'A':
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1976
            case 'n':
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1977
            case 'N':
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1978
                appendValue(field, count, 19, SignStyle.NOT_NEGATIVE);
35a1414bbf12 8148949: DateTimeFormatter pattern letters 'A','n','N'
ntv
parents: 37610
diff changeset
  1979
                break;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1980
            default:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1981
                if (count == 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1982
                    appendValue(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1983
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1984
                    appendValue(field, count);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1985
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1986
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1987
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1988
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1989
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1990
    /** Map of letters to fields. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1991
    private static final Map<Character, TemporalField> FIELD_MAP = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1992
    static {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1993
        // SDF = SimpleDateFormat
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1994
        FIELD_MAP.put('G', ChronoField.ERA);                       // SDF, LDML (different to both for 1/2 chars)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1995
        FIELD_MAP.put('y', ChronoField.YEAR_OF_ERA);               // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1996
        FIELD_MAP.put('u', ChronoField.YEAR);                      // LDML (different in SDF)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1997
        FIELD_MAP.put('Q', IsoFields.QUARTER_OF_YEAR);             // LDML (removed quarter from 310)
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1998
        FIELD_MAP.put('q', IsoFields.QUARTER_OF_YEAR);             // LDML (stand-alone)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  1999
        FIELD_MAP.put('M', ChronoField.MONTH_OF_YEAR);             // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2000
        FIELD_MAP.put('L', ChronoField.MONTH_OF_YEAR);             // SDF, LDML (stand-alone)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2001
        FIELD_MAP.put('D', ChronoField.DAY_OF_YEAR);               // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2002
        FIELD_MAP.put('d', ChronoField.DAY_OF_MONTH);              // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2003
        FIELD_MAP.put('F', ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH);  // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2004
        FIELD_MAP.put('E', ChronoField.DAY_OF_WEEK);               // SDF, LDML (different to both for 1/2 chars)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2005
        FIELD_MAP.put('c', ChronoField.DAY_OF_WEEK);               // LDML (stand-alone)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2006
        FIELD_MAP.put('e', ChronoField.DAY_OF_WEEK);               // LDML (needs localized week number)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2007
        FIELD_MAP.put('a', ChronoField.AMPM_OF_DAY);               // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2008
        FIELD_MAP.put('H', ChronoField.HOUR_OF_DAY);               // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2009
        FIELD_MAP.put('k', ChronoField.CLOCK_HOUR_OF_DAY);         // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2010
        FIELD_MAP.put('K', ChronoField.HOUR_OF_AMPM);              // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2011
        FIELD_MAP.put('h', ChronoField.CLOCK_HOUR_OF_AMPM);        // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2012
        FIELD_MAP.put('m', ChronoField.MINUTE_OF_HOUR);            // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2013
        FIELD_MAP.put('s', ChronoField.SECOND_OF_MINUTE);          // SDF, LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2014
        FIELD_MAP.put('S', ChronoField.NANO_OF_SECOND);            // LDML (SDF uses milli-of-second number)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2015
        FIELD_MAP.put('A', ChronoField.MILLI_OF_DAY);              // LDML
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2016
        FIELD_MAP.put('n', ChronoField.NANO_OF_SECOND);            // 310 (proposed for LDML)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2017
        FIELD_MAP.put('N', ChronoField.NANO_OF_DAY);               // 310 (proposed for LDML)
37599
9ee25a0a2c01 8148947: DateTimeFormatter pattern letter 'g'
ntv
parents: 37596
diff changeset
  2018
        FIELD_MAP.put('g', JulianFields.MODIFIED_JULIAN_DAY);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2019
        // 310 - z - time-zone names, matches LDML and SimpleDateFormat 1 to 4
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2020
        // 310 - Z - matches SimpleDateFormat and LDML
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2021
        // 310 - V - time-zone id, matches LDML
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  2022
        // 310 - v - general timezone names, not matching exactly with LDML because LDML specify to fall back
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  2023
        //           to 'VVVV' if general-nonlocation unavailable but here it's not falling back because of lack of data
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2024
        // 310 - p - prefix for padding
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2025
        // 310 - X - matches LDML, almost matches SDF for 1, exact match 2&3, extended 4&5
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2026
        // 310 - x - matches LDML
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2027
        // 310 - w, W, and Y are localized forms matching LDML
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2028
        // LDML - U - cycle year name, not supported by 310 yet
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2029
        // LDML - l - deprecated
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2030
        // LDML - j - not relevant
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2031
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2032
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2033
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2034
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2035
     * Causes the next added printer/parser to pad to a fixed width using a space.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2036
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2037
     * This padding will pad to a fixed width using spaces.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2038
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2039
     * During formatting, the decorated element will be output and then padded
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2040
     * to the specified width. An exception will be thrown during formatting if
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2041
     * the pad width is exceeded.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2042
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2043
     * During parsing, the padding and decorated element are parsed.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2044
     * If parsing is lenient, then the pad width is treated as a maximum.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2045
     * The padding is parsed greedily. Thus, if the decorated element starts with
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2046
     * the pad character, it will not be parsed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2047
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2048
     * @param padWidth  the pad width, 1 or greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2049
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2050
     * @throws IllegalArgumentException if pad width is too small
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2051
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2052
    public DateTimeFormatterBuilder padNext(int padWidth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2053
        return padNext(padWidth, ' ');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2054
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2055
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2056
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2057
     * Causes the next added printer/parser to pad to a fixed width.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2058
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2059
     * This padding is intended for padding other than zero-padding.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2060
     * Zero-padding should be achieved using the appendValue methods.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2061
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2062
     * During formatting, the decorated element will be output and then padded
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2063
     * to the specified width. An exception will be thrown during formatting if
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2064
     * the pad width is exceeded.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2065
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2066
     * During parsing, the padding and decorated element are parsed.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2067
     * If parsing is lenient, then the pad width is treated as a maximum.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2068
     * If parsing is case insensitive, then the pad character is matched ignoring case.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2069
     * The padding is parsed greedily. Thus, if the decorated element starts with
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2070
     * the pad character, it will not be parsed.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2071
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2072
     * @param padWidth  the pad width, 1 or greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2073
     * @param padChar  the pad character
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2074
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2075
     * @throws IllegalArgumentException if pad width is too small
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2076
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2077
    public DateTimeFormatterBuilder padNext(int padWidth, char padChar) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2078
        if (padWidth < 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2079
            throw new IllegalArgumentException("The pad width must be at least one but was " + padWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2080
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2081
        active.padNextWidth = padWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2082
        active.padNextChar = padChar;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2083
        active.valueParserIndex = -1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2084
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2085
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2086
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2087
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2088
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2089
     * Mark the start of an optional section.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2090
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2091
     * The output of formatting can include optional sections, which may be nested.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2092
     * An optional section is started by calling this method and ended by calling
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2093
     * {@link #optionalEnd()} or by ending the build process.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2094
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2095
     * All elements in the optional section are treated as optional.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2096
     * During formatting, the section is only output if data is available in the
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2097
     * {@code TemporalAccessor} for all the elements in the section.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2098
     * During parsing, the whole section may be missing from the parsed string.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2099
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2100
     * For example, consider a builder setup as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2101
     * {@code builder.appendValue(HOUR_OF_DAY,2).optionalStart().appendValue(MINUTE_OF_HOUR,2)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2102
     * The optional section ends automatically at the end of the builder.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2103
     * During formatting, the minute will only be output if its value can be obtained from the date-time.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2104
     * During parsing, the input will be successfully parsed whether the minute is present or not.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2105
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2106
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2107
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2108
    public DateTimeFormatterBuilder optionalStart() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2109
        active.valueParserIndex = -1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2110
        active = new DateTimeFormatterBuilder(active, true);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2111
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2112
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2113
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2114
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2115
     * Ends an optional section.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2116
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2117
     * The output of formatting can include optional sections, which may be nested.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2118
     * An optional section is started by calling {@link #optionalStart()} and ended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2119
     * using this method (or at the end of the builder).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2120
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2121
     * Calling this method without having previously called {@code optionalStart}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2122
     * will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2123
     * Calling this method immediately after calling {@code optionalStart} has no effect
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2124
     * on the formatter other than ending the (empty) optional section.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2125
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2126
     * All elements in the optional section are treated as optional.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2127
     * During formatting, the section is only output if data is available in the
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2128
     * {@code TemporalAccessor} for all the elements in the section.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2129
     * During parsing, the whole section may be missing from the parsed string.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2130
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2131
     * For example, consider a builder setup as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2132
     * {@code builder.appendValue(HOUR_OF_DAY,2).optionalStart().appendValue(MINUTE_OF_HOUR,2).optionalEnd()}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2133
     * During formatting, the minute will only be output if its value can be obtained from the date-time.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2134
     * During parsing, the input will be successfully parsed whether the minute is present or not.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2135
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2136
     * @return this, for chaining, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2137
     * @throws IllegalStateException if there was no previous call to {@code optionalStart}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2138
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2139
    public DateTimeFormatterBuilder optionalEnd() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2140
        if (active.parent == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2141
            throw new IllegalStateException("Cannot call optionalEnd() as there was no previous call to optionalStart()");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2142
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2143
        if (active.printerParsers.size() > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2144
            CompositePrinterParser cpp = new CompositePrinterParser(active.printerParsers, active.optional);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2145
            active = active.parent;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2146
            appendInternal(cpp);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2147
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2148
            active = active.parent;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2149
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2150
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2151
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2152
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2153
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2154
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2155
     * Appends a printer and/or parser to the internal list handling padding.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2156
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2157
     * @param pp  the printer-parser to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2158
     * @return the index into the active parsers list
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2159
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2160
    private int appendInternal(DateTimePrinterParser pp) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2161
        Objects.requireNonNull(pp, "pp");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2162
        if (active.padNextWidth > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2163
            if (pp != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2164
                pp = new PadPrinterParserDecorator(pp, active.padNextWidth, active.padNextChar);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2165
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2166
            active.padNextWidth = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2167
            active.padNextChar = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2168
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2169
        active.printerParsers.add(pp);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2170
        active.valueParserIndex = -1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2171
        return active.printerParsers.size() - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2172
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2173
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2174
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2175
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2176
     * Completes this builder by creating the {@code DateTimeFormatter}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2177
     * using the default locale.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2178
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2179
     * This will create a formatter with the {@linkplain Locale#getDefault(Locale.Category) default FORMAT locale}.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2180
     * Numbers will be printed and parsed using the standard DecimalStyle.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2181
     * The resolver style will be {@link ResolverStyle#SMART SMART}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2182
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2183
     * Calling this method will end any open optional sections by repeatedly
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2184
     * calling {@link #optionalEnd()} before creating the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2185
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2186
     * This builder can still be used after creating the formatter if desired,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2187
     * although the state may have been changed by calls to {@code optionalEnd}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2188
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2189
     * @return the created formatter, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2190
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2191
    public DateTimeFormatter toFormatter() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2192
        return toFormatter(Locale.getDefault(Locale.Category.FORMAT));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2193
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2194
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2195
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2196
     * Completes this builder by creating the {@code DateTimeFormatter}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2197
     * using the specified locale.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2198
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2199
     * This will create a formatter with the specified locale.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2200
     * Numbers will be printed and parsed using the standard DecimalStyle.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2201
     * The resolver style will be {@link ResolverStyle#SMART SMART}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2202
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2203
     * Calling this method will end any open optional sections by repeatedly
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2204
     * calling {@link #optionalEnd()} before creating the formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2205
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2206
     * This builder can still be used after creating the formatter if desired,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2207
     * although the state may have been changed by calls to {@code optionalEnd}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2208
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2209
     * @param locale  the locale to use for formatting, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2210
     * @return the created formatter, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2211
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2212
    public DateTimeFormatter toFormatter(Locale locale) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2213
        return toFormatter(locale, ResolverStyle.SMART, null);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2214
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2215
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2216
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2217
     * Completes this builder by creating the formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2218
     * This uses the default locale.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2219
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2220
     * @param resolverStyle  the resolver style to use, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2221
     * @return the created formatter, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2222
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2223
    DateTimeFormatter toFormatter(ResolverStyle resolverStyle, Chronology chrono) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2224
        return toFormatter(Locale.getDefault(Locale.Category.FORMAT), resolverStyle, chrono);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2225
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2226
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2227
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2228
     * Completes this builder by creating the formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2229
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2230
     * @param locale  the locale to use for formatting, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2231
     * @param chrono  the chronology to use, may be null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2232
     * @return the created formatter, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2233
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2234
    private DateTimeFormatter toFormatter(Locale locale, ResolverStyle resolverStyle, Chronology chrono) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2235
        Objects.requireNonNull(locale, "locale");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2236
        while (active.parent != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2237
            optionalEnd();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2238
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2239
        CompositePrinterParser pp = new CompositePrinterParser(printerParsers, false);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2240
        return new DateTimeFormatter(pp, locale, DecimalStyle.STANDARD,
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2241
                resolverStyle, null, chrono, null);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2242
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2243
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2244
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2245
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2246
     * Strategy for formatting/parsing date-time information.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2247
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2248
     * The printer may format any part, or the whole, of the input date-time object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2249
     * Typically, a complete format is constructed from a number of smaller
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2250
     * units, each outputting a single field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2251
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2252
     * The parser may parse any piece of text from the input, storing the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2253
     * in the context. Typically, each individual parser will just parse one
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2254
     * field, such as the day-of-month, storing the value in the context.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2255
     * Once the parse is complete, the caller will then resolve the parsed values
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2256
     * to create the desired object, such as a {@code LocalDate}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2257
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2258
     * The parse position will be updated during the parse. Parsing will start at
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2259
     * the specified index and the return value specifies the new parse position
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2260
     * for the next parser. If an error occurs, the returned index will be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2261
     * and will have the error position encoded using the complement operator.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2262
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2263
     * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2264
     * This interface must be implemented with care to ensure other classes operate correctly.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2265
     * All implementations that can be instantiated must be final, immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2266
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2267
     * The context is not a thread-safe object and a new instance will be created
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2268
     * for each format that occurs. The context must not be stored in an instance
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2269
     * variable or shared with any other threads.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2270
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2271
    interface DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2272
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2273
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2274
         * Prints the date-time object to the buffer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2275
         * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2276
         * The context holds information to use during the format.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2277
         * It also contains the date-time information to be printed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2278
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2279
         * The buffer must not be mutated beyond the content controlled by the implementation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2280
         *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2281
         * @param context  the context to format using, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2282
         * @param buf  the buffer to append to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2283
         * @return false if unable to query the value from the date-time, true otherwise
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2284
         * @throws DateTimeException if the date-time cannot be printed successfully
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2285
         */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2286
        boolean format(DateTimePrintContext context, StringBuilder buf);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2287
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2288
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2289
         * Parses text into date-time information.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2290
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2291
         * The context holds information to use during the parse.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2292
         * It is also used to store the parsed date-time information.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2293
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2294
         * @param context  the context to use and parse into, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2295
         * @param text  the input text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2296
         * @param position  the position to start parsing at, from 0 to the text length
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2297
         * @return the new parse position, where negative means an error with the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2298
         *  error position encoded using the complement ~ operator
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2299
         * @throws NullPointerException if the context or text is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2300
         * @throws IndexOutOfBoundsException if the position is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2301
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2302
        int parse(DateTimeParseContext context, CharSequence text, int position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2303
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2304
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2305
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2306
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2307
     * Composite printer and parser.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2308
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2309
    static final class CompositePrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2310
        private final DateTimePrinterParser[] printerParsers;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2311
        private final boolean optional;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2312
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2313
        CompositePrinterParser(List<DateTimePrinterParser> printerParsers, boolean optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2314
            this(printerParsers.toArray(new DateTimePrinterParser[printerParsers.size()]), optional);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2315
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2316
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2317
        CompositePrinterParser(DateTimePrinterParser[] printerParsers, boolean optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2318
            this.printerParsers = printerParsers;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2319
            this.optional = optional;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2320
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2321
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2322
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2323
         * Returns a copy of this printer-parser with the optional flag changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2324
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2325
         * @param optional  the optional flag to set in the copy
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2326
         * @return the new printer-parser, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2327
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2328
        public CompositePrinterParser withOptional(boolean optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2329
            if (optional == this.optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2330
                return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2331
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2332
            return new CompositePrinterParser(printerParsers, optional);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2333
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2334
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2335
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2336
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2337
            int length = buf.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2338
            if (optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2339
                context.startOptional();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2340
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2341
            try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2342
                for (DateTimePrinterParser pp : printerParsers) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2343
                    if (pp.format(context, buf) == false) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2344
                        buf.setLength(length);  // reset buffer
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2345
                        return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2346
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2347
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2348
            } finally {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2349
                if (optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2350
                    context.endOptional();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2351
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2352
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2353
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2354
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2355
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2356
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2357
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2358
            if (optional) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2359
                context.startOptional();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2360
                int pos = position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2361
                for (DateTimePrinterParser pp : printerParsers) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2362
                    pos = pp.parse(context, text, pos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2363
                    if (pos < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2364
                        context.endOptional(false);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2365
                        return position;  // return original position
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2366
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2367
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2368
                context.endOptional(true);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2369
                return pos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2370
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2371
                for (DateTimePrinterParser pp : printerParsers) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2372
                    position = pp.parse(context, text, position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2373
                    if (position < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2374
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2375
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2376
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2377
                return position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2378
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2379
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2380
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2381
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2382
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2383
            StringBuilder buf = new StringBuilder();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2384
            if (printerParsers != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2385
                buf.append(optional ? "[" : "(");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2386
                for (DateTimePrinterParser pp : printerParsers) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2387
                    buf.append(pp);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2388
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2389
                buf.append(optional ? "]" : ")");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2390
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2391
            return buf.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2392
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2393
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2394
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2395
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2396
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2397
     * Pads the output to a fixed width.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2398
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2399
    static final class PadPrinterParserDecorator implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2400
        private final DateTimePrinterParser printerParser;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2401
        private final int padWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2402
        private final char padChar;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2403
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2404
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2405
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2406
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2407
         * @param printerParser  the printer, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2408
         * @param padWidth  the width to pad to, 1 or greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2409
         * @param padChar  the pad character
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2410
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2411
        PadPrinterParserDecorator(DateTimePrinterParser printerParser, int padWidth, char padChar) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2412
            // input checked by DateTimeFormatterBuilder
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2413
            this.printerParser = printerParser;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2414
            this.padWidth = padWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2415
            this.padChar = padChar;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2416
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2417
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2418
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2419
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2420
            int preLen = buf.length();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2421
            if (printerParser.format(context, buf) == false) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2422
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2423
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2424
            int len = buf.length() - preLen;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2425
            if (len > padWidth) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2426
                throw new DateTimeException(
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2427
                    "Cannot print as output of " + len + " characters exceeds pad width of " + padWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2428
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2429
            for (int i = 0; i < padWidth - len; i++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2430
                buf.insert(preLen, padChar);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2431
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2432
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2433
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2434
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2435
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2436
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2437
            // cache context before changed by decorated parser
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2438
            final boolean strict = context.isStrict();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2439
            // parse
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2440
            if (position > text.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2441
                throw new IndexOutOfBoundsException();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2442
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2443
            if (position == text.length()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2444
                return ~position;  // no more characters in the string
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2445
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2446
            int endPos = position + padWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2447
            if (endPos > text.length()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2448
                if (strict) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2449
                    return ~position;  // not enough characters in the string to meet the parse width
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2450
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2451
                endPos = text.length();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2452
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2453
            int pos = position;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2454
            while (pos < endPos && context.charEquals(text.charAt(pos), padChar)) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2455
                pos++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2456
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2457
            text = text.subSequence(0, endPos);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2458
            int resultPos = printerParser.parse(context, text, pos);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2459
            if (resultPos != endPos && strict) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2460
                return ~(position + pos);  // parse of decorated field didn't parse to the end
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2461
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2462
            return resultPos;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2463
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2464
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2465
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2466
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2467
            return "Pad(" + printerParser + "," + padWidth + (padChar == ' ' ? ")" : ",'" + padChar + "')");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2468
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2469
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2470
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2471
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2472
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2473
     * Enumeration to apply simple parse settings.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2474
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2475
    static enum SettingsParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2476
        SENSITIVE,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2477
        INSENSITIVE,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2478
        STRICT,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2479
        LENIENT;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2480
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2481
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2482
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2483
            return true;  // nothing to do here
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2484
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2485
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2486
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2487
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2488
            // using ordinals to avoid javac synthetic inner class
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2489
            switch (ordinal()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2490
                case 0: context.setCaseSensitive(true); break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2491
                case 1: context.setCaseSensitive(false); break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2492
                case 2: context.setStrict(true); break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2493
                case 3: context.setStrict(false); break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2494
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2495
            return position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2496
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2497
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2498
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2499
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2500
            // using ordinals to avoid javac synthetic inner class
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2501
            switch (ordinal()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2502
                case 0: return "ParseCaseSensitive(true)";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2503
                case 1: return "ParseCaseSensitive(false)";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2504
                case 2: return "ParseStrict(true)";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2505
                case 3: return "ParseStrict(false)";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2506
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2507
            throw new IllegalStateException("Unreachable");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2508
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2509
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2510
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2511
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2512
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2513
     * Defaults a value into the parse if not currently present.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2514
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2515
    static class DefaultValueParser implements DateTimePrinterParser {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2516
        private final TemporalField field;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2517
        private final long value;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2518
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2519
        DefaultValueParser(TemporalField field, long value) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2520
            this.field = field;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2521
            this.value = value;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2522
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2523
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2524
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2525
            return true;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2526
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2527
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2528
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2529
            if (context.getParsed(field) == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2530
                context.setParsedField(field, value, position, position);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2531
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2532
            return position;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2533
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2534
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2535
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2536
    //-----------------------------------------------------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2537
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2538
     * Prints or parses a character literal.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2539
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2540
    static final class CharLiteralPrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2541
        private final char literal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2542
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2543
        CharLiteralPrinterParser(char literal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2544
            this.literal = literal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2545
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2546
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2547
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2548
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2549
            buf.append(literal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2550
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2551
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2552
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2553
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2554
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2555
            int length = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2556
            if (position == length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2557
                return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2558
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2559
            char ch = text.charAt(position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2560
            if (ch != literal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2561
                if (context.isCaseSensitive() ||
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2562
                        (Character.toUpperCase(ch) != Character.toUpperCase(literal) &&
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2563
                         Character.toLowerCase(ch) != Character.toLowerCase(literal))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2564
                    return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2565
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2566
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2567
            return position + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2568
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2569
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2570
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2571
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2572
            if (literal == '\'') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2573
                return "''";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2574
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2575
            return "'" + literal + "'";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2576
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2577
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2578
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2579
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2580
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2581
     * Prints or parses a string literal.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2582
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2583
    static final class StringLiteralPrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2584
        private final String literal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2585
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2586
        StringLiteralPrinterParser(String literal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2587
            this.literal = literal;  // validated by caller
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2588
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2589
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2590
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2591
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2592
            buf.append(literal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2593
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2594
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2595
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2596
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2597
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2598
            int length = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2599
            if (position > length || position < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2600
                throw new IndexOutOfBoundsException();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2601
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2602
            if (context.subSequenceEquals(text, position, literal, 0, literal.length()) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2603
                return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2604
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2605
            return position + literal.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2606
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2607
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2608
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2609
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2610
            String converted = literal.replace("'", "''");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2611
            return "'" + converted + "'";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2612
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2613
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2614
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2615
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2616
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2617
     * Prints and parses a numeric date-time field with optional padding.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2618
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2619
    static class NumberPrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2620
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2621
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2622
         * Array of 10 to the power of n.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2623
         */
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2624
        static final long[] EXCEED_POINTS = new long[] {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2625
            0L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2626
            10L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2627
            100L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2628
            1000L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2629
            10000L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2630
            100000L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2631
            1000000L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2632
            10000000L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2633
            100000000L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2634
            1000000000L,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2635
            10000000000L,
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2636
        };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2637
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2638
        final TemporalField field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2639
        final int minWidth;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2640
        final int maxWidth;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2641
        private final SignStyle signStyle;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2642
        final int subsequentWidth;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2643
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2644
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2645
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2646
         *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2647
         * @param field  the field to format, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2648
         * @param minWidth  the minimum field width, from 1 to 19
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2649
         * @param maxWidth  the maximum field width, from minWidth to 19
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2650
         * @param signStyle  the positive/negative sign style, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2651
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2652
        NumberPrinterParser(TemporalField field, int minWidth, int maxWidth, SignStyle signStyle) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2653
            // validated by caller
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2654
            this.field = field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2655
            this.minWidth = minWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2656
            this.maxWidth = maxWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2657
            this.signStyle = signStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2658
            this.subsequentWidth = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2659
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2660
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2661
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2662
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2663
         *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2664
         * @param field  the field to format, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2665
         * @param minWidth  the minimum field width, from 1 to 19
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2666
         * @param maxWidth  the maximum field width, from minWidth to 19
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2667
         * @param signStyle  the positive/negative sign style, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2668
         * @param subsequentWidth  the width of subsequent non-negative numbers, 0 or greater,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2669
         *  -1 if fixed width due to active adjacent parsing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2670
         */
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2671
        protected NumberPrinterParser(TemporalField field, int minWidth, int maxWidth, SignStyle signStyle, int subsequentWidth) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2672
            // validated by caller
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2673
            this.field = field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2674
            this.minWidth = minWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2675
            this.maxWidth = maxWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2676
            this.signStyle = signStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2677
            this.subsequentWidth = subsequentWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2678
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2679
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2680
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2681
         * Returns a new instance with fixed width flag set.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2682
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2683
         * @return a new updated printer-parser, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2684
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2685
        NumberPrinterParser withFixedWidth() {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2686
            if (subsequentWidth == -1) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2687
                return this;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2688
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2689
            return new NumberPrinterParser(field, minWidth, maxWidth, signStyle, -1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2690
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2691
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2692
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2693
         * Returns a new instance with an updated subsequent width.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2694
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2695
         * @param subsequentWidth  the width of subsequent non-negative numbers, 0 or greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2696
         * @return a new updated printer-parser, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2697
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2698
        NumberPrinterParser withSubsequentWidth(int subsequentWidth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2699
            return new NumberPrinterParser(field, minWidth, maxWidth, signStyle, this.subsequentWidth + subsequentWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2700
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2701
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2702
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2703
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2704
            Long valueLong = context.getValue(field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2705
            if (valueLong == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2706
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2707
            }
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2708
            long value = getValue(context, valueLong);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2709
            DecimalStyle decimalStyle = context.getDecimalStyle();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2710
            String str = (value == Long.MIN_VALUE ? "9223372036854775808" : Long.toString(Math.abs(value)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2711
            if (str.length() > maxWidth) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2712
                throw new DateTimeException("Field " + field +
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2713
                    " cannot be printed as the value " + value +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2714
                    " exceeds the maximum print width of " + maxWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2715
            }
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2716
            str = decimalStyle.convertNumberToI18N(str);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2717
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2718
            if (value >= 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2719
                switch (signStyle) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2720
                    case EXCEEDS_PAD:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2721
                        if (minWidth < 19 && value >= EXCEED_POINTS[minWidth]) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2722
                            buf.append(decimalStyle.getPositiveSign());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2723
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2724
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2725
                    case ALWAYS:
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2726
                        buf.append(decimalStyle.getPositiveSign());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2727
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2728
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2729
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2730
                switch (signStyle) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2731
                    case NORMAL:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2732
                    case EXCEEDS_PAD:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2733
                    case ALWAYS:
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2734
                        buf.append(decimalStyle.getNegativeSign());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2735
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2736
                    case NOT_NEGATIVE:
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2737
                        throw new DateTimeException("Field " + field +
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2738
                            " cannot be printed as the value " + value +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2739
                            " cannot be negative according to the SignStyle");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2740
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2741
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2742
            for (int i = 0; i < minWidth - str.length(); i++) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2743
                buf.append(decimalStyle.getZeroDigit());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2744
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2745
            buf.append(str);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2746
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2747
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2748
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2749
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2750
         * Gets the value to output.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2751
         *
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2752
         * @param context  the context
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2753
         * @param value  the value of the field, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2754
         * @return the value
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2755
         */
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2756
        long getValue(DateTimePrintContext context, long value) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2757
            return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2758
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2759
23595
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2760
        /**
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2761
         * For NumberPrinterParser, the width is fixed depending on the
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2762
         * minWidth, maxWidth, signStyle and whether subsequent fields are fixed.
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2763
         * @param context the context
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2764
         * @return true if the field is fixed width
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2765
         * @see DateTimeFormatterBuilder#appendValue(java.time.temporal.TemporalField, int)
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2766
         */
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2767
        boolean isFixedWidth(DateTimeParseContext context) {
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2768
            return subsequentWidth == -1 ||
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2769
                (subsequentWidth > 0 && minWidth == maxWidth && signStyle == SignStyle.NOT_NEGATIVE);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2770
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2771
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2772
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2773
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2774
            int length = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2775
            if (position == length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2776
                return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2777
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2778
            char sign = text.charAt(position);  // IOOBE if invalid position
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2779
            boolean negative = false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2780
            boolean positive = false;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2781
            if (sign == context.getDecimalStyle().getPositiveSign()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2782
                if (signStyle.parse(true, context.isStrict(), minWidth == maxWidth) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2783
                    return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2784
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2785
                positive = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2786
                position++;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2787
            } else if (sign == context.getDecimalStyle().getNegativeSign()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2788
                if (signStyle.parse(false, context.isStrict(), minWidth == maxWidth) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2789
                    return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2790
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2791
                negative = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2792
                position++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2793
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2794
                if (signStyle == SignStyle.ALWAYS && context.isStrict()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2795
                    return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2796
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2797
            }
23595
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2798
            int effMinWidth = (context.isStrict() || isFixedWidth(context) ? minWidth : 1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2799
            int minEndPos = position + effMinWidth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2800
            if (minEndPos > length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2801
                return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2802
            }
23595
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  2803
            int effMaxWidth = (context.isStrict() || isFixedWidth(context) ? maxWidth : 9) + Math.max(subsequentWidth, 0);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2804
            long total = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2805
            BigInteger totalBig = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2806
            int pos = position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2807
            for (int pass = 0; pass < 2; pass++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2808
                int maxEndPos = Math.min(pos + effMaxWidth, length);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2809
                while (pos < maxEndPos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2810
                    char ch = text.charAt(pos++);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2811
                    int digit = context.getDecimalStyle().convertToDigit(ch);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2812
                    if (digit < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2813
                        pos--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2814
                        if (pos < minEndPos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2815
                            return ~position;  // need at least min width digits
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2816
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2817
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2818
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2819
                    if ((pos - position) > 18) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2820
                        if (totalBig == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2821
                            totalBig = BigInteger.valueOf(total);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2822
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2823
                        totalBig = totalBig.multiply(BigInteger.TEN).add(BigInteger.valueOf(digit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2824
                    } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2825
                        total = total * 10 + digit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2826
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2827
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2828
                if (subsequentWidth > 0 && pass == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2829
                    // re-parse now we know the correct width
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2830
                    int parseLen = pos - position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2831
                    effMaxWidth = Math.max(effMinWidth, parseLen - subsequentWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2832
                    pos = position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2833
                    total = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2834
                    totalBig = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2835
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2836
                    break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2837
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2838
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2839
            if (negative) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2840
                if (totalBig != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2841
                    if (totalBig.equals(BigInteger.ZERO) && context.isStrict()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2842
                        return ~(position - 1);  // minus zero not allowed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2843
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2844
                    totalBig = totalBig.negate();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2845
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2846
                    if (total == 0 && context.isStrict()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2847
                        return ~(position - 1);  // minus zero not allowed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2848
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2849
                    total = -total;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2850
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2851
            } else if (signStyle == SignStyle.EXCEEDS_PAD && context.isStrict()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2852
                int parseLen = pos - position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2853
                if (positive) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2854
                    if (parseLen <= minWidth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2855
                        return ~(position - 1);  // '+' only parsed if minWidth exceeded
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2856
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2857
                } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2858
                    if (parseLen > minWidth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2859
                        return ~position;  // '+' must be parsed if minWidth exceeded
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2860
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2861
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2862
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2863
            if (totalBig != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2864
                if (totalBig.bitLength() > 63) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2865
                    // overflow, parse 1 less digit
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2866
                    totalBig = totalBig.divide(BigInteger.TEN);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2867
                    pos--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2868
                }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2869
                return setValue(context, totalBig.longValue(), position, pos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2870
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2871
            return setValue(context, total, position, pos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2872
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2873
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2874
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2875
         * Stores the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2876
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2877
         * @param context  the context to store into, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2878
         * @param value  the value
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2879
         * @param errorPos  the position of the field being parsed
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2880
         * @param successPos  the position after the field being parsed
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2881
         * @return the new position
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2882
         */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2883
        int setValue(DateTimeParseContext context, long value, int errorPos, int successPos) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  2884
            return context.setParsedField(field, value, errorPos, successPos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2885
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2886
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2887
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2888
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2889
            if (minWidth == 1 && maxWidth == 19 && signStyle == SignStyle.NORMAL) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2890
                return "Value(" + field + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2891
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2892
            if (minWidth == maxWidth && signStyle == SignStyle.NOT_NEGATIVE) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2893
                return "Value(" + field + "," + minWidth + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2894
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  2895
            return "Value(" + field + "," + minWidth + "," + maxWidth + "," + signStyle + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2896
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2897
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2898
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2899
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2900
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2901
     * Prints and parses a reduced numeric date-time field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2902
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2903
    static final class ReducedPrinterParser extends NumberPrinterParser {
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2904
        /**
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2905
         * The base date for reduced value parsing.
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2906
         */
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2907
        static final LocalDate BASE_DATE = LocalDate.of(2000, 1, 1);
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2908
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2909
        private final int baseValue;
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2910
        private final ChronoLocalDate baseDate;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2911
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2912
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2913
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2914
         *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2915
         * @param field  the field to format, validated not null
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2916
         * @param minWidth  the minimum field width, from 1 to 10
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2917
         * @param maxWidth  the maximum field width, from 1 to 10
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2918
         * @param baseValue  the base value
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2919
         * @param baseDate  the base date
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2920
         */
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2921
        ReducedPrinterParser(TemporalField field, int minWidth, int maxWidth,
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2922
                int baseValue, ChronoLocalDate baseDate) {
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2923
            this(field, minWidth, maxWidth, baseValue, baseDate, 0);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2924
            if (minWidth < 1 || minWidth > 10) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2925
                throw new IllegalArgumentException("The minWidth must be from 1 to 10 inclusive but was " + minWidth);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2926
            }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2927
            if (maxWidth < 1 || maxWidth > 10) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2928
                throw new IllegalArgumentException("The maxWidth must be from 1 to 10 inclusive but was " + minWidth);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2929
            }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2930
            if (maxWidth < minWidth) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2931
                throw new IllegalArgumentException("Maximum width must exceed or equal the minimum width but " +
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2932
                        maxWidth + " < " + minWidth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2933
            }
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2934
            if (baseDate == null) {
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2935
                if (field.range().isValidValue(baseValue) == false) {
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2936
                    throw new IllegalArgumentException("The base value must be within the range of the field");
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2937
                }
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2938
                if ((((long) baseValue) + EXCEED_POINTS[maxWidth]) > Integer.MAX_VALUE) {
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2939
                    throw new DateTimeException("Unable to add printer-parser as the range exceeds the capacity of an int");
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2940
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2941
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2942
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2943
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2944
        /**
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2945
         * Constructor.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2946
         * The arguments have already been checked.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2947
         *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2948
         * @param field  the field to format, validated not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2949
         * @param minWidth  the minimum field width, from 1 to 10
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2950
         * @param maxWidth  the maximum field width, from 1 to 10
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2951
         * @param baseValue  the base value
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2952
         * @param baseDate  the base date
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2953
         * @param subsequentWidth the subsequentWidth for this instance
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2954
         */
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2955
        private ReducedPrinterParser(TemporalField field, int minWidth, int maxWidth,
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2956
                int baseValue, ChronoLocalDate baseDate, int subsequentWidth) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2957
            super(field, minWidth, maxWidth, SignStyle.NOT_NEGATIVE, subsequentWidth);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2958
            this.baseValue = baseValue;
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2959
            this.baseDate = baseDate;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2960
        }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2961
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2962
        @Override
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2963
        long getValue(DateTimePrintContext context, long value) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2964
            long absValue = Math.abs(value);
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2965
            int baseValue = this.baseValue;
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2966
            if (baseDate != null) {
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2967
                Chronology chrono = Chronology.from(context.getTemporal());
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2968
                baseValue = chrono.date(baseDate).get(field);
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2969
            }
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2970
            if (value >= baseValue && value < baseValue + EXCEED_POINTS[minWidth]) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2971
                // Use the reduced value if it fits in minWidth
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2972
                return absValue % EXCEED_POINTS[minWidth];
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2973
            }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2974
            // Otherwise truncate to fit in maxWidth
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2975
            return absValue % EXCEED_POINTS[maxWidth];
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2976
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2977
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  2978
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  2979
        int setValue(DateTimeParseContext context, long value, int errorPos, int successPos) {
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2980
            int baseValue = this.baseValue;
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2981
            if (baseDate != null) {
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2982
                Chronology chrono = context.getEffectiveChronology();
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2983
                baseValue = chrono.date(baseDate).get(field);
21296
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2984
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2985
                // In case the Chronology is changed later, add a callback when/if it changes
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2986
                final long initialValue = value;
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2987
                context.addChronoChangedListener(
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2988
                        (_unused) ->  {
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2989
                            /* Repeat the set of the field using the current Chronology
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2990
                             * The success/error position is ignored because the value is
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2991
                             * intentionally being overwritten.
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2992
                             */
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2993
                            setValue(context, initialValue, errorPos, successPos);
de1c1faa6f77 8025828: Late binding of Chronology to appendValueReduced
rriggs
parents: 20873
diff changeset
  2994
                        });
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  2995
            }
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2996
            int parseLen = successPos - errorPos;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2997
            if (parseLen == minWidth && value >= 0) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2998
                long range = EXCEED_POINTS[minWidth];
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  2999
                long lastPart = baseValue % range;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3000
                long basePart = baseValue - lastPart;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3001
                if (baseValue > 0) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3002
                    value = basePart + value;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3003
                } else {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3004
                    value = basePart - value;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3005
                }
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  3006
                if (value < baseValue) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3007
                    value += range;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3008
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3009
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3010
            return context.setParsedField(field, value, errorPos, successPos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3011
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3012
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3013
        /**
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3014
         * Returns a new instance with fixed width flag set.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3015
         *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3016
         * @return a new updated printer-parser, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3017
         */
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3018
        @Override
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3019
        ReducedPrinterParser withFixedWidth() {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3020
            if (subsequentWidth == -1) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3021
                return this;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3022
            }
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  3023
            return new ReducedPrinterParser(field, minWidth, maxWidth, baseValue, baseDate, -1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3024
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3025
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3026
        /**
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3027
         * Returns a new instance with an updated subsequent width.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3028
         *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3029
         * @param subsequentWidth  the width of subsequent non-negative numbers, 0 or greater
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3030
         * @return a new updated printer-parser, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3031
         */
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3032
        @Override
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3033
        ReducedPrinterParser withSubsequentWidth(int subsequentWidth) {
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  3034
            return new ReducedPrinterParser(field, minWidth, maxWidth, baseValue, baseDate,
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3035
                    this.subsequentWidth + subsequentWidth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3036
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3037
23595
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3038
        /**
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3039
         * For a ReducedPrinterParser, fixed width is false if the mode is strict,
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3040
         * otherwise it is set as for NumberPrinterParser.
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3041
         * @param context the context
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3042
         * @return if the field is fixed width
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3043
         * @see DateTimeFormatterBuilder#appendValueReduced(java.time.temporal.TemporalField, int, int, int)
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3044
         */
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3045
        @Override
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3046
        boolean isFixedWidth(DateTimeParseContext context) {
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3047
           if (context.isStrict() == false) {
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3048
               return false;
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3049
           }
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3050
           return super.isFixedWidth(context);
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3051
        }
08ca64c3f0e4 8032491: DateTimeFormatter fixed width adjacent value parsing does not match spec
rriggs
parents: 21296
diff changeset
  3052
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3053
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3054
        public String toString() {
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33671
diff changeset
  3055
            return "ReducedValue(" + field + "," + minWidth + "," + maxWidth +
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33671
diff changeset
  3056
                    "," + Objects.requireNonNullElse(baseDate, baseValue) + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3057
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3058
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3059
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3060
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3061
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3062
     * Prints and parses a numeric date-time field with optional padding.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3063
     */
37610
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3064
    static final class FractionPrinterParser extends NumberPrinterParser {
50473
bef0ed237504 8204444: java.time cleanup
martin
parents: 49983
diff changeset
  3065
        private final boolean decimalPoint;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3066
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3067
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3068
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3069
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3070
         * @param field  the field to output, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3071
         * @param minWidth  the minimum width to output, from 0 to 9
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3072
         * @param maxWidth  the maximum width to output, from 0 to 9
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3073
         * @param decimalPoint  whether to output the localized decimal point symbol
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3074
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3075
        FractionPrinterParser(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) {
37610
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3076
            this(field, minWidth, maxWidth, decimalPoint, 0);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3077
            Objects.requireNonNull(field, "field");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3078
            if (field.range().isFixed() == false) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  3079
                throw new IllegalArgumentException("Field must have a fixed set of values: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3080
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3081
            if (minWidth < 0 || minWidth > 9) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3082
                throw new IllegalArgumentException("Minimum width must be from 0 to 9 inclusive but was " + minWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3083
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3084
            if (maxWidth < 1 || maxWidth > 9) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3085
                throw new IllegalArgumentException("Maximum width must be from 1 to 9 inclusive but was " + maxWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3086
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3087
            if (maxWidth < minWidth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3088
                throw new IllegalArgumentException("Maximum width must exceed or equal the minimum width but " +
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3089
                        maxWidth + " < " + minWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3090
            }
37610
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3091
        }
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3092
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3093
        /**
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3094
         * Constructor.
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3095
         *
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3096
         * @param field  the field to output, not null
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3097
         * @param minWidth  the minimum width to output, from 0 to 9
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3098
         * @param maxWidth  the maximum width to output, from 0 to 9
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3099
         * @param decimalPoint  whether to output the localized decimal point symbol
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3100
         * @param subsequentWidth the subsequentWidth for this instance
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3101
         */
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3102
        FractionPrinterParser(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint, int subsequentWidth) {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3103
            super(field, minWidth, maxWidth, SignStyle.NOT_NEGATIVE, subsequentWidth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3104
            this.decimalPoint = decimalPoint;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3105
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3106
37610
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3107
        /**
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3108
         * Returns a new instance with fixed width flag set.
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3109
         *
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3110
         * @return a new updated printer-parser, not null
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3111
         */
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3112
        @Override
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3113
        FractionPrinterParser withFixedWidth() {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3114
            if (subsequentWidth == -1) {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3115
                return this;
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3116
            }
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3117
            return new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint, -1);
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3118
        }
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3119
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3120
        /**
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3121
         * Returns a new instance with an updated subsequent width.
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3122
         *
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3123
         * @param subsequentWidth  the width of subsequent non-negative numbers, 0 or greater
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3124
         * @return a new updated printer-parser, not null
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3125
         */
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3126
        @Override
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3127
        FractionPrinterParser withSubsequentWidth(int subsequentWidth) {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3128
            return new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint, this.subsequentWidth + subsequentWidth);
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3129
        }
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3130
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3131
        /**
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3132
         * For FractionPrinterPrinterParser, the width is fixed if context is sttrict,
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3133
         * minWidth equal to maxWidth and decimalpoint is absent.
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3134
         * @param context the context
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3135
         * @return if the field is fixed width
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3136
         * @see DateTimeFormatterBuilder#appendValueFraction(java.time.temporal.TemporalField, int, int, boolean)
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3137
         */
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3138
        @Override
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3139
        boolean isFixedWidth(DateTimeParseContext context) {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3140
            if (context.isStrict() && minWidth == maxWidth && decimalPoint == false) {
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3141
                return true;
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3142
            }
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3143
            return false;
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3144
        }
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3145
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3146
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3147
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3148
            Long value = context.getValue(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3149
            if (value == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3150
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3151
            }
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3152
            DecimalStyle decimalStyle = context.getDecimalStyle();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3153
            BigDecimal fraction = convertToFraction(value);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3154
            if (fraction.scale() == 0) {  // scale is zero if value is zero
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3155
                if (minWidth > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3156
                    if (decimalPoint) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3157
                        buf.append(decimalStyle.getDecimalSeparator());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3158
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3159
                    for (int i = 0; i < minWidth; i++) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3160
                        buf.append(decimalStyle.getZeroDigit());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3161
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3162
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3163
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3164
                int outputScale = Math.min(Math.max(fraction.scale(), minWidth), maxWidth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3165
                fraction = fraction.setScale(outputScale, RoundingMode.FLOOR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3166
                String str = fraction.toPlainString().substring(2);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3167
                str = decimalStyle.convertNumberToI18N(str);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3168
                if (decimalPoint) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3169
                    buf.append(decimalStyle.getDecimalSeparator());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3170
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3171
                buf.append(str);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3172
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3173
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3174
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3175
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3176
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3177
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
37610
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3178
            int effectiveMin = (context.isStrict() || isFixedWidth(context) ? minWidth : 0);
12f7514dccc4 8031085: DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"
ntv
parents: 37599
diff changeset
  3179
            int effectiveMax = (context.isStrict() || isFixedWidth(context) ? maxWidth : 9);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3180
            int length = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3181
            if (position == length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3182
                // valid if whole field is optional, invalid if minimum width
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3183
                return (effectiveMin > 0 ? ~position : position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3184
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3185
            if (decimalPoint) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3186
                if (text.charAt(position) != context.getDecimalStyle().getDecimalSeparator()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3187
                    // valid if whole field is optional, invalid if minimum width
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3188
                    return (effectiveMin > 0 ? ~position : position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3189
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3190
                position++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3191
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3192
            int minEndPos = position + effectiveMin;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3193
            if (minEndPos > length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3194
                return ~position;  // need at least min width digits
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3195
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3196
            int maxEndPos = Math.min(position + effectiveMax, length);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3197
            int total = 0;  // can use int because we are only parsing up to 9 digits
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3198
            int pos = position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3199
            while (pos < maxEndPos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3200
                char ch = text.charAt(pos++);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3201
                int digit = context.getDecimalStyle().convertToDigit(ch);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3202
                if (digit < 0) {
58086
d52f77f0acb5 8230136: DateTimeFormatterBuilder.FractionPrinterParser#parse fails to verify minWidth
naoto
parents: 57478
diff changeset
  3203
                    if (pos <= minEndPos) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3204
                        return ~position;  // need at least min width digits
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3205
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3206
                    pos--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3207
                    break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3208
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3209
                total = total * 10 + digit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3210
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3211
            BigDecimal fraction = new BigDecimal(total).movePointLeft(pos - position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3212
            long value = convertFromFraction(fraction);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3213
            return context.setParsedField(field, value, position, pos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3214
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3215
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3216
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3217
         * Converts a value for this field to a fraction between 0 and 1.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3218
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3219
         * The fractional value is between 0 (inclusive) and 1 (exclusive).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3220
         * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3221
         * The fraction is obtained by calculation from the field range using 9 decimal
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3222
         * places and a rounding mode of {@link RoundingMode#FLOOR FLOOR}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3223
         * The calculation is inaccurate if the values do not run continuously from smallest to largest.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3224
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3225
         * For example, the second-of-minute value of 15 would be returned as 0.25,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3226
         * assuming the standard definition of 60 seconds in a minute.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3227
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3228
         * @param value  the value to convert, must be valid for this rule
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3229
         * @return the value as a fraction within the range, from 0 to 1, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3230
         * @throws DateTimeException if the value cannot be converted to a fraction
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3231
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3232
        private BigDecimal convertToFraction(long value) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3233
            ValueRange range = field.range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3234
            range.checkValidValue(value, field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3235
            BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3236
            BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3237
            BigDecimal valueBD = BigDecimal.valueOf(value).subtract(minBD);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3238
            BigDecimal fraction = valueBD.divide(rangeBD, 9, RoundingMode.FLOOR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3239
            // stripTrailingZeros bug
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3240
            return fraction.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : fraction.stripTrailingZeros();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3241
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3242
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3243
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3244
         * Converts a fraction from 0 to 1 for this field to a value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3245
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3246
         * The fractional value must be between 0 (inclusive) and 1 (exclusive).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3247
         * It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3248
         * The value is obtained by calculation from the field range and a rounding
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3249
         * mode of {@link RoundingMode#FLOOR FLOOR}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3250
         * The calculation is inaccurate if the values do not run continuously from smallest to largest.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3251
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3252
         * For example, the fractional second-of-minute of 0.25 would be converted to 15,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3253
         * assuming the standard definition of 60 seconds in a minute.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3254
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3255
         * @param fraction  the fraction to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3256
         * @return the value of the field, valid for this rule
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3257
         * @throws DateTimeException if the value cannot be converted
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3258
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3259
        private long convertFromFraction(BigDecimal fraction) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3260
            ValueRange range = field.range();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3261
            BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3262
            BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3263
            BigDecimal valueBD = fraction.multiply(rangeBD).setScale(0, RoundingMode.FLOOR).add(minBD);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3264
            return valueBD.longValueExact();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3265
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3266
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3267
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3268
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3269
            String decimal = (decimalPoint ? ",DecimalPoint" : "");
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  3270
            return "Fraction(" + field + "," + minWidth + "," + maxWidth + decimal + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3271
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3272
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3273
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3274
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3275
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3276
     * Prints or parses field text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3277
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3278
    static final class TextPrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3279
        private final TemporalField field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3280
        private final TextStyle textStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3281
        private final DateTimeTextProvider provider;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3282
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3283
         * The cached number printer parser.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3284
         * Immutable and volatile, so no synchronization needed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3285
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3286
        private volatile NumberPrinterParser numberPrinterParser;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3287
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3288
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3289
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3290
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3291
         * @param field  the field to output, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3292
         * @param textStyle  the text style, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3293
         * @param provider  the text provider, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3294
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3295
        TextPrinterParser(TemporalField field, TextStyle textStyle, DateTimeTextProvider provider) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3296
            // validated by caller
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3297
            this.field = field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3298
            this.textStyle = textStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3299
            this.provider = provider;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3300
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3301
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3302
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3303
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3304
            Long value = context.getValue(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3305
            if (value == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3306
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3307
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3308
            String text;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  3309
            Chronology chrono = context.getTemporal().query(TemporalQueries.chronology());
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3310
            if (chrono == null || chrono == IsoChronology.INSTANCE) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3311
                text = provider.getText(field, value, textStyle, context.getLocale());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3312
            } else {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3313
                text = provider.getText(chrono, field, value, textStyle, context.getLocale());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3314
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3315
            if (text == null) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3316
                return numberPrinterParser().format(context, buf);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3317
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3318
            buf.append(text);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3319
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3320
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3321
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3322
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3323
        public int parse(DateTimeParseContext context, CharSequence parseText, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3324
            int length = parseText.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3325
            if (position < 0 || position > length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3326
                throw new IndexOutOfBoundsException();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3327
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3328
            TextStyle style = (context.isStrict() ? textStyle : null);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3329
            Chronology chrono = context.getEffectiveChronology();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3330
            Iterator<Entry<String, Long>> it;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3331
            if (chrono == null || chrono == IsoChronology.INSTANCE) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3332
                it = provider.getTextIterator(field, style, context.getLocale());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3333
            } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3334
                it = provider.getTextIterator(chrono, field, style, context.getLocale());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3335
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3336
            if (it != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3337
                while (it.hasNext()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3338
                    Entry<String, Long> entry = it.next();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3339
                    String itText = entry.getKey();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3340
                    if (context.subSequenceEquals(itText, 0, parseText, position, itText.length())) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3341
                        return context.setParsedField(field, entry.getValue(), position, position + itText.length());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3342
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3343
                }
29877
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3344
                if (field == ERA && !context.isStrict()) {
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3345
                    // parse the possible era name from era.toString()
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3346
                    List<Era> eras = chrono.eras();
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3347
                    for (Era era : eras) {
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3348
                        String name = era.toString();
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3349
                        if (context.subSequenceEquals(name, 0, parseText, position, name.length())) {
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3350
                            return context.setParsedField(field, era.getValue(), position, position + name.length());
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3351
                        }
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3352
                    }
b6b5d166d055 8077640: DateTimeFormatter does not parse/accept the era.toString() result from MinguoEra/ThaiBuddhistEra
sherman
parents: 28751
diff changeset
  3353
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3354
                if (context.isStrict()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3355
                    return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3356
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3357
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3358
            return numberPrinterParser().parse(context, parseText, position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3359
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3360
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3361
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3362
         * Create and cache a number printer parser.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3363
         * @return the number printer parser for this field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3364
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3365
        private NumberPrinterParser numberPrinterParser() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3366
            if (numberPrinterParser == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3367
                numberPrinterParser = new NumberPrinterParser(field, 1, 19, SignStyle.NORMAL);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3368
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3369
            return numberPrinterParser;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3370
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3371
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3372
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3373
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3374
            if (textStyle == TextStyle.FULL) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  3375
                return "Text(" + field + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3376
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  3377
            return "Text(" + field + "," + textStyle + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3378
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3379
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3380
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3381
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3382
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3383
     * Prints or parses an ISO-8601 instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3384
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3385
    static final class InstantPrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3386
        // days in a 400 year cycle = 146097
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3387
        // days in a 10,000 year cycle = 146097 * 25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3388
        // seconds per day = 86400
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3389
        private static final long SECONDS_PER_10000_YEARS = 146097L * 25L * 86400L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3390
        private static final long SECONDS_0000_TO_1970 = ((146097L * 5L) - (30L * 365L + 7L)) * 86400L;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3391
        private final int fractionalDigits;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3392
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3393
        InstantPrinterParser(int fractionalDigits) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3394
            this.fractionalDigits = fractionalDigits;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3395
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3396
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3397
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3398
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3399
            // use INSTANT_SECONDS, thus this code is not bound by Instant.MAX
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3400
            Long inSecs = context.getValue(INSTANT_SECONDS);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3401
            Long inNanos = null;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3402
            if (context.getTemporal().isSupported(NANO_OF_SECOND)) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3403
                inNanos = context.getTemporal().getLong(NANO_OF_SECOND);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3404
            }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3405
            if (inSecs == null) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3406
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3407
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3408
            long inSec = inSecs;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3409
            int inNano = NANO_OF_SECOND.checkValidIntValue(inNanos != null ? inNanos : 0);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3410
            // format mostly using LocalDateTime.toString
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3411
            if (inSec >= -SECONDS_0000_TO_1970) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3412
                // current era
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3413
                long zeroSecs = inSec - SECONDS_PER_10000_YEARS + SECONDS_0000_TO_1970;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3414
                long hi = Math.floorDiv(zeroSecs, SECONDS_PER_10000_YEARS) + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3415
                long lo = Math.floorMod(zeroSecs, SECONDS_PER_10000_YEARS);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3416
                LocalDateTime ldt = LocalDateTime.ofEpochSecond(lo - SECONDS_0000_TO_1970, 0, ZoneOffset.UTC);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3417
                if (hi > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3418
                    buf.append('+').append(hi);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3419
                }
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3420
                buf.append(ldt);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3421
                if (ldt.getSecond() == 0) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3422
                    buf.append(":00");
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3423
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3424
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3425
                // before current era
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3426
                long zeroSecs = inSec + SECONDS_0000_TO_1970;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3427
                long hi = zeroSecs / SECONDS_PER_10000_YEARS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3428
                long lo = zeroSecs % SECONDS_PER_10000_YEARS;
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3429
                LocalDateTime ldt = LocalDateTime.ofEpochSecond(lo - SECONDS_0000_TO_1970, 0, ZoneOffset.UTC);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3430
                int pos = buf.length();
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3431
                buf.append(ldt);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3432
                if (ldt.getSecond() == 0) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3433
                    buf.append(":00");
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3434
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3435
                if (hi < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3436
                    if (ldt.getYear() == -10_000) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3437
                        buf.replace(pos, pos + 2, Long.toString(hi - 1));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3438
                    } else if (lo == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3439
                        buf.insert(pos, hi);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3440
                    } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3441
                        buf.insert(pos + 1, Math.abs(hi));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3442
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3443
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3444
            }
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3445
            // add fraction
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3446
            if ((fractionalDigits < 0 && inNano > 0) || fractionalDigits > 0) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3447
                buf.append('.');
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3448
                int div = 100_000_000;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3449
                for (int i = 0; ((fractionalDigits == -1 && inNano > 0) ||
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3450
                                    (fractionalDigits == -2 && (inNano > 0 || (i % 3) != 0)) ||
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3451
                                    i < fractionalDigits); i++) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3452
                    int digit = inNano / div;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3453
                    buf.append((char) (digit + '0'));
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3454
                    inNano = inNano - (digit * div);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3455
                    div = div / 10;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3456
                }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3457
            }
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3458
            buf.append('Z');
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3459
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3460
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3461
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3462
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3463
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3464
            // new context to avoid overwriting fields like year/month/day
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3465
            int minDigits = (fractionalDigits < 0 ? 0 : fractionalDigits);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3466
            int maxDigits = (fractionalDigits < 0 ? 9 : fractionalDigits);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3467
            CompositePrinterParser parser = new DateTimeFormatterBuilder()
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3468
                    .append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T')
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3469
                    .appendValue(HOUR_OF_DAY, 2).appendLiteral(':')
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3470
                    .appendValue(MINUTE_OF_HOUR, 2).appendLiteral(':')
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3471
                    .appendValue(SECOND_OF_MINUTE, 2)
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3472
                    .appendFraction(NANO_OF_SECOND, minDigits, maxDigits, true)
52012
6f58ecdb060a 8166138: DateTimeFormatter.ISO_INSTANT should handle offsets
nishjain
parents: 51874
diff changeset
  3473
                    .appendOffsetId()
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3474
                    .toFormatter().toPrinterParser(false);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3475
            DateTimeParseContext newContext = context.copy();
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3476
            int pos = parser.parse(newContext, text, position);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3477
            if (pos < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3478
                return pos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3479
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3480
            // parser restricts most fields to 2 digits, so definitely int
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3481
            // correctly parsed nano is also guaranteed to be valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3482
            long yearParsed = newContext.getParsed(YEAR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3483
            int month = newContext.getParsed(MONTH_OF_YEAR).intValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3484
            int day = newContext.getParsed(DAY_OF_MONTH).intValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3485
            int hour = newContext.getParsed(HOUR_OF_DAY).intValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3486
            int min = newContext.getParsed(MINUTE_OF_HOUR).intValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3487
            Long secVal = newContext.getParsed(SECOND_OF_MINUTE);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3488
            Long nanoVal = newContext.getParsed(NANO_OF_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3489
            int sec = (secVal != null ? secVal.intValue() : 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3490
            int nano = (nanoVal != null ? nanoVal.intValue() : 0);
52012
6f58ecdb060a 8166138: DateTimeFormatter.ISO_INSTANT should handle offsets
nishjain
parents: 51874
diff changeset
  3491
            int offset = newContext.getParsed(OFFSET_SECONDS).intValue();
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3492
            int days = 0;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3493
            if (hour == 24 && min == 0 && sec == 0 && nano == 0) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3494
                hour = 0;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3495
                days = 1;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3496
            } else if (hour == 23 && min == 59 && sec == 60) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3497
                context.setParsedLeapSecond();
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3498
                sec = 59;
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3499
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3500
            int year = (int) yearParsed % 10_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3501
            long instantSecs;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3502
            try {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  3503
                LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, min, sec, 0).plusDays(days);
52012
6f58ecdb060a 8166138: DateTimeFormatter.ISO_INSTANT should handle offsets
nishjain
parents: 51874
diff changeset
  3504
                instantSecs = ldt.toEpochSecond(ZoneOffset.ofTotalSeconds(offset));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3505
                instantSecs += Math.multiplyExact(yearParsed / 10_000L, SECONDS_PER_10000_YEARS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3506
            } catch (RuntimeException ex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3507
                return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3508
            }
24986
81e3c2e485d4 8046416: Unable to parse an Instant from fields
scolebourne
parents: 24256
diff changeset
  3509
            int successPos = pos;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3510
            successPos = context.setParsedField(INSTANT_SECONDS, instantSecs, position, successPos);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3511
            return context.setParsedField(NANO_OF_SECOND, nano, position, successPos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3512
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3513
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3514
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3515
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3516
            return "Instant()";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3517
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3518
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3519
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3520
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3521
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3522
     * Prints or parses an offset ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3523
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3524
    static final class OffsetIdPrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3525
        static final String[] PATTERNS = new String[] {
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3526
                "+HH", "+HHmm", "+HH:mm", "+HHMM", "+HH:MM", "+HHMMss", "+HH:MM:ss", "+HHMMSS", "+HH:MM:SS", "+HHmmss", "+HH:mm:ss",
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3527
                "+H",  "+Hmm",  "+H:mm",  "+HMM",  "+H:MM",  "+HMMss",  "+H:MM:ss",  "+HMMSS",  "+H:MM:SS",  "+Hmmss",  "+H:mm:ss",
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3528
        };  // order used in pattern builder
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3529
        static final OffsetIdPrinterParser INSTANCE_ID_Z = new OffsetIdPrinterParser("+HH:MM:ss", "Z");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3530
        static final OffsetIdPrinterParser INSTANCE_ID_ZERO = new OffsetIdPrinterParser("+HH:MM:ss", "0");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3531
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3532
        private final String noOffsetText;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3533
        private final int type;
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3534
        private final int style;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3535
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3536
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3537
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3538
         *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3539
         * @param pattern  the pattern
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3540
         * @param noOffsetText  the text to use for UTC, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3541
         */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3542
        OffsetIdPrinterParser(String pattern, String noOffsetText) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3543
            Objects.requireNonNull(pattern, "pattern");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3544
            Objects.requireNonNull(noOffsetText, "noOffsetText");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3545
            this.type = checkPattern(pattern);
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3546
            this.style = type % 11;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3547
            this.noOffsetText = noOffsetText;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3548
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3549
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3550
        private int checkPattern(String pattern) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3551
            for (int i = 0; i < PATTERNS.length; i++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3552
                if (PATTERNS[i].equals(pattern)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3553
                    return i;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3554
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3555
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3556
            throw new IllegalArgumentException("Invalid zone offset pattern: " + pattern);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3557
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3558
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3559
        private boolean isPaddedHour() {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3560
            return type < 11;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3561
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3562
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3563
        private boolean isColon() {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3564
            return style > 0 && (style % 2) == 0;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3565
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3566
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3567
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3568
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3569
            Long offsetSecs = context.getValue(OFFSET_SECONDS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3570
            if (offsetSecs == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3571
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3572
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3573
            int totalSecs = Math.toIntExact(offsetSecs);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3574
            if (totalSecs == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3575
                buf.append(noOffsetText);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3576
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3577
                int absHours = Math.abs((totalSecs / 3600) % 100);  // anything larger than 99 silently dropped
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3578
                int absMinutes = Math.abs((totalSecs / 60) % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3579
                int absSeconds = Math.abs(totalSecs % 60);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3580
                int bufPos = buf.length();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3581
                int output = absHours;
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3582
                buf.append(totalSecs < 0 ? "-" : "+");
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3583
                if (isPaddedHour() || absHours >= 10) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3584
                    formatZeroPad(false, absHours, buf);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3585
                } else {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3586
                    buf.append((char) (absHours + '0'));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3587
                }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3588
                if ((style >= 3 && style <= 8) || (style >= 9 && absSeconds > 0) || (style >= 1 && absMinutes > 0)) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3589
                    formatZeroPad(isColon(), absMinutes, buf);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3590
                    output += absMinutes;
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3591
                    if (style == 7 || style == 8 || (style >= 5 && absSeconds > 0)) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3592
                        formatZeroPad(isColon(), absSeconds, buf);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3593
                        output += absSeconds;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3594
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3595
                }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3596
                if (output == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3597
                    buf.setLength(bufPos);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3598
                    buf.append(noOffsetText);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3599
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3600
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3601
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3602
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3603
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3604
        private void formatZeroPad(boolean colon, int value, StringBuilder buf) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3605
            buf.append(colon ? ":" : "")
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3606
                    .append((char) (value / 10 + '0'))
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3607
                    .append((char) (value % 10 + '0'));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3608
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3609
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3610
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3611
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3612
            int length = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3613
            int noOffsetLen = noOffsetText.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3614
            if (noOffsetLen == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3615
                if (position == length) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3616
                    return context.setParsedField(OFFSET_SECONDS, 0, position, position);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3617
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3618
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3619
                if (position == length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3620
                    return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3621
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3622
                if (context.subSequenceEquals(text, position, noOffsetText, 0, noOffsetLen)) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3623
                    return context.setParsedField(OFFSET_SECONDS, 0, position, position + noOffsetLen);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3624
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3625
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3626
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3627
            // parse normal plus/minus offset
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3628
            char sign = text.charAt(position);  // IOOBE if invalid position
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3629
            if (sign == '+' || sign == '-') {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3630
                // starts
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3631
                int negative = (sign == '-' ? -1 : 1);
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3632
                boolean isColon = isColon();
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3633
                boolean paddedHour = isPaddedHour();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3634
                int[] array = new int[4];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3635
                array[0] = position + 1;
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3636
                int parseType = type;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3637
                // select parse type when lenient
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3638
                if (!context.isStrict()) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3639
                    if (paddedHour) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3640
                        if (isColon || (parseType == 0 && length > position + 3 && text.charAt(position + 3) == ':')) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3641
                            isColon = true; // needed in cases like ("+HH", "+01:01")
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3642
                            parseType = 10;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3643
                        } else {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3644
                            parseType = 9;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3645
                        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3646
                    } else {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3647
                        if (isColon || (parseType == 11 && length > position + 3 && (text.charAt(position + 2) == ':' || text.charAt(position + 3) == ':'))) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3648
                            isColon = true;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3649
                            parseType = 21;  // needed in cases like ("+H", "+1:01")
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3650
                        } else {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3651
                            parseType = 20;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3652
                        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3653
                    }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3654
                }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3655
                // parse according to the selected pattern
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3656
                switch (parseType) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3657
                    case 0: // +HH
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3658
                    case 11: // +H
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3659
                        parseHour(text, paddedHour, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3660
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3661
                    case 1: // +HHmm
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3662
                    case 2: // +HH:mm
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3663
                    case 13: // +H:mm
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3664
                        parseHour(text, paddedHour, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3665
                        parseMinute(text, isColon, false, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3666
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3667
                    case 3: // +HHMM
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3668
                    case 4: // +HH:MM
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3669
                    case 15: // +H:MM
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3670
                        parseHour(text, paddedHour, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3671
                        parseMinute(text, isColon, true, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3672
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3673
                    case 5: // +HHMMss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3674
                    case 6: // +HH:MM:ss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3675
                    case 17: // +H:MM:ss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3676
                        parseHour(text, paddedHour, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3677
                        parseMinute(text, isColon, true, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3678
                        parseSecond(text, isColon, false, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3679
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3680
                    case 7: // +HHMMSS
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3681
                    case 8: // +HH:MM:SS
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3682
                    case 19: // +H:MM:SS
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3683
                        parseHour(text, paddedHour, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3684
                        parseMinute(text, isColon, true, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3685
                        parseSecond(text, isColon, true, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3686
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3687
                    case 9: // +HHmmss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3688
                    case 10: // +HH:mm:ss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3689
                    case 21: // +H:mm:ss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3690
                        parseHour(text, paddedHour, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3691
                        parseOptionalMinuteSecond(text, isColon, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3692
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3693
                    case 12: // +Hmm
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3694
                        parseVariableWidthDigits(text, 1, 4, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3695
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3696
                    case 14: // +HMM
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3697
                        parseVariableWidthDigits(text, 3, 4, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3698
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3699
                    case 16: // +HMMss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3700
                        parseVariableWidthDigits(text, 3, 6, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3701
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3702
                    case 18: // +HMMSS
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3703
                        parseVariableWidthDigits(text, 5, 6, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3704
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3705
                    case 20: // +Hmmss
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3706
                        parseVariableWidthDigits(text, 1, 6, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3707
                        break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3708
                }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3709
                if (array[0] > 0) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3710
                    if (array[1] > 23 || array[2] > 59 || array[3] > 59) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3711
                        throw new DateTimeException("Value out of range: Hour[0-23], Minute[0-59], Second[0-59]");
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3712
                    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3713
                    long offsetSecs = negative * (array[1] * 3600L + array[2] * 60L + array[3]);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3714
                    return context.setParsedField(OFFSET_SECONDS, offsetSecs, position, array[0]);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3715
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3716
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3717
            // handle special case of empty no offset text
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3718
            if (noOffsetLen == 0) {
36646
de0663d2d82e 8032051: "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
ntv
parents: 36638
diff changeset
  3719
                return context.setParsedField(OFFSET_SECONDS, 0, position, position);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3720
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3721
            return ~position;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3722
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3723
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3724
        private void parseHour(CharSequence parseText, boolean paddedHour, int[] array) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3725
            if (paddedHour) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3726
                // parse two digits
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3727
                if (!parseDigits(parseText, false, 1, array)) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3728
                    array[0] = ~array[0];
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3729
                }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3730
            } else {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3731
                // parse one or two digits
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3732
                parseVariableWidthDigits(parseText, 1, 2, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3733
            }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3734
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3735
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3736
        private void parseMinute(CharSequence parseText, boolean isColon, boolean mandatory, int[] array) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3737
            if (!parseDigits(parseText, isColon, 2, array)) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3738
                if (mandatory) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3739
                    array[0] = ~array[0];
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3740
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3741
            }
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3742
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3743
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3744
        private void parseSecond(CharSequence parseText, boolean isColon, boolean mandatory, int[] array) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3745
            if (!parseDigits(parseText, isColon, 3, array)) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3746
                if (mandatory) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3747
                    array[0] = ~array[0];
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3748
                }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3749
            }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3750
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3751
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3752
        private void parseOptionalMinuteSecond(CharSequence parseText, boolean isColon, int[] array) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3753
            if (parseDigits(parseText, isColon, 2, array)) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3754
                parseDigits(parseText, isColon, 3, array);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3755
            }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3756
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3757
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3758
        private boolean parseDigits(CharSequence parseText, boolean isColon, int arrayIndex, int[] array) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3759
            int pos = array[0];
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3760
            if (pos < 0) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3761
                return true;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3762
            }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3763
            if (isColon && arrayIndex != 1) { //  ':' will precede only in case of minute/second
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3764
                if (pos + 1 > parseText.length() || parseText.charAt(pos) != ':') {
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3765
                    return false;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3766
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3767
                pos++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3768
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3769
            if (pos + 2 > parseText.length()) {
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3770
                return false;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3771
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3772
            char ch1 = parseText.charAt(pos++);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3773
            char ch2 = parseText.charAt(pos++);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3774
            if (ch1 < '0' || ch1 > '9' || ch2 < '0' || ch2 > '9') {
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3775
                return false;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3776
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3777
            int value = (ch1 - 48) * 10 + (ch2 - 48);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3778
            if (value < 0 || value > 59) {
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3779
                return false;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3780
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3781
            array[arrayIndex] = value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3782
            array[0] = pos;
39831
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3783
            return true;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3784
        }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3785
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3786
        private void parseVariableWidthDigits(CharSequence parseText, int minDigits, int maxDigits, int[] array) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3787
            // scan the text to find the available number of digits up to maxDigits
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3788
            // so long as the number available is minDigits or more, the input is valid
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3789
            // then parse the number of available digits
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3790
            int pos = array[0];
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3791
            int available = 0;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3792
            char[] chars = new char[maxDigits];
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3793
            for (int i = 0; i < maxDigits; i++) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3794
                if (pos + 1  > parseText.length()) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3795
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3796
                }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3797
                char ch = parseText.charAt(pos++);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3798
                if (ch < '0' || ch > '9') {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3799
                    pos--;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3800
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3801
                }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3802
                chars[i] = ch;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3803
                available++;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3804
            }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3805
            if (available < minDigits) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3806
                array[0] = ~array[0];
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3807
                return;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3808
            }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3809
            switch (available) {
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3810
                case 1:
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3811
                    array[1] = (chars[0] - 48);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3812
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3813
                case 2:
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3814
                    array[1] = ((chars[0] - 48) * 10 + (chars[1] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3815
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3816
                case 3:
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3817
                    array[1] = (chars[0] - 48);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3818
                    array[2] = ((chars[1] - 48) * 10 + (chars[2] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3819
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3820
                case 4:
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3821
                    array[1] = ((chars[0] - 48) * 10 + (chars[1] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3822
                    array[2] = ((chars[2] - 48) * 10 + (chars[3] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3823
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3824
                case 5:
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3825
                    array[1] = (chars[0] - 48);
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3826
                    array[2] = ((chars[1] - 48) * 10 + (chars[2] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3827
                    array[3] = ((chars[3] - 48) * 10 + (chars[4] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3828
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3829
                case 6:
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3830
                    array[1] = ((chars[0] - 48) * 10 + (chars[1] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3831
                    array[2] = ((chars[2] - 48) * 10 + (chars[3] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3832
                    array[3] = ((chars[4] - 48) * 10 + (chars[5] - 48));
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3833
                    break;
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3834
            }
6ea15d593819 8066806: java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
ntv
parents: 37907
diff changeset
  3835
            array[0] = pos;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3836
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3837
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3838
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3839
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3840
            String converted = noOffsetText.replace("'", "''");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  3841
            return "Offset(" + PATTERNS[type] + ",'" + converted + "')";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3842
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3843
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3844
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3845
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  3846
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3847
     * Prints or parses an offset ID.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3848
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3849
    static final class LocalizedOffsetIdPrinterParser implements DateTimePrinterParser {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3850
        private final TextStyle style;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3851
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3852
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3853
         * Constructor.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3854
         *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3855
         * @param style  the style, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3856
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3857
        LocalizedOffsetIdPrinterParser(TextStyle style) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3858
            this.style = style;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3859
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3860
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3861
        private static StringBuilder appendHMS(StringBuilder buf, int t) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3862
            return buf.append((char)(t / 10 + '0'))
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3863
                      .append((char)(t % 10 + '0'));
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3864
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3865
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3866
        @Override
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3867
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3868
            Long offsetSecs = context.getValue(OFFSET_SECONDS);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3869
            if (offsetSecs == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3870
                return false;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3871
            }
57478
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3872
            String key = "timezone.gmtZeroFormat";
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3873
            String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale());
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3874
            if (gmtText == null) {
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3875
                gmtText = "GMT";  // Default to "GMT"
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3876
            }
37596
cb38abe7e107 8154050: java.time.format.DateTimeFormatter can't parse localized zone-offset
ntv
parents: 36646
diff changeset
  3877
            buf.append(gmtText);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3878
            int totalSecs = Math.toIntExact(offsetSecs);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3879
            if (totalSecs != 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3880
                int absHours = Math.abs((totalSecs / 3600) % 100);  // anything larger than 99 silently dropped
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3881
                int absMinutes = Math.abs((totalSecs / 60) % 60);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3882
                int absSeconds = Math.abs(totalSecs % 60);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3883
                buf.append(totalSecs < 0 ? "-" : "+");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3884
                if (style == TextStyle.FULL) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3885
                    appendHMS(buf, absHours);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3886
                    buf.append(':');
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3887
                    appendHMS(buf, absMinutes);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3888
                    if (absSeconds != 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3889
                       buf.append(':');
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3890
                       appendHMS(buf, absSeconds);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3891
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3892
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3893
                    if (absHours >= 10) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3894
                        buf.append((char)(absHours / 10 + '0'));
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3895
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3896
                    buf.append((char)(absHours % 10 + '0'));
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3897
                    if (absMinutes != 0 || absSeconds != 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3898
                        buf.append(':');
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3899
                        appendHMS(buf, absMinutes);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3900
                        if (absSeconds != 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3901
                            buf.append(':');
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3902
                            appendHMS(buf, absSeconds);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3903
                        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3904
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3905
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3906
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3907
            return true;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3908
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3909
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3910
        int getDigit(CharSequence text, int position) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3911
            char c = text.charAt(position);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3912
            if (c < '0' || c > '9') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3913
                return -1;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3914
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3915
            return c - '0';
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3916
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3917
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3918
        @Override
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3919
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3920
            int pos = position;
37596
cb38abe7e107 8154050: java.time.format.DateTimeFormatter can't parse localized zone-offset
ntv
parents: 36646
diff changeset
  3921
            int end = text.length();
57478
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3922
            String key = "timezone.gmtZeroFormat";
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3923
            String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale());
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3924
            if (gmtText == null) {
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3925
                gmtText = "GMT";  // Default to "GMT"
5d7cb195ea1c 8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
rpatil
parents: 53018
diff changeset
  3926
            }
37596
cb38abe7e107 8154050: java.time.format.DateTimeFormatter can't parse localized zone-offset
ntv
parents: 36646
diff changeset
  3927
            if (!context.subSequenceEquals(text, pos, gmtText, 0, gmtText.length())) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3928
                    return ~position;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3929
                }
37596
cb38abe7e107 8154050: java.time.format.DateTimeFormatter can't parse localized zone-offset
ntv
parents: 36646
diff changeset
  3930
            pos += gmtText.length();
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3931
            // parse normal plus/minus offset
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3932
            int negative = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3933
            if (pos == end) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3934
                return context.setParsedField(OFFSET_SECONDS, 0, position, pos);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3935
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3936
            char sign = text.charAt(pos);  // IOOBE if invalid position
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3937
            if (sign == '+') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3938
                negative = 1;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3939
            } else if (sign == '-') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3940
                negative = -1;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3941
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3942
                return context.setParsedField(OFFSET_SECONDS, 0, position, pos);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3943
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3944
            pos++;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3945
            int h = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3946
            int m = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3947
            int s = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3948
            if (style == TextStyle.FULL) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3949
                int h1 = getDigit(text, pos++);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3950
                int h2 = getDigit(text, pos++);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3951
                if (h1 < 0 || h2 < 0 || text.charAt(pos++) != ':') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3952
                    return ~position;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3953
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3954
                h = h1 * 10 + h2;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3955
                int m1 = getDigit(text, pos++);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3956
                int m2 = getDigit(text, pos++);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3957
                if (m1 < 0 || m2 < 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3958
                    return ~position;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3959
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3960
                m = m1 * 10 + m2;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3961
                if (pos + 2 < end && text.charAt(pos) == ':') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3962
                    int s1 = getDigit(text, pos + 1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3963
                    int s2 = getDigit(text, pos + 2);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3964
                    if (s1 >= 0 && s2 >= 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3965
                        s = s1 * 10 + s2;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3966
                        pos += 3;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3967
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3968
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3969
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3970
                h = getDigit(text, pos++);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3971
                if (h < 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3972
                    return ~position;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3973
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3974
                if (pos < end) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3975
                    int h2 = getDigit(text, pos);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3976
                    if (h2 >=0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3977
                        h = h * 10 + h2;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3978
                        pos++;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3979
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3980
                    if (pos + 2 < end && text.charAt(pos) == ':') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3981
                        if (pos + 2 < end && text.charAt(pos) == ':') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3982
                            int m1 = getDigit(text, pos + 1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3983
                            int m2 = getDigit(text, pos + 2);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3984
                            if (m1 >= 0 && m2 >= 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3985
                                m = m1 * 10 + m2;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3986
                                pos += 3;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3987
                                if (pos + 2 < end && text.charAt(pos) == ':') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3988
                                    int s1 = getDigit(text, pos + 1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3989
                                    int s2 = getDigit(text, pos + 2);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3990
                                    if (s1 >= 0 && s2 >= 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3991
                                        s = s1 * 10 + s2;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3992
                                        pos += 3;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3993
                                   }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3994
                                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3995
                            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3996
                        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3997
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3998
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  3999
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4000
            long offsetSecs = negative * (h * 3600L + m * 60L + s);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4001
            return context.setParsedField(OFFSET_SECONDS, offsetSecs, position, pos);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4002
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4003
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4004
        @Override
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4005
        public String toString() {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4006
            return "LocalizedOffset(" + style + ")";
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4007
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4008
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4009
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4010
    //-----------------------------------------------------------------------
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4011
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4012
     * Prints or parses a zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4013
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4014
    static final class ZoneTextPrinterParser extends ZoneIdPrinterParser {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4015
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4016
        /** The text style to output. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4017
        private final TextStyle textStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4018
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4019
        /** The preferred zoneid map */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4020
        private Set<String> preferredZones;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4021
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4022
        /**  Display in generic time-zone format. True in case of pattern letter 'v' */
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4023
        private final boolean isGeneric;
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4024
        ZoneTextPrinterParser(TextStyle textStyle, Set<ZoneId> preferredZones, boolean isGeneric) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  4025
            super(TemporalQueries.zone(), "ZoneText(" + textStyle + ")");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4026
            this.textStyle = Objects.requireNonNull(textStyle, "textStyle");
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4027
            this.isGeneric = isGeneric;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4028
            if (preferredZones != null && preferredZones.size() != 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4029
                this.preferredZones = new HashSet<>();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4030
                for (ZoneId id : preferredZones) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4031
                    this.preferredZones.add(id.getId());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4032
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4033
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4034
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4035
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4036
        private static final int STD = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4037
        private static final int DST = 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4038
        private static final int GENERIC = 2;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4039
        private static final Map<String, SoftReference<Map<Locale, String[]>>> cache =
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4040
            new ConcurrentHashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4041
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4042
        private String getDisplayName(String id, int type, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4043
            if (textStyle == TextStyle.NARROW) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4044
                return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4045
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4046
            String[] names;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4047
            SoftReference<Map<Locale, String[]>> ref = cache.get(id);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4048
            Map<Locale, String[]> perLocale = null;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4049
            if (ref == null || (perLocale = ref.get()) == null ||
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4050
                (names = perLocale.get(locale)) == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4051
                names = TimeZoneNameUtility.retrieveDisplayNames(id, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4052
                if (names == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4053
                    return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4054
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4055
                names = Arrays.copyOfRange(names, 0, 7);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4056
                names[5] =
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4057
                    TimeZoneNameUtility.retrieveGenericDisplayName(id, TimeZone.LONG, locale);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4058
                if (names[5] == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4059
                    names[5] = names[0]; // use the id
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4060
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4061
                names[6] =
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4062
                    TimeZoneNameUtility.retrieveGenericDisplayName(id, TimeZone.SHORT, locale);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4063
                if (names[6] == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4064
                    names[6] = names[0];
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4065
                }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4066
                if (perLocale == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4067
                    perLocale = new ConcurrentHashMap<>();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4068
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4069
                perLocale.put(locale, names);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4070
                cache.put(id, new SoftReference<>(perLocale));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4071
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4072
            switch (type) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4073
            case STD:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4074
                return names[textStyle.zoneNameStyleIndex() + 1];
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4075
            case DST:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4076
                return names[textStyle.zoneNameStyleIndex() + 3];
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4077
            }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4078
            return names[textStyle.zoneNameStyleIndex() + 5];
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4079
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4080
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4081
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4082
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  4083
            ZoneId zone = context.getValue(TemporalQueries.zoneId());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4084
            if (zone == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4085
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4086
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4087
            String zname = zone.getId();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4088
            if (!(zone instanceof ZoneOffset)) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4089
                TemporalAccessor dt = context.getTemporal();
37907
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4090
                int type = GENERIC;
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4091
                if (!isGeneric) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4092
                    if (dt.isSupported(ChronoField.INSTANT_SECONDS)) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4093
                        type = zone.getRules().isDaylightSavings(Instant.from(dt)) ? DST : STD;
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4094
                    } else if (dt.isSupported(ChronoField.EPOCH_DAY) &&
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4095
                               dt.isSupported(ChronoField.NANO_OF_DAY)) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4096
                        LocalDate date = LocalDate.ofEpochDay(dt.getLong(ChronoField.EPOCH_DAY));
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4097
                        LocalTime time = LocalTime.ofNanoOfDay(dt.getLong(ChronoField.NANO_OF_DAY));
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4098
                        LocalDateTime ldt = date.atTime(time);
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4099
                        if (zone.getRules().getTransition(ldt) == null) {
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4100
                            type = zone.getRules().isDaylightSavings(ldt.atZone(zone).toInstant()) ? DST : STD;
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4101
                        }
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4102
                    }
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4103
                }
643c10927a1a 8155823: Add date-time patterns 'v' and 'vvvv'
ntv
parents: 37812
diff changeset
  4104
                String name = getDisplayName(zname, type, context.getLocale());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4105
                if (name != null) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4106
                    zname = name;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4107
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4108
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4109
            buf.append(zname);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4110
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4111
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4112
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4113
        // cache per instance for now
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4114
        private final Map<Locale, Entry<Integer, SoftReference<PrefixTree>>>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4115
            cachedTree = new HashMap<>();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4116
        private final Map<Locale, Entry<Integer, SoftReference<PrefixTree>>>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4117
            cachedTreeCI = new HashMap<>();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4118
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4119
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4120
        protected PrefixTree getTree(DateTimeParseContext context) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4121
            if (textStyle == TextStyle.NARROW) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4122
                return super.getTree(context);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4123
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4124
            Locale locale = context.getLocale();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4125
            boolean isCaseSensitive = context.isCaseSensitive();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4126
            Set<String> regionIds = ZoneRulesProvider.getAvailableZoneIds();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4127
            int regionIdsSize = regionIds.size();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4128
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4129
            Map<Locale, Entry<Integer, SoftReference<PrefixTree>>> cached =
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4130
                isCaseSensitive ? cachedTree : cachedTreeCI;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4131
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4132
            Entry<Integer, SoftReference<PrefixTree>> entry = null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4133
            PrefixTree tree = null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4134
            String[][] zoneStrings = null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4135
            if ((entry = cached.get(locale)) == null ||
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4136
                (entry.getKey() != regionIdsSize ||
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4137
                (tree = entry.getValue().get()) == null)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4138
                tree = PrefixTree.newTree(context);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4139
                zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4140
                for (String[] names : zoneStrings) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4141
                    String zid = names[0];
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4142
                    if (!regionIds.contains(zid)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4143
                        continue;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4144
                    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4145
                    tree.add(zid, zid);    // don't convert zid -> metazone
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4146
                    zid = ZoneName.toZid(zid, locale);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4147
                    int i = textStyle == TextStyle.FULL ? 1 : 2;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4148
                    for (; i < names.length; i += 2) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4149
                        tree.add(names[i], zid);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4150
                    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4151
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4152
                // if we have a set of preferred zones, need a copy and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4153
                // add the preferred zones again to overwrite
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4154
                if (preferredZones != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4155
                    for (String[] names : zoneStrings) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4156
                        String zid = names[0];
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4157
                        if (!preferredZones.contains(zid) || !regionIds.contains(zid)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4158
                            continue;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4159
                        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4160
                        int i = textStyle == TextStyle.FULL ? 1 : 2;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4161
                        for (; i < names.length; i += 2) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4162
                            tree.add(names[i], zid);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4163
                       }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4164
                    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4165
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4166
                cached.put(locale, new SimpleImmutableEntry<>(regionIdsSize, new SoftReference<>(tree)));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4167
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4168
            return tree;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4169
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4170
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4171
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4172
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4173
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4174
     * Prints or parses a zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4175
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4176
    static class ZoneIdPrinterParser implements DateTimePrinterParser {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4177
        private final TemporalQuery<ZoneId> query;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4178
        private final String description;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4179
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4180
        ZoneIdPrinterParser(TemporalQuery<ZoneId> query, String description) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4181
            this.query = query;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4182
            this.description = description;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4183
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4184
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4185
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4186
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4187
            ZoneId zone = context.getValue(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4188
            if (zone == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4189
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4190
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4191
            buf.append(zone.getId());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4192
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4193
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4194
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4195
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4196
         * The cached tree to speed up parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4197
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4198
        private static volatile Entry<Integer, PrefixTree> cachedPrefixTree;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4199
        private static volatile Entry<Integer, PrefixTree> cachedPrefixTreeCI;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4200
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4201
        protected PrefixTree getTree(DateTimeParseContext context) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4202
            // prepare parse tree
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4203
            Set<String> regionIds = ZoneRulesProvider.getAvailableZoneIds();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4204
            final int regionIdsSize = regionIds.size();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4205
            Entry<Integer, PrefixTree> cached = context.isCaseSensitive()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4206
                                                ? cachedPrefixTree : cachedPrefixTreeCI;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4207
            if (cached == null || cached.getKey() != regionIdsSize) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4208
                synchronized (this) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4209
                    cached = context.isCaseSensitive() ? cachedPrefixTree : cachedPrefixTreeCI;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4210
                    if (cached == null || cached.getKey() != regionIdsSize) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4211
                        cached = new SimpleImmutableEntry<>(regionIdsSize, PrefixTree.newTree(regionIds, context));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4212
                        if (context.isCaseSensitive()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4213
                            cachedPrefixTree = cached;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4214
                        } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4215
                            cachedPrefixTreeCI = cached;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4216
                        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4217
                    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4218
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4219
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4220
            return cached.getValue();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4221
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4222
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4223
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4224
         * This implementation looks for the longest matching string.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4225
         * For example, parsing Etc/GMT-2 will return Etc/GMC-2 rather than just
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4226
         * Etc/GMC although both are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4227
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4228
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4229
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4230
            int length = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4231
            if (position > length) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4232
                throw new IndexOutOfBoundsException();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4233
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4234
            if (position == length) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4235
                return ~position;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4236
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4237
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4238
            // handle fixed time-zone IDs
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4239
            char nextChar = text.charAt(position);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4240
            if (nextChar == '+' || nextChar == '-') {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4241
                return parseOffsetBased(context, text, position, position, OffsetIdPrinterParser.INSTANCE_ID_Z);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4242
            } else if (length >= position + 2) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4243
                char nextNextChar = text.charAt(position + 1);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4244
                if (context.charEquals(nextChar, 'U') && context.charEquals(nextNextChar, 'T')) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4245
                    if (length >= position + 3 && context.charEquals(text.charAt(position + 2), 'C')) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4246
                        return parseOffsetBased(context, text, position, position + 3, OffsetIdPrinterParser.INSTANCE_ID_ZERO);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4247
                    }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4248
                    return parseOffsetBased(context, text, position, position + 2, OffsetIdPrinterParser.INSTANCE_ID_ZERO);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4249
                } else if (context.charEquals(nextChar, 'G') && length >= position + 3 &&
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4250
                        context.charEquals(nextNextChar, 'M') && context.charEquals(text.charAt(position + 2), 'T')) {
33671
3a1185883ff1 8138664: ZonedDateTime parse error for any date using 'GMT0' ZoneID - Java Bug System
ntv
parents: 29877
diff changeset
  4251
                    if (length >= position + 4 && context.charEquals(text.charAt(position + 3), '0')) {
3a1185883ff1 8138664: ZonedDateTime parse error for any date using 'GMT0' ZoneID - Java Bug System
ntv
parents: 29877
diff changeset
  4252
                        context.setParsed(ZoneId.of("GMT0"));
3a1185883ff1 8138664: ZonedDateTime parse error for any date using 'GMT0' ZoneID - Java Bug System
ntv
parents: 29877
diff changeset
  4253
                        return position + 4;
3a1185883ff1 8138664: ZonedDateTime parse error for any date using 'GMT0' ZoneID - Java Bug System
ntv
parents: 29877
diff changeset
  4254
                    }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4255
                    return parseOffsetBased(context, text, position, position + 3, OffsetIdPrinterParser.INSTANCE_ID_ZERO);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4256
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4257
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4258
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4259
            // parse
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4260
            PrefixTree tree = getTree(context);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4261
            ParsePosition ppos = new ParsePosition(position);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4262
            String parsedZoneId = tree.match(text, ppos);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4263
            if (parsedZoneId == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4264
                if (context.charEquals(nextChar, 'Z')) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4265
                    context.setParsed(ZoneOffset.UTC);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4266
                    return position + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4267
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4268
                return ~position;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4269
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4270
            context.setParsed(ZoneId.of(parsedZoneId));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4271
            return ppos.getIndex();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4272
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4273
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4274
        /**
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4275
         * Parse an offset following a prefix and set the ZoneId if it is valid.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4276
         * To matching the parsing of ZoneId.of the values are not normalized
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4277
         * to ZoneOffsets.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4278
         *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4279
         * @param context the parse context
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4280
         * @param text the input text
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4281
         * @param prefixPos start of the prefix
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4282
         * @param position start of text after the prefix
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4283
         * @param parser parser for the value after the prefix
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4284
         * @return the position after the parse
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4285
         */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4286
        private int parseOffsetBased(DateTimeParseContext context, CharSequence text, int prefixPos, int position, OffsetIdPrinterParser parser) {
49983
41069c4fad29 8202582: DateTimeFormatterBuilder.parseOffsetBased unnecessarily calls toString()
rgoel
parents: 48251
diff changeset
  4287
            String prefix = text.subSequence(prefixPos, position).toString().toUpperCase();
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4288
            if (position >= text.length()) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4289
                context.setParsed(ZoneId.of(prefix));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4290
                return position;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4291
            }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4292
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4293
            // '0' or 'Z' after prefix is not part of a valid ZoneId; use bare prefix
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4294
            if (text.charAt(position) == '0' ||
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4295
                context.charEquals(text.charAt(position), 'Z')) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4296
                context.setParsed(ZoneId.of(prefix));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4297
                return position;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4298
            }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4299
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4300
            DateTimeParseContext newContext = context.copy();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4301
            int endPos = parser.parse(newContext, text, position);
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4302
            try {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4303
                if (endPos < 0) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4304
                    if (parser == OffsetIdPrinterParser.INSTANCE_ID_Z) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4305
                        return ~prefixPos;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4306
                    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4307
                    context.setParsed(ZoneId.of(prefix));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4308
                    return position;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4309
                }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4310
                int offset = (int) newContext.getParsed(OFFSET_SECONDS).longValue();
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4311
                ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(offset);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4312
                context.setParsed(ZoneId.ofOffset(prefix, zoneOffset));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4313
                return endPos;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4314
            } catch (DateTimeException dte) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  4315
                return ~prefixPos;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4316
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4317
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4318
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4319
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4320
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4321
            return description;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4322
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4323
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4324
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4325
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4326
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4327
     * A String based prefix tree for parsing time-zone names.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4328
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4329
    static class PrefixTree {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4330
        protected String key;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4331
        protected String value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4332
        protected char c0;    // performance optimization to avoid the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4333
                              // boundary check cost of key.charat(0)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4334
        protected PrefixTree child;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4335
        protected PrefixTree sibling;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4336
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4337
        private PrefixTree(String k, String v, PrefixTree child) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4338
            this.key = k;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4339
            this.value = v;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4340
            this.child = child;
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52012
diff changeset
  4341
            if (k.isEmpty()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4342
                c0 = 0xffff;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4343
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4344
                c0 = key.charAt(0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4345
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4346
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4347
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4348
        /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4349
         * Creates a new prefix parsing tree based on parse context.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4350
         *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4351
         * @param context  the parse context
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4352
         * @return the tree, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4353
         */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4354
        public static PrefixTree newTree(DateTimeParseContext context) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4355
            //if (!context.isStrict()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4356
            //    return new LENIENT("", null, null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4357
            //}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4358
            if (context.isCaseSensitive()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4359
                return new PrefixTree("", null, null);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4360
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4361
            return new CI("", null, null);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4362
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4363
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4364
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4365
         * Creates a new prefix parsing tree.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4366
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4367
         * @param keys  a set of strings to build the prefix parsing tree, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4368
         * @param context  the parse context
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4369
         * @return the tree, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4370
         */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4371
        public static  PrefixTree newTree(Set<String> keys, DateTimeParseContext context) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4372
            PrefixTree tree = newTree(context);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4373
            for (String k : keys) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4374
                tree.add0(k, k);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4375
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4376
            return tree;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4377
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4378
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4379
        /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4380
         * Clone a copy of this tree
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4381
         */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4382
        public PrefixTree copyTree() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4383
            PrefixTree copy = new PrefixTree(key, value, null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4384
            if (child != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4385
                copy.child = child.copyTree();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4386
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4387
            if (sibling != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4388
                copy.sibling = sibling.copyTree();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4389
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4390
            return copy;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4391
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4392
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4393
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4394
        /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4395
         * Adds a pair of {key, value} into the prefix tree.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4396
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4397
         * @param k  the key, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4398
         * @param v  the value, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4399
         * @return  true if the pair is added successfully
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4400
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4401
        public boolean add(String k, String v) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4402
            return add0(k, v);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4403
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4404
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4405
        private boolean add0(String k, String v) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4406
            k = toKey(k);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4407
            int prefixLen = prefixLength(k);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4408
            if (prefixLen == key.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4409
                if (prefixLen < k.length()) {  // down the tree
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4410
                    String subKey = k.substring(prefixLen);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4411
                    PrefixTree c = child;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4412
                    while (c != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4413
                        if (isEqual(c.c0, subKey.charAt(0))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4414
                            return c.add0(subKey, v);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4415
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4416
                        c = c.sibling;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4417
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4418
                    // add the node as the child of the current node
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4419
                    c = newNode(subKey, v, null);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4420
                    c.sibling = child;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4421
                    child = c;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4422
                    return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4423
                }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4424
                // have an existing <key, value> already, overwrite it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4425
                // if (value != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4426
                //    return false;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4427
                //}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4428
                value = v;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4429
                return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4430
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4431
            // split the existing node
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4432
            PrefixTree n1 = newNode(key.substring(prefixLen), value, child);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4433
            key = k.substring(0, prefixLen);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4434
            child = n1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4435
            if (prefixLen < k.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4436
                PrefixTree n2 = newNode(k.substring(prefixLen), v, null);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4437
                child.sibling = n2;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4438
                value = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4439
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4440
                value = v;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4441
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4442
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4443
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4444
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4445
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4446
         * Match text with the prefix tree.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4447
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4448
         * @param text  the input text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4449
         * @param off  the offset position to start parsing at
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4450
         * @param end  the end position to stop parsing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4451
         * @return the resulting string, or null if no match found.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4452
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4453
        public String match(CharSequence text, int off, int end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4454
            if (!prefixOf(text, off, end)){
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4455
                return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4456
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4457
            if (child != null && (off += key.length()) != end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4458
                PrefixTree c = child;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4459
                do {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4460
                    if (isEqual(c.c0, text.charAt(off))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4461
                        String found = c.match(text, off, end);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4462
                        if (found != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4463
                            return found;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4464
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4465
                        return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4466
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4467
                    c = c.sibling;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4468
                } while (c != null);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4469
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4470
            return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4471
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4472
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4473
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4474
         * Match text with the prefix tree.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4475
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4476
         * @param text  the input text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4477
         * @param pos  the position to start parsing at, from 0 to the text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4478
         *  length. Upon return, position will be updated to the new parse
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4479
         *  position, or unchanged, if no match found.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4480
         * @return the resulting string, or null if no match found.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4481
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4482
        public String match(CharSequence text, ParsePosition pos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4483
            int off = pos.getIndex();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4484
            int end = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4485
            if (!prefixOf(text, off, end)){
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4486
                return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4487
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4488
            off += key.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4489
            if (child != null && off != end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4490
                PrefixTree c = child;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4491
                do {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4492
                    if (isEqual(c.c0, text.charAt(off))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4493
                        pos.setIndex(off);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4494
                        String found = c.match(text, pos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4495
                        if (found != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4496
                            return found;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4497
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4498
                        break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4499
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4500
                    c = c.sibling;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4501
                } while (c != null);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4502
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4503
            pos.setIndex(off);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4504
            return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4505
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4506
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4507
        protected String toKey(String k) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4508
            return k;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4509
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4510
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4511
        protected PrefixTree newNode(String k, String v, PrefixTree child) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4512
            return new PrefixTree(k, v, child);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4513
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4514
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4515
        protected boolean isEqual(char c1, char c2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4516
            return c1 == c2;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4517
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4518
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4519
        protected boolean prefixOf(CharSequence text, int off, int end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4520
            if (text instanceof String) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4521
                return ((String)text).startsWith(key, off);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4522
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4523
            int len = key.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4524
            if (len > end - off) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4525
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4526
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4527
            int off0 = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4528
            while (len-- > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4529
                if (!isEqual(key.charAt(off0++), text.charAt(off++))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4530
                    return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4531
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4532
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4533
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4534
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4535
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4536
        private int prefixLength(String k) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4537
            int off = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4538
            while (off < k.length() && off < key.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4539
                if (!isEqual(k.charAt(off), key.charAt(off))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4540
                    return off;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4541
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4542
                off++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4543
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4544
            return off;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4545
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4546
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4547
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4548
         * Case Insensitive prefix tree.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4549
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4550
        private static class CI extends PrefixTree {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4551
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4552
            private CI(String k, String v, PrefixTree child) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4553
                super(k, v, child);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4554
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4555
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4556
            @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4557
            protected CI newNode(String k, String v, PrefixTree child) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4558
                return new CI(k, v, child);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4559
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4560
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4561
            @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4562
            protected boolean isEqual(char c1, char c2) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4563
                return DateTimeParseContext.charEqualsIgnoreCase(c1, c2);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4564
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4565
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4566
            @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4567
            protected boolean prefixOf(CharSequence text, int off, int end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4568
                int len = key.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4569
                if (len > end - off) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4570
                    return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4571
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4572
                int off0 = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4573
                while (len-- > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4574
                    if (!isEqual(key.charAt(off0++), text.charAt(off++))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4575
                        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4576
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4577
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4578
                return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4579
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4580
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4581
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4582
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4583
         * Lenient prefix tree. Case insensitive and ignores characters
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4584
         * like space, underscore and slash.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4585
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4586
        private static class LENIENT extends CI {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4587
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4588
            private LENIENT(String k, String v, PrefixTree child) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4589
                super(k, v, child);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4590
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4591
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4592
            @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4593
            protected CI newNode(String k, String v, PrefixTree child) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4594
                return new LENIENT(k, v, child);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4595
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4596
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4597
            private boolean isLenientChar(char c) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4598
                return c == ' ' || c == '_' || c == '/';
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4599
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4600
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4601
            protected String toKey(String k) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4602
                for (int i = 0; i < k.length(); i++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4603
                    if (isLenientChar(k.charAt(i))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4604
                        StringBuilder sb = new StringBuilder(k.length());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4605
                        sb.append(k, 0, i);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4606
                        i++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4607
                        while (i < k.length()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4608
                            if (!isLenientChar(k.charAt(i))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4609
                                sb.append(k.charAt(i));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4610
                            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4611
                            i++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4612
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4613
                        return sb.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4614
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4615
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4616
                return k;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4617
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4618
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4619
            @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4620
            public String match(CharSequence text, ParsePosition pos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4621
                int off = pos.getIndex();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4622
                int end = text.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4623
                int len = key.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4624
                int koff = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4625
                while (koff < len && off < end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4626
                    if (isLenientChar(text.charAt(off))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4627
                        off++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4628
                        continue;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4629
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4630
                    if (!isEqual(key.charAt(koff++), text.charAt(off++))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4631
                        return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4632
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4633
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4634
                if (koff != len) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4635
                    return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4636
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4637
                if (child != null && off != end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4638
                    int off0 = off;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4639
                    while (off0 < end && isLenientChar(text.charAt(off0))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4640
                        off0++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4641
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4642
                    if (off0 < end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4643
                        PrefixTree c = child;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4644
                        do {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4645
                            if (isEqual(c.c0, text.charAt(off0))) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4646
                                pos.setIndex(off0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4647
                                String found = c.match(text, pos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4648
                                if (found != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4649
                                    return found;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4650
                                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4651
                                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4652
                            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4653
                            c = c.sibling;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4654
                        } while (c != null);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4655
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4656
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4657
                pos.setIndex(off);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4658
                return value;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4659
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4660
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4661
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4662
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4663
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4664
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4665
     * Prints or parses a chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4666
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4667
    static final class ChronoPrinterParser implements DateTimePrinterParser {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4668
        /** The text style to output, null means the ID. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4669
        private final TextStyle textStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4670
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4671
        ChronoPrinterParser(TextStyle textStyle) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4672
            // validated by caller
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4673
            this.textStyle = textStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4674
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4675
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4676
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4677
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20749
diff changeset
  4678
            Chronology chrono = context.getValue(TemporalQueries.chronology());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4679
            if (chrono == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4680
                return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4681
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4682
            if (textStyle == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4683
                buf.append(chrono.getId());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4684
            } else {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4685
                buf.append(getChronologyName(chrono, context.getLocale()));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4686
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4687
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4688
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4689
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4690
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4691
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4692
            // simple looping parser to find the chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4693
            if (position < 0 || position > text.length()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4694
                throw new IndexOutOfBoundsException();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4695
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4696
            Set<Chronology> chronos = Chronology.getAvailableChronologies();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4697
            Chronology bestMatch = null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4698
            int matchLen = -1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4699
            for (Chronology chrono : chronos) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4700
                String name;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4701
                if (textStyle == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4702
                    name = chrono.getId();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4703
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4704
                    name = getChronologyName(chrono, context.getLocale());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4705
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4706
                int nameLen = name.length();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4707
                if (nameLen > matchLen && context.subSequenceEquals(text, position, name, 0, nameLen)) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4708
                    bestMatch = chrono;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4709
                    matchLen = nameLen;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4710
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4711
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4712
            if (bestMatch == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4713
                return ~position;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4714
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4715
            context.setParsed(bestMatch);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4716
            return position + matchLen;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4717
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4718
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4719
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4720
         * Returns the chronology name of the given chrono in the given locale
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4721
         * if available, or the chronology Id otherwise. The regular ResourceBundle
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4722
         * search path is used for looking up the chronology name.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4723
         *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4724
         * @param chrono  the chronology, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4725
         * @param locale  the locale, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4726
         * @return the chronology name of chrono in locale, or the id if no name is available
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4727
         * @throws NullPointerException if chrono or locale is null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4728
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4729
        private String getChronologyName(Chronology chrono, Locale locale) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4730
            String key = "calendarname." + chrono.getCalendarType();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4731
            String name = DateTimeTextProvider.getLocalizedResource(key, locale);
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33671
diff changeset
  4732
            return Objects.requireNonNullElseGet(name, () -> chrono.getId());
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4733
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4734
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4735
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4736
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4737
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4738
     * Prints or parses a localized pattern.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4739
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4740
    static final class LocalizedPrinterParser implements DateTimePrinterParser {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4741
        /** Cache of formatters. */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4742
        private static final ConcurrentMap<String, DateTimeFormatter> FORMATTER_CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4743
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4744
        private final FormatStyle dateStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4745
        private final FormatStyle timeStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4746
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4747
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4748
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4749
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4750
         * @param dateStyle  the date style to use, may be null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4751
         * @param timeStyle  the time style to use, may be null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4752
         */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4753
        LocalizedPrinterParser(FormatStyle dateStyle, FormatStyle timeStyle) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4754
            // validated by caller
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4755
            this.dateStyle = dateStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4756
            this.timeStyle = timeStyle;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4757
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4758
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4759
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4760
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4761
            Chronology chrono = Chronology.from(context.getTemporal());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4762
            return formatter(context.getLocale(), chrono).toPrinterParser(false).format(context, buf);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4763
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4764
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4765
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4766
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4767
            Chronology chrono = context.getEffectiveChronology();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4768
            return formatter(context.getLocale(), chrono).toPrinterParser(false).parse(context, text, position);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4769
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4770
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4771
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4772
         * Gets the formatter to use.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4773
         * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4774
         * The formatter will be the most appropriate to use for the date and time style in the locale.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4775
         * For example, some locales will use the month name while others will use the number.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4776
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4777
         * @param locale  the locale to use, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4778
         * @param chrono  the chronology to use, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4779
         * @return the formatter, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4780
         * @throws IllegalArgumentException if the formatter cannot be found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4781
         */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4782
        private DateTimeFormatter formatter(Locale locale, Chronology chrono) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4783
            String key = chrono.getId() + '|' + locale.toString() + '|' + dateStyle + timeStyle;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4784
            DateTimeFormatter formatter = FORMATTER_CACHE.get(key);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4785
            if (formatter == null) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  4786
                String pattern = getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4787
                formatter = new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4788
                DateTimeFormatter old = FORMATTER_CACHE.putIfAbsent(key, formatter);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4789
                if (old != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4790
                    formatter = old;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4791
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4792
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4793
            return formatter;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4794
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4795
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4796
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4797
        public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4798
            return "Localized(" + (dateStyle != null ? dateStyle : "") + "," +
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4799
                (timeStyle != null ? timeStyle : "") + ")";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4800
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4801
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4802
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4803
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4804
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4805
     * Prints or parses a localized pattern from a localized field.
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
  4806
     * The specific formatter and parameters is not selected until
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4807
     * the field is to be printed or parsed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4808
     * The locale is needed to select the proper WeekFields from which
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4809
     * the field for day-of-week, week-of-month, or week-of-year is selected.
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4810
     * Hence the inherited field NumberPrinterParser.field is unused.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4811
     */
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4812
    static final class WeekBasedFieldPrinterParser extends NumberPrinterParser {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4813
        private char chr;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4814
        private int count;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4815
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4816
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4817
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4818
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4819
         * @param chr the pattern format letter that added this PrinterParser.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4820
         * @param count the repeat count of the format letter
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4821
         * @param minWidth  the minimum field width, from 1 to 19
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4822
         * @param maxWidth  the maximum field width, from minWidth to 19
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4823
         */
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4824
        WeekBasedFieldPrinterParser(char chr, int count, int minWidth, int maxWidth) {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4825
            this(chr, count, minWidth, maxWidth, 0);
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4826
        }
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4827
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4828
        /**
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4829
         * Constructor.
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4830
         *
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4831
         * @param chr the pattern format letter that added this PrinterParser.
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4832
         * @param count the repeat count of the format letter
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4833
         * @param minWidth  the minimum field width, from 1 to 19
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4834
         * @param maxWidth  the maximum field width, from minWidth to 19
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4835
         * @param subsequentWidth  the width of subsequent non-negative numbers, 0 or greater,
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4836
         * -1 if fixed width due to active adjacent parsing
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4837
         */
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4838
        WeekBasedFieldPrinterParser(char chr, int count, int minWidth, int maxWidth,
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4839
                int subsequentWidth) {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4840
            super(null, minWidth, maxWidth, SignStyle.NOT_NEGATIVE, subsequentWidth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4841
            this.chr = chr;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4842
            this.count = count;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4843
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4844
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4845
        /**
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4846
         * Returns a new instance with fixed width flag set.
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4847
         *
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4848
         * @return a new updated printer-parser, not null
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4849
         */
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4850
        @Override
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4851
        WeekBasedFieldPrinterParser withFixedWidth() {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4852
            if (subsequentWidth == -1) {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4853
                return this;
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4854
            }
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4855
            return new WeekBasedFieldPrinterParser(chr, count, minWidth, maxWidth, -1);
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4856
        }
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4857
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4858
        /**
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4859
         * Returns a new instance with an updated subsequent width.
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4860
         *
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4861
         * @param subsequentWidth  the width of subsequent non-negative numbers, 0 or greater
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4862
         * @return a new updated printer-parser, not null
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4863
         */
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4864
        @Override
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4865
        WeekBasedFieldPrinterParser withSubsequentWidth(int subsequentWidth) {
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4866
            return new WeekBasedFieldPrinterParser(chr, count, minWidth, maxWidth,
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4867
                    this.subsequentWidth + subsequentWidth);
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4868
        }
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4869
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4870
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4871
        public boolean format(DateTimePrintContext context, StringBuilder buf) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  4872
            return printerParser(context.getLocale()).format(context, buf);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4873
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4874
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4875
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4876
        public int parse(DateTimeParseContext context, CharSequence text, int position) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4877
            return printerParser(context.getLocale()).parse(context, text, position);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4878
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4879
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4880
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4881
         * Gets the printerParser to use based on the field and the locale.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4882
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4883
         * @param locale  the locale to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4884
         * @return the formatter, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4885
         * @throws IllegalArgumentException if the formatter cannot be found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4886
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4887
        private DateTimePrinterParser printerParser(Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4888
            WeekFields weekDef = WeekFields.of(locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4889
            TemporalField field = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4890
            switch (chr) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4891
                case 'Y':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4892
                    field = weekDef.weekBasedYear();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4893
                    if (count == 2) {
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4894
                        return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE,
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4895
                                this.subsequentWidth);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4896
                    } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4897
                        return new NumberPrinterParser(field, count, 19,
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4898
                                (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD,
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4899
                                this.subsequentWidth);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4900
                    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4901
                case 'e':
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4902
                case 'c':
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4903
                    field = weekDef.dayOfWeek();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4904
                    break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4905
                case 'w':
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4906
                    field = weekDef.weekOfWeekBasedYear();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4907
                    break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4908
                case 'W':
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4909
                    field = weekDef.weekOfMonth();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4910
                    break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4911
                default:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4912
                    throw new IllegalStateException("unreachable");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4913
            }
42924
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4914
            return new NumberPrinterParser(field, minWidth, maxWidth, SignStyle.NOT_NEGATIVE,
0d68b06248fb 8145633: Adjacent value parsing not supported for Localized Patterns
ntv
parents: 42360
diff changeset
  4915
                    this.subsequentWidth);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4916
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4917
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4918
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4919
        public String toString() {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4920
            StringBuilder sb = new StringBuilder(30);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4921
            sb.append("Localized(");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4922
            if (chr == 'Y') {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4923
                if (count == 1) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4924
                    sb.append("WeekBasedYear");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4925
                } else if (count == 2) {
20749
7891146ae141 8024076: Incorrect 2 -> 4 year parsing and resolution in DateTimeFormatter
rriggs
parents: 19030
diff changeset
  4926
                    sb.append("ReducedValue(WeekBasedYear,2,2,2000-01-01)");
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4927
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4928
                    sb.append("WeekBasedYear,").append(count).append(",")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4929
                            .append(19).append(",")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4930
                            .append((count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4931
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4932
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4933
                switch (chr) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4934
                    case 'c':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4935
                    case 'e':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4936
                        sb.append("DayOfWeek");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4937
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4938
                    case 'w':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4939
                        sb.append("WeekOfWeekBasedYear");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4940
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4941
                    case 'W':
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4942
                        sb.append("WeekOfMonth");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4943
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4944
                    default:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4945
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4946
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4947
                sb.append(",");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4948
                sb.append(count);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4949
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4950
            sb.append(")");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 16005
diff changeset
  4951
            return sb.toString();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4952
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4953
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4954
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4955
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4956
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4957
     * Length comparator.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4958
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4959
    static final Comparator<String> LENGTH_SORT = new Comparator<String>() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4960
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4961
        public int compare(String str1, String str2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4962
            return str1.length() == str2.length() ? str1.compareTo(str2) : str1.length() - str2.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4963
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4964
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  4965
}