jdk/src/share/classes/java/time/format/DateTimeFormatter.java
changeset 16852 60207b2b4b42
parent 15658 55b829ca2334
child 17474 8c100beabcc0
equal deleted inserted replaced
16851:3bbdae468b05 16852:60207b2b4b42
    77 import java.text.ParseException;
    77 import java.text.ParseException;
    78 import java.text.ParsePosition;
    78 import java.text.ParsePosition;
    79 import java.time.DateTimeException;
    79 import java.time.DateTimeException;
    80 import java.time.ZoneId;
    80 import java.time.ZoneId;
    81 import java.time.ZoneOffset;
    81 import java.time.ZoneOffset;
       
    82 import java.time.chrono.Chronology;
       
    83 import java.time.chrono.IsoChronology;
    82 import java.time.format.DateTimeFormatterBuilder.CompositePrinterParser;
    84 import java.time.format.DateTimeFormatterBuilder.CompositePrinterParser;
    83 import java.time.chrono.Chronology;
       
    84 import java.time.temporal.ChronoField;
    85 import java.time.temporal.ChronoField;
    85 import java.time.temporal.IsoFields;
    86 import java.time.temporal.IsoFields;
    86 import java.time.temporal.TemporalAccessor;
    87 import java.time.temporal.TemporalAccessor;
       
    88 import java.time.temporal.TemporalField;
    87 import java.time.temporal.TemporalQuery;
    89 import java.time.temporal.TemporalQuery;
       
    90 import java.util.Arrays;
       
    91 import java.util.Collections;
    88 import java.util.HashMap;
    92 import java.util.HashMap;
       
    93 import java.util.HashSet;
    89 import java.util.Locale;
    94 import java.util.Locale;
    90 import java.util.Map;
    95 import java.util.Map;
    91 import java.util.Objects;
    96 import java.util.Objects;
       
    97 import java.util.Set;
    92 
    98 
    93 /**
    99 /**
    94  * Formatter for printing and parsing date-time objects.
   100  * Formatter for printing and parsing date-time objects.
    95  * <p>
   101  * <p>
    96  * This class provides the main application entry point for printing and parsing
   102  * This class provides the main application entry point for printing and parsing
    97  * and provides common implementations of {@code DateTimeFormatter}:
   103  * and provides common implementations of {@code DateTimeFormatter}:
    98  * <p><ul>
   104  * <ul>
    99  * <li>Using pattern letters, such as {@code yyyy-MMM-dd}
   105  * <li>Using predefined constants, such as {@link #ISO_LOCAL_DATE}</li>
   100  * <li>Using localized styles, such as {@code long} or {@code medium}
   106  * <li>Using pattern letters, such as {@code uuuu-MMM-dd}</li>
   101  * <li>Using predefined constants, such as {@code ISO_LOCAL_DATE}
   107  * <li>Using localized styles, such as {@code long} or {@code medium}</li>
   102  * </ul></p>
   108  * </ul>
       
   109  * <p>
       
   110  * More complex formatters are provided by
       
   111  * {@link DateTimeFormatterBuilder DateTimeFormatterBuilder}.
   103  *
   112  *
   104  * <p>
   113  * <p>
   105  * In most cases, provided formatters will be sufficient.
   114  * The main date-time classes provide two methods - one for formatting,
   106  * For more complex formatters, a {@link DateTimeFormatterBuilder builder} is provided.
   115  * {@code format(DateTimeFormatter formatter)}, and one for parsing,
   107  * The main date-time classes provide two methods - one for printing,
       
   108  * {@code toString(DateTimeFormatter formatter)}, and one for parsing,
       
   109  * {@code parse(CharSequence text, DateTimeFormatter formatter)}.
   116  * {@code parse(CharSequence text, DateTimeFormatter formatter)}.
   110  * For example:
   117  * <p>For example:
   111  * <pre>
   118  * <blockquote><pre>
   112  *  String text = date.toString(formatter);
   119  *  String text = date.toString(formatter);
   113  *  LocalDate date = LocalDate.parse(text, formatter);
   120  *  LocalDate date = LocalDate.parse(text, formatter);
       
   121  * </pre></blockquote>
       
   122  * <p>
       
   123  * In addition to the format, formatters can be created with desired Locale,
       
   124  * Chronology, ZoneId, and formatting symbols.
       
   125  * <p>
       
   126  * The {@link #withLocale withLocale} method returns a new formatter that
       
   127  * overrides the locale. The locale affects some aspects of formatting and
       
   128  * parsing. For example, the {@link #ofLocalizedDate ofLocalizedDate} provides a
       
   129  * formatter that uses the locale specific date format.
       
   130  * <p>
       
   131  * The {@link #withChronology withChronology} method returns a new formatter
       
   132  * that overrides the chronology. If overridden, the date-time value is
       
   133  * converted to the chronology before formatting. During parsing the date-time
       
   134  * value is converted to the chronology before it is returned.
       
   135  * <p>
       
   136  * The {@link #withZone withZone} method returns a new formatter that overrides
       
   137  * the zone. If overridden, the date-time value is converted to a ZonedDateTime
       
   138  * with the requested ZoneId before formatting. During parsing the ZoneId is
       
   139  * applied before the value is returned.
       
   140  * <p>
       
   141  * The {@link #withSymbols withSymbols} method returns a new formatter that
       
   142  * overrides the {@link DateTimeFormatSymbols}. The symbols are used for
       
   143  * formatting and parsing.
       
   144  * <p>
       
   145  * Some applications may need to use the older {@link Format java.text.Format}
       
   146  * class for formatting. The {@link #toFormat()} method returns an
       
   147  * implementation of {@code java.text.Format}.
       
   148  * <p>
       
   149  * <h3 id="predefined">Predefined Formatters</h3>
       
   150  * <table summary="Predefined Formatters" cellpadding="2" cellspacing="3" border="0" >
       
   151  * <thead>
       
   152  * <tr class="tableSubHeadingColor">
       
   153  * <th class="colFirst" align="left">Formatter</th>
       
   154  * <th class="colFirst" align="left">Description</th>
       
   155  * <th class="colLast" align="left">Example</th>
       
   156  * </tr>
       
   157  * </thead>
       
   158  * <tbody>
       
   159  * <tr class="rowColor">
       
   160  * <td>{@link #ofLocalizedDate ofLocalizedDate(dateStyle)} </td>
       
   161  * <td> Formatter with date style from the locale </td>
       
   162  * <td> '2011-12-03'</td>
       
   163  * </tr>
       
   164  * <tr class="altColor">
       
   165  * <td> {@link #ofLocalizedTime ofLocalizedTime(timeStyle)} </td>
       
   166  * <td> Formatter with time style from the locale </td>
       
   167  * <td> '10:15:30'</td>
       
   168  * </tr>
       
   169  * <tr class="rowColor">
       
   170  * <td> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateTimeStyle)} </td>
       
   171  * <td> Formatter with a style for date and time from the locale</td>
       
   172  * <td> '3 Jun 2008 11:05:30'</td>
       
   173  * </tr>
       
   174  * <tr class="altColor">
       
   175  * <td> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateStyle,timeStyle)}
       
   176  * </td>
       
   177  * <td> Formatter with date and time styles from the locale </td>
       
   178  * <td> '3 Jun 2008 11:05'</td>
       
   179  * </tr>
       
   180  * <tr class="rowColor">
       
   181  * <td> {@link #BASIC_ISO_DATE}</td>
       
   182  * <td>Basic ISO date </td> <td>'20111203'</td>
       
   183  * </tr>
       
   184  * <tr class="altColor">
       
   185  * <td> {@link #ISO_LOCAL_DATE}</td>
       
   186  * <td> ISO Local Date </td>
       
   187  * <td>'2011-12-03'</td>
       
   188  * </tr>
       
   189  * <tr class="rowColor">
       
   190  * <td> {@link #ISO_OFFSET_DATE}</td>
       
   191  * <td> ISO Date with offset </td>
       
   192  * <td>'2011-12-03+01:00'</td>
       
   193  * </tr>
       
   194  * <tr class="altColor">
       
   195  * <td> {@link #ISO_DATE}</td>
       
   196  * <td> ISO Date with or without offset </td>
       
   197  * <td> '2011-12-03+01:00'; '2011-12-03'</td>
       
   198  * </tr>
       
   199  * <tr class="rowColor">
       
   200  * <td> {@link #ISO_LOCAL_TIME}</td>
       
   201  * <td> Time without offset </td>
       
   202  * <td>'10:15:30'</td>
       
   203  * </tr>
       
   204  * <tr class="altColor">
       
   205  * <td> {@link #ISO_OFFSET_TIME}</td>
       
   206  * <td> Time with offset </td>
       
   207  * <td>'10:15:30+01:00'</td>
       
   208  * </tr>
       
   209  * <tr class="rowColor">
       
   210  * <td> {@link #ISO_TIME}</td>
       
   211  * <td> Time with or without offset </td>
       
   212  * <td>'10:15:30+01:00'; '10:15:30'</td>
       
   213  * </tr>
       
   214  * <tr class="altColor">
       
   215  * <td> {@link #ISO_LOCAL_DATE_TIME}</td>
       
   216  * <td> ISO Local Date and Time </td>
       
   217  * <td>'2011-12-03T10:15:30'</td>
       
   218  * </tr>
       
   219  * <tr class="rowColor">
       
   220  * <td> {@link #ISO_OFFSET_DATE_TIME}</td>
       
   221  * <td> Date Time with Offset
       
   222  * </td><td>2011-12-03T10:15:30+01:00'</td>
       
   223  * </tr>
       
   224  * <tr class="altColor">
       
   225  * <td> {@link #ISO_ZONED_DATE_TIME}</td>
       
   226  * <td> Zoned Date Time </td>
       
   227  * <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td>
       
   228  * </tr>
       
   229  * <tr class="rowColor">
       
   230  * <td> {@link #ISO_DATE_TIME}</td>
       
   231  * <td> Date and time with ZoneId </td>
       
   232  * <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td>
       
   233  * </tr>
       
   234  * <tr class="altColor">
       
   235  * <td> {@link #ISO_ORDINAL_DATE}</td>
       
   236  * <td> Year and day of year </td>
       
   237  * <td>'2012-337'</td>
       
   238  * </tr>
       
   239  * <tr class="rowColor">
       
   240  * <td> {@link #ISO_WEEK_DATE}</td>
       
   241  * <td> Year and Week </td>
       
   242  * <td>2012-W48-6'</td></tr>
       
   243  * <tr class="altColor">
       
   244  * <td> {@link #ISO_INSTANT}</td>
       
   245  * <td> Date and Time of an Instant </td>
       
   246  * <td>'2011-12-03T10:15:30Z' </td>
       
   247  * </tr>
       
   248  * <tr class="rowColor">
       
   249  * <td> {@link #RFC_1123_DATE_TIME}</td>
       
   250  * <td> RFC 1123 / RFC 822 </td>
       
   251  * <td>'Tue, 3 Jun 2008 11:05:30 GMT'</td>
       
   252  * </tr>
       
   253  * </tbody>
       
   254  * </table>
       
   255  *
       
   256  * <h3 id="patterns">Patterns for Formatting and Parsing</h3>
       
   257  * Patterns are based on a simple sequence of letters and symbols.
       
   258  * A pattern is used to create a Formatter using the
       
   259  * {@link #ofPattern(String)} and {@link #ofPattern(String, Locale)} methods.
       
   260  * For example,
       
   261  * {@code "d MMM uuuu"} will format 2011-12-03 as '3&nbsp;Dec&nbsp;2011'.
       
   262  * A formatter created from a pattern can be used as many times as necessary,
       
   263  * it is immutable and is thread-safe.
       
   264  * <p>
       
   265  * For example:
       
   266  * <blockquote><pre>
       
   267  *  DateTimeFormatter formatter = DateTimeFormatter.pattern("yyyy MM dd");
       
   268  *  String text = date.toString(formatter);
       
   269  *  LocalDate date = LocalDate.parse(text, formatter);
       
   270  * </pre></blockquote>
       
   271  * <p>
       
   272  * All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The
       
   273  * following pattern letters are defined:
       
   274  * <pre>
       
   275  *  Symbol  Meaning                     Presentation      Examples
       
   276  *  ------  -------                     ------------      -------
       
   277  *   G       era                         text              AD; Anno Domini; A
       
   278  *   u       year                        year              2004; 04
       
   279  *   y       year-of-era                 year              2004; 04
       
   280  *   D       day-of-year                 number            189
       
   281  *   M/L     month-of-year               number/text       7; 07; Jul; July; J
       
   282  *   d       day-of-month                number            10
       
   283  *
       
   284  *   Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
       
   285  *   Y       week-based-year             year              1996; 96
       
   286  *   w       week-of-week-based-year     number            27
       
   287  *   W       week-of-month               number            4
       
   288  *   E       day-of-week                 text              Tue; Tuesday; T
       
   289  *   e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
       
   290  *   F       week-of-month               number            3
       
   291  *
       
   292  *   a       am-pm-of-day                text              PM
       
   293  *   h       clock-hour-of-am-pm (1-12)  number            12
       
   294  *   K       hour-of-am-pm (0-11)        number            0
       
   295  *   k       clock-hour-of-am-pm (1-24)  number            0
       
   296  *
       
   297  *   H       hour-of-day (0-23)          number            0
       
   298  *   m       minute-of-hour              number            30
       
   299  *   s       second-of-minute            number            55
       
   300  *   S       fraction-of-second          fraction          978
       
   301  *   A       milli-of-day                number            1234
       
   302  *   n       nano-of-second              number            987654321
       
   303  *   N       nano-of-day                 number            1234000000
       
   304  *
       
   305  *   V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
       
   306  *   z       time-zone name              zone-name         Pacific Standard Time; PST
       
   307  *   O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
       
   308  *   X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
       
   309  *   x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
       
   310  *   Z       zone-offset                 offset-Z          +0000; -0800; -08:00;
       
   311  *
       
   312  *   p       pad next                    pad modifier      1
       
   313  *
       
   314  *   '       escape for text             delimiter
       
   315  *   ''      single quote                literal           '
       
   316  *   [       optional section start
       
   317  *   ]       optional section end
       
   318  *   #       reserved for future use
       
   319  *   {       reserved for future use
       
   320  *   }       reserved for future use
   114  * </pre>
   321  * </pre>
   115  * Some aspects of formatting and parsing are dependent on the locale.
   322  * <p>
   116  * The locale can be changed using the {@link #withLocale(Locale)} method
   323  * The count of pattern letters determines the format.
   117  * which returns a new formatter in the requested locale.
   324  * <p>
   118  * <p>
   325  * <b>Text</b>: The text style is determined based on the number of pattern
   119  * Some applications may need to use the older {@link Format} class for formatting.
   326  * letters used. Less than 4 pattern letters will use the
   120  * The {@link #toFormat()} method returns an implementation of the old API.
   327  * {@link TextStyle#SHORT short form}. Exactly 4 pattern letters will use the
       
   328  * {@link TextStyle#FULL full form}. Exactly 5 pattern letters will use the
       
   329  * {@link TextStyle#NARROW narrow form}.
       
   330  * Pattern letters 'L', 'c', and 'q' specify the stand-alone form of the text styles.
       
   331  * <p>
       
   332  * <b>Number</b>: If the count of letters is one, then the value is output using
       
   333  * the minimum number of digits and without padding. Otherwise, the count of digits
       
   334  * is used as the width of the output field, with the value zero-padded as necessary.
       
   335  * The following pattern letters have constraints on the count of letters.
       
   336  * Only one letter of 'c' and 'F' can be specified.
       
   337  * Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified.
       
   338  * Up to three letters of 'D' can be specified.
       
   339  * <p>
       
   340  * <b>Number/Text</b>: If the count of pattern letters is 3 or greater, use the
       
   341  * Text rules above. Otherwise use the Number rules above.
       
   342  * <p>
       
   343  * <b>Fraction</b>: Outputs the nano-of-second field as a fraction-of-second.
       
   344  * The nano-of-second value has nine digits, thus the count of pattern letters
       
   345  * is from 1 to 9. If it is less than 9, then the nano-of-second value is
       
   346  * truncated, with only the most significant digits being output. When parsing
       
   347  * in strict mode, the number of parsed digits must match the count of pattern
       
   348  * letters. When parsing in lenient mode, the number of parsed digits must be at
       
   349  * least the count of pattern letters, up to 9 digits.
       
   350  * <p>
       
   351  * <b>Year</b>: The count of letters determines the minimum field width below
       
   352  * which padding is used. If the count of letters is two, then a
       
   353  * {@link DateTimeFormatterBuilder#appendValueReduced reduced} two digit form is
       
   354  * used. For printing, this outputs the rightmost two digits. For parsing, this
       
   355  * will parse using the base value of 2000, resulting in a year within the range
       
   356  * 2000 to 2099 inclusive. If the count of letters is less than four (but not
       
   357  * two), then the sign is only output for negative years as per
       
   358  * {@link SignStyle#NORMAL}. Otherwise, the sign is output if the pad width is
       
   359  * exceeded, as per {@link SignStyle#EXCEEDS_PAD}.
       
   360  * <p>
       
   361  * <b>ZoneId</b>: This outputs the time-zone ID, such as 'Europe/Paris'. If the
       
   362  * count of letters is two, then the time-zone ID is output. Any other count of
       
   363  * letters throws {@code IllegalArgumentException}.
       
   364  * <p>
       
   365  * <b>Zone names</b>: This outputs the display name of the time-zone ID. If the
       
   366  * count of letters is one, two or three, then the short name is output. If the
       
   367  * count of letters is four, then the full name is output. Five or more letters
       
   368  * throws {@code IllegalArgumentException}.
       
   369  * <p>
       
   370  * <b>Offset X and x</b>: This formats the offset based on the number of pattern
       
   371  * letters. One letter outputs just the hour, such as '+01', unless the minute
       
   372  * is non-zero in which case the minute is also output, such as '+0130'. Two
       
   373  * letters outputs the hour and minute, without a colon, such as '+0130'. Three
       
   374  * letters outputs the hour and minute, with a colon, such as '+01:30'. Four
       
   375  * letters outputs the hour and minute and optional second, without a colon,
       
   376  * such as '+013015'. Five letters outputs the hour and minute and optional
       
   377  * second, with a colon, such as '+01:30:15'. Six or more letters throws
       
   378  * {@code IllegalArgumentException}. Pattern letter 'X' (upper case) will output
       
   379  * 'Z' when the offset to be output would be zero, whereas pattern letter 'x'
       
   380  * (lower case) will output '+00', '+0000', or '+00:00'.
       
   381  * <p>
       
   382  * <b>Offset O</b>: This formats the localized offset based on the number of
       
   383  * pattern letters. One letter outputs the {@linkplain TextStyle#SHORT short}
       
   384  * form of the localized offset, which is localized offset text, such as 'GMT',
       
   385  * with hour without leading zero, optional 2-digit minute and second if
       
   386  * non-zero, and colon, for example 'GMT+8'. Four letters outputs the
       
   387  * {@linkplain TextStyle#FULL full} form, which is localized offset text,
       
   388  * such as 'GMT, with 2-digit hour and minute field, optional second field
       
   389  * if non-zero, and colon, for example 'GMT+08:00'. Any other count of letters
       
   390  * throws {@code IllegalArgumentException}.
       
   391  * <p>
       
   392  * <b>Offset Z</b>: This formats the offset based on the number of pattern
       
   393  * letters. One, two or three letters outputs the hour and minute, without a
       
   394  * colon, such as '+0130'. The output will be '+0000' when the offset is zero.
       
   395  * Four letters outputs the {@linkplain TextStyle#FULL full} form of localized
       
   396  * offset, equivalent to four letters of Offset-O. The output will be the
       
   397  * corresponding localized offset text if the offset is zero. Five
       
   398  * letters outputs the hour, minute, with optional second if non-zero, with
       
   399  * colon. It outputs 'Z' if the offset is zero.
       
   400  * Six or more letters throws {@code IllegalArgumentException}.
       
   401  * <p>
       
   402  * <b>Optional section</b>: The optional section markers work exactly like
       
   403  * calling {@link DateTimeFormatterBuilder#optionalStart()} and
       
   404  * {@link DateTimeFormatterBuilder#optionalEnd()}.
       
   405  * <p>
       
   406  * <b>Pad modifier</b>: Modifies the pattern that immediately follows to be
       
   407  * padded with spaces. The pad width is determined by the number of pattern
       
   408  * letters. This is the same as calling
       
   409  * {@link DateTimeFormatterBuilder#padNext(int)}.
       
   410  * <p>
       
   411  * For example, 'ppH' outputs the hour-of-day padded on the left with spaces to
       
   412  * a width of 2.
       
   413  * <p>
       
   414  * Any unrecognized letter is an error. Any non-letter character, other than
       
   415  * '[', ']', '{', '}', '#' and the single quote will be output directly.
       
   416  * Despite this, it is recommended to use single quotes around all characters
       
   417  * that you want to output directly to ensure that future changes do not break
       
   418  * your application.
   121  *
   419  *
   122  * <h3>Specification for implementors</h3>
   420  * <h3>Specification for implementors</h3>
   123  * This class is immutable and thread-safe.
   421  * This class is immutable and thread-safe.
   124  *
   422  *
   125  * @since 1.8
   423  * @since 1.8
   137     /**
   435     /**
   138      * The symbols to use for formatting, not null.
   436      * The symbols to use for formatting, not null.
   139      */
   437      */
   140     private final DateTimeFormatSymbols symbols;
   438     private final DateTimeFormatSymbols symbols;
   141     /**
   439     /**
       
   440      * The resolver style to use, not null.
       
   441      */
       
   442     private final ResolverStyle resolverStyle;
       
   443     /**
       
   444      * The fields to use in resolving, null for all fields.
       
   445      */
       
   446     private final Set<TemporalField> resolverFields;
       
   447     /**
   142      * The chronology to use for formatting, null for no override.
   448      * The chronology to use for formatting, null for no override.
   143      */
   449      */
   144     private final Chronology chrono;
   450     private final Chronology chrono;
   145     /**
   451     /**
   146      * The zone to use for formatting, null for no override.
   452      * The zone to use for formatting, null for no override.
   149 
   455 
   150     //-----------------------------------------------------------------------
   456     //-----------------------------------------------------------------------
   151     /**
   457     /**
   152      * Creates a formatter using the specified pattern.
   458      * Creates a formatter using the specified pattern.
   153      * <p>
   459      * <p>
   154      * This method will create a formatter based on a simple pattern of letters and symbols.
   460      * This method will create a formatter based on a simple
   155      * For example, {@code d MMM yyyy} will format 2011-12-03 as '3 Dec 2011'.
   461      * <a href="#patterns">pattern of letters and symbols</a>
   156      * <p>
   462      * as described in the class documentation.
   157      * The returned formatter will use the default locale, but this can be changed
   463      * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'.
   158      * using {@link DateTimeFormatter#withLocale(Locale)}.
   464      * <p>
   159      * <p>
   465      * The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}.
   160      * All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters.
   466      * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter
   161      * The following pattern letters are defined:
   467      * Alternatively use the {@link #ofPattern(String, Locale)} variant of this method.
   162      * <pre>
   468      * <p>
   163      *  Symbol  Meaning                     Presentation      Examples
   469      * The returned formatter has no override chronology or zone.
   164      *  ------  -------                     ------------      -------
   470      * It uses {@link ResolverStyle#SMART SMART} resolver style.
   165      *   G       era                         text              A; AD; Anno Domini
       
   166      *   y       year                        year              2004; 04
       
   167      *   D       day-of-year                 number            189
       
   168      *   M       month-of-year               number/text       7; 07; Jul; July; J
       
   169      *   d       day-of-month                number            10
       
   170      *
       
   171      *   Q       quarter-of-year             number/text       3; 03; Q3
       
   172      *   Y       week-based-year             year              1996; 96
       
   173      *   w       week-of-year                number            27
       
   174      *   W       week-of-month               number            27
       
   175      *   e       localized day-of-week       number            2; Tue; Tuesday; T
       
   176      *   E       day-of-week                 number/text       2; Tue; Tuesday; T
       
   177      *   F       week-of-month               number            3
       
   178      *
       
   179      *   a       am-pm-of-day                text              PM
       
   180      *   h       clock-hour-of-am-pm (1-12)  number            12
       
   181      *   K       hour-of-am-pm (0-11)        number            0
       
   182      *   k       clock-hour-of-am-pm (1-24)  number            0
       
   183      *
       
   184      *   H       hour-of-day (0-23)          number            0
       
   185      *   m       minute-of-hour              number            30
       
   186      *   s       second-of-minute            number            55
       
   187      *   S       fraction-of-second          fraction          978
       
   188      *   A       milli-of-day                number            1234
       
   189      *   n       nano-of-second              number            987654321
       
   190      *   N       nano-of-day                 number            1234000000
       
   191      *
       
   192      *   V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
       
   193      *   z       time-zone name              zone-name         Pacific Standard Time; PST
       
   194      *   X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
       
   195      *   x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
       
   196      *   Z       zone-offset                 offset-Z          +0000; -0800; -08:00;
       
   197      *
       
   198      *   p       pad next                    pad modifier      1
       
   199      *
       
   200      *   '       escape for text             delimiter
       
   201      *   ''      single quote                literal           '
       
   202      *   [       optional section start
       
   203      *   ]       optional section end
       
   204      *   {}      reserved for future use
       
   205      * </pre>
       
   206      * <p>
       
   207      * The count of pattern letters determine the format.
       
   208      * <p>
       
   209      * <b>Text</b>: The text style is determined based on the number of pattern letters used.
       
   210      * Less than 4 pattern letters will use the {@link TextStyle#SHORT short form}.
       
   211      * Exactly 4 pattern letters will use the {@link TextStyle#FULL full form}.
       
   212      * Exactly 5 pattern letters will use the {@link TextStyle#NARROW narrow form}.
       
   213      * <p>
       
   214      * <b>Number</b>: If the count of letters is one, then the value is output using the minimum number
       
   215      * of digits and without padding as per {@link DateTimeFormatterBuilder#appendValue(java.time.temporal.TemporalField)}.
       
   216      * Otherwise, the count of digits is used as the width of the output field as per
       
   217      * {@link DateTimeFormatterBuilder#appendValue(java.time.temporal.TemporalField, int)}.
       
   218      * <p>
       
   219      * <b>Number/Text</b>: If the count of pattern letters is 3 or greater, use the Text rules above.
       
   220      * Otherwise use the Number rules above.
       
   221      * <p>
       
   222      * <b>Fraction</b>: Outputs the nano-of-second field as a fraction-of-second.
       
   223      * The nano-of-second value has nine digits, thus the count of pattern letters is from 1 to 9.
       
   224      * If it is less than 9, then the nano-of-second value is truncated, with only the most
       
   225      * significant digits being output.
       
   226      * When parsing in strict mode, the number of parsed digits must match the count of pattern letters.
       
   227      * When parsing in lenient mode, the number of parsed digits must be at least the count of pattern
       
   228      * letters, up to 9 digits.
       
   229      * <p>
       
   230      * <b>Year</b>: The count of letters determines the minimum field width below which padding is used.
       
   231      * If the count of letters is two, then a {@link DateTimeFormatterBuilder#appendValueReduced reduced}
       
   232      * two digit form is used.
       
   233      * For printing, this outputs the rightmost two digits. For parsing, this will parse using the
       
   234      * base value of 2000, resulting in a year within the range 2000 to 2099 inclusive.
       
   235      * If the count of letters is less than four (but not two), then the sign is only output for negative
       
   236      * years as per {@link SignStyle#NORMAL}.
       
   237      * Otherwise, the sign is output if the pad width is exceeded, as per {@link SignStyle#EXCEEDS_PAD}
       
   238      * <p>
       
   239      * <b>ZoneId</b>: This outputs the time-zone ID, such as 'Europe/Paris'.
       
   240      * If the count of letters is two, then the time-zone ID is output.
       
   241      * Any other count of letters throws {@code IllegalArgumentException}.
       
   242      * <p>
       
   243      * <b>Zone names</b>: This outputs the display name of the time-zone ID.
       
   244      * If the count of letters is one, two or three, then the short name is output.
       
   245      * If the count of letters is four, then the full name is output.
       
   246      * Five or more letters throws {@code IllegalArgumentException}.
       
   247      * <p>
       
   248      * <b>Offset X and x</b>: This formats the offset based on the number of pattern letters.
       
   249      * One letter outputs just the hour', such as '+01', unless the minute is non-zero
       
   250      * in which case the minute is also output, such as '+0130'.
       
   251      * Two letters outputs the hour and minute, without a colon, such as '+0130'.
       
   252      * Three letters outputs the hour and minute, with a colon, such as '+01:30'.
       
   253      * Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'.
       
   254      * Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'.
       
   255      * Six or more letters throws {@code IllegalArgumentException}.
       
   256      * Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero,
       
   257      * whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.
       
   258      * <p>
       
   259      * <b>Offset Z</b>: This formats the offset based on the number of pattern letters.
       
   260      * One, two or three letters outputs the hour and minute, without a colon, such as '+0130'.
       
   261      * Four or more letters throws {@code IllegalArgumentException}.
       
   262      * The output will be '+0000' when the offset is zero.
       
   263      * <p>
       
   264      * <b>Optional section</b>: The optional section markers work exactly like calling
       
   265      * {@link DateTimeFormatterBuilder#optionalStart()} and {@link DateTimeFormatterBuilder#optionalEnd()}.
       
   266      * <p>
       
   267      * <b>Pad modifier</b>: Modifies the pattern that immediately follows to be padded with spaces.
       
   268      * The pad width is determined by the number of pattern letters.
       
   269      * This is the same as calling {@link DateTimeFormatterBuilder#padNext(int)}.
       
   270      * <p>
       
   271      * For example, 'ppH' outputs the hour-of-day padded on the left with spaces to a width of 2.
       
   272      * <p>
       
   273      * Any unrecognized letter is an error.
       
   274      * Any non-letter character, other than '[', ']', '{', '}' and the single quote will be output directly.
       
   275      * Despite this, it is recommended to use single quotes around all characters that you want to
       
   276      * output directly to ensure that future changes do not break your application.
       
   277      *
   471      *
   278      * @param pattern  the pattern to use, not null
   472      * @param pattern  the pattern to use, not null
   279      * @return the formatter based on the pattern, not null
   473      * @return the formatter based on the pattern, not null
   280      * @throws IllegalArgumentException if the pattern is invalid
   474      * @throws IllegalArgumentException if the pattern is invalid
   281      * @see DateTimeFormatterBuilder#appendPattern(String)
   475      * @see DateTimeFormatterBuilder#appendPattern(String)
   283     public static DateTimeFormatter ofPattern(String pattern) {
   477     public static DateTimeFormatter ofPattern(String pattern) {
   284         return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter();
   478         return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter();
   285     }
   479     }
   286 
   480 
   287     /**
   481     /**
   288      * Creates a formatter using the specified pattern.
   482      * Creates a formatter using the specified pattern and locale.
   289      * <p>
   483      * <p>
   290      * This method will create a formatter based on a simple pattern of letters and symbols.
   484      * This method will create a formatter based on a simple
   291      * For example, {@code d MMM yyyy} will format 2011-12-03 as '3 Dec 2011'.
   485      * <a href="#patterns">pattern of letters and symbols</a>
   292      * <p>
   486      * as described in the class documentation.
   293      * See {@link #ofPattern(String)} for details of the pattern.
   487      * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'.
   294      * <p>
   488      * <p>
   295      * The returned formatter will use the specified locale, but this can be changed
   489      * The formatter will use the specified locale.
   296      * using {@link DateTimeFormatter#withLocale(Locale)}.
   490      * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter
       
   491      * <p>
       
   492      * The returned formatter has no override chronology or zone.
       
   493      * It uses {@link ResolverStyle#SMART SMART} resolver style.
   297      *
   494      *
   298      * @param pattern  the pattern to use, not null
   495      * @param pattern  the pattern to use, not null
   299      * @param locale  the locale to use, not null
   496      * @param locale  the locale to use, not null
   300      * @return the formatter based on the pattern, not null
   497      * @return the formatter based on the pattern, not null
   301      * @throws IllegalArgumentException if the pattern is invalid
   498      * @throws IllegalArgumentException if the pattern is invalid
   305         return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale);
   502         return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale);
   306     }
   503     }
   307 
   504 
   308     //-----------------------------------------------------------------------
   505     //-----------------------------------------------------------------------
   309     /**
   506     /**
   310      * Returns a locale specific date format.
   507      * Returns a locale specific date format for the ISO chronology.
   311      * <p>
   508      * <p>
   312      * This returns a formatter that will format or parse a date.
   509      * This returns a formatter that will format or parse a date.
   313      * The exact format pattern used varies by locale.
   510      * The exact format pattern used varies by locale.
   314      * <p>
   511      * <p>
   315      * The locale is determined from the formatter. The formatter returned directly by
   512      * The locale is determined from the formatter. The formatter returned directly by
   318      * on the result of this method.
   515      * on the result of this method.
   319      * <p>
   516      * <p>
   320      * Note that the localized pattern is looked up lazily.
   517      * Note that the localized pattern is looked up lazily.
   321      * This {@code DateTimeFormatter} holds the style required and the locale,
   518      * This {@code DateTimeFormatter} holds the style required and the locale,
   322      * looking up the pattern required on demand.
   519      * looking up the pattern required on demand.
       
   520      * <p>
       
   521      * The returned formatter has a chronology of ISO set to ensure dates in
       
   522      * other calendar systems are correctly converted.
       
   523      * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
   323      *
   524      *
   324      * @param dateStyle  the formatter style to obtain, not null
   525      * @param dateStyle  the formatter style to obtain, not null
   325      * @return the date formatter, not null
   526      * @return the date formatter, not null
   326      */
   527      */
   327     public static DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle) {
   528     public static DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle) {
   328         Objects.requireNonNull(dateStyle, "dateStyle");
   529         Objects.requireNonNull(dateStyle, "dateStyle");
   329         return new DateTimeFormatterBuilder().appendLocalized(dateStyle, null).toFormatter();
   530         return new DateTimeFormatterBuilder().appendLocalized(dateStyle, null)
   330     }
   531                 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
   331 
   532     }
   332     /**
   533 
   333      * Returns a locale specific time format.
   534     /**
       
   535      * Returns a locale specific time format for the ISO chronology.
   334      * <p>
   536      * <p>
   335      * This returns a formatter that will format or parse a time.
   537      * This returns a formatter that will format or parse a time.
   336      * The exact format pattern used varies by locale.
   538      * The exact format pattern used varies by locale.
   337      * <p>
   539      * <p>
   338      * The locale is determined from the formatter. The formatter returned directly by
   540      * The locale is determined from the formatter. The formatter returned directly by
   341      * on the result of this method.
   543      * on the result of this method.
   342      * <p>
   544      * <p>
   343      * Note that the localized pattern is looked up lazily.
   545      * Note that the localized pattern is looked up lazily.
   344      * This {@code DateTimeFormatter} holds the style required and the locale,
   546      * This {@code DateTimeFormatter} holds the style required and the locale,
   345      * looking up the pattern required on demand.
   547      * looking up the pattern required on demand.
       
   548      * <p>
       
   549      * The returned formatter has a chronology of ISO set to ensure dates in
       
   550      * other calendar systems are correctly converted.
       
   551      * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
   346      *
   552      *
   347      * @param timeStyle  the formatter style to obtain, not null
   553      * @param timeStyle  the formatter style to obtain, not null
   348      * @return the time formatter, not null
   554      * @return the time formatter, not null
   349      */
   555      */
   350     public static DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle) {
   556     public static DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle) {
   351         Objects.requireNonNull(timeStyle, "timeStyle");
   557         Objects.requireNonNull(timeStyle, "timeStyle");
   352         return new DateTimeFormatterBuilder().appendLocalized(null, timeStyle).toFormatter();
   558         return new DateTimeFormatterBuilder().appendLocalized(null, timeStyle)
   353     }
   559                 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
   354 
   560     }
   355     /**
   561 
   356      * Returns a locale specific date-time formatter, which is typically of short length.
   562     /**
       
   563      * Returns a locale specific date-time formatter for the ISO chronology.
   357      * <p>
   564      * <p>
   358      * This returns a formatter that will format or parse a date-time.
   565      * This returns a formatter that will format or parse a date-time.
   359      * The exact format pattern used varies by locale.
   566      * The exact format pattern used varies by locale.
   360      * <p>
   567      * <p>
   361      * The locale is determined from the formatter. The formatter returned directly by
   568      * The locale is determined from the formatter. The formatter returned directly by
   364      * on the result of this method.
   571      * on the result of this method.
   365      * <p>
   572      * <p>
   366      * Note that the localized pattern is looked up lazily.
   573      * Note that the localized pattern is looked up lazily.
   367      * This {@code DateTimeFormatter} holds the style required and the locale,
   574      * This {@code DateTimeFormatter} holds the style required and the locale,
   368      * looking up the pattern required on demand.
   575      * looking up the pattern required on demand.
       
   576      * <p>
       
   577      * The returned formatter has a chronology of ISO set to ensure dates in
       
   578      * other calendar systems are correctly converted.
       
   579      * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
   369      *
   580      *
   370      * @param dateTimeStyle  the formatter style to obtain, not null
   581      * @param dateTimeStyle  the formatter style to obtain, not null
   371      * @return the date-time formatter, not null
   582      * @return the date-time formatter, not null
   372      */
   583      */
   373     public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateTimeStyle) {
   584     public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateTimeStyle) {
   374         Objects.requireNonNull(dateTimeStyle, "dateTimeStyle");
   585         Objects.requireNonNull(dateTimeStyle, "dateTimeStyle");
   375         return new DateTimeFormatterBuilder().appendLocalized(dateTimeStyle, dateTimeStyle).toFormatter();
   586         return new DateTimeFormatterBuilder().appendLocalized(dateTimeStyle, dateTimeStyle)
   376     }
   587                 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
   377 
   588     }
   378     /**
   589 
   379      * Returns a locale specific date and time format.
   590     /**
       
   591      * Returns a locale specific date and time format for the ISO chronology.
   380      * <p>
   592      * <p>
   381      * This returns a formatter that will format or parse a date-time.
   593      * This returns a formatter that will format or parse a date-time.
   382      * The exact format pattern used varies by locale.
   594      * The exact format pattern used varies by locale.
   383      * <p>
   595      * <p>
   384      * The locale is determined from the formatter. The formatter returned directly by
   596      * The locale is determined from the formatter. The formatter returned directly by
   387      * on the result of this method.
   599      * on the result of this method.
   388      * <p>
   600      * <p>
   389      * Note that the localized pattern is looked up lazily.
   601      * Note that the localized pattern is looked up lazily.
   390      * This {@code DateTimeFormatter} holds the style required and the locale,
   602      * This {@code DateTimeFormatter} holds the style required and the locale,
   391      * looking up the pattern required on demand.
   603      * looking up the pattern required on demand.
       
   604      * <p>
       
   605      * The returned formatter has a chronology of ISO set to ensure dates in
       
   606      * other calendar systems are correctly converted.
       
   607      * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
   392      *
   608      *
   393      * @param dateStyle  the date formatter style to obtain, not null
   609      * @param dateStyle  the date formatter style to obtain, not null
   394      * @param timeStyle  the time formatter style to obtain, not null
   610      * @param timeStyle  the time formatter style to obtain, not null
   395      * @return the date, time or date-time formatter, not null
   611      * @return the date, time or date-time formatter, not null
   396      */
   612      */
   397     public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle) {
   613     public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle) {
   398         Objects.requireNonNull(dateStyle, "dateStyle");
   614         Objects.requireNonNull(dateStyle, "dateStyle");
   399         Objects.requireNonNull(timeStyle, "timeStyle");
   615         Objects.requireNonNull(timeStyle, "timeStyle");
   400         return new DateTimeFormatterBuilder().appendLocalized(dateStyle, timeStyle).toFormatter();
   616         return new DateTimeFormatterBuilder().appendLocalized(dateStyle, timeStyle)
   401     }
   617                 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
   402 
   618     }
   403     //-----------------------------------------------------------------------
   619 
   404     /**
   620     //-----------------------------------------------------------------------
   405      * Returns the ISO date formatter that formats or parses a date without an offset,
   621     /**
   406      * such as '2011-12-03'.
   622      * The ISO date formatter that formats or parses a date without an
       
   623      * offset, such as '2011-12-03'.
   407      * <p>
   624      * <p>
   408      * This returns an immutable formatter capable of formatting and parsing
   625      * This returns an immutable formatter capable of formatting and parsing
   409      * the ISO-8601 extended local date format.
   626      * the ISO-8601 extended local date format.
   410      * The format consists of:
   627      * The format consists of:
   411      * <p><ul>
   628      * <p><ul>
   416      * <li>Two digits for the {@link ChronoField#MONTH_OF_YEAR month-of-year}.
   633      * <li>Two digits for the {@link ChronoField#MONTH_OF_YEAR month-of-year}.
   417      *  This is pre-padded by zero to ensure two digits.
   634      *  This is pre-padded by zero to ensure two digits.
   418      * <li>A dash
   635      * <li>A dash
   419      * <li>Two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}.
   636      * <li>Two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}.
   420      *  This is pre-padded by zero to ensure two digits.
   637      *  This is pre-padded by zero to ensure two digits.
   421      * </ul></p>
   638      * </ul>
       
   639      * <p>
       
   640      * The returned formatter has a chronology of ISO set to ensure dates in
       
   641      * other calendar systems are correctly converted.
       
   642      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   422      */
   643      */
   423     public static final DateTimeFormatter ISO_LOCAL_DATE;
   644     public static final DateTimeFormatter ISO_LOCAL_DATE;
   424     static {
   645     static {
   425         ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
   646         ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
   426             .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
   647                 .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
   427             .appendLiteral('-')
   648                 .appendLiteral('-')
   428             .appendValue(MONTH_OF_YEAR, 2)
   649                 .appendValue(MONTH_OF_YEAR, 2)
   429             .appendLiteral('-')
   650                 .appendLiteral('-')
   430             .appendValue(DAY_OF_MONTH, 2)
   651                 .appendValue(DAY_OF_MONTH, 2)
   431             .toFormatter();
   652                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   432     }
   653     }
   433 
   654 
   434     //-----------------------------------------------------------------------
   655     //-----------------------------------------------------------------------
   435     /**
   656     /**
   436      * Returns the ISO date formatter that formats or parses a date with an offset,
   657      * The ISO date formatter that formats or parses a date with an
   437      * such as '2011-12-03+01:00'.
   658      * offset, such as '2011-12-03+01:00'.
   438      * <p>
   659      * <p>
   439      * This returns an immutable formatter capable of formatting and parsing
   660      * This returns an immutable formatter capable of formatting and parsing
   440      * the ISO-8601 extended offset date format.
   661      * the ISO-8601 extended offset date format.
   441      * The format consists of:
   662      * The format consists of:
   442      * <p><ul>
   663      * <p><ul>
   443      * <li>The {@link #ISO_LOCAL_DATE}
   664      * <li>The {@link #ISO_LOCAL_DATE}
   444      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   665      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   445      *  they will be handled even though this is not part of the ISO-8601 standard.
   666      *  they will be handled even though this is not part of the ISO-8601 standard.
   446      *  Parsing is case insensitive.
   667      *  Parsing is case insensitive.
   447      * </ul></p>
   668      * </ul>
       
   669      * <p>
       
   670      * The returned formatter has a chronology of ISO set to ensure dates in
       
   671      * other calendar systems are correctly converted.
       
   672      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   448      */
   673      */
   449     public static final DateTimeFormatter ISO_OFFSET_DATE;
   674     public static final DateTimeFormatter ISO_OFFSET_DATE;
   450     static {
   675     static {
   451         ISO_OFFSET_DATE = new DateTimeFormatterBuilder()
   676         ISO_OFFSET_DATE = new DateTimeFormatterBuilder()
   452             .parseCaseInsensitive()
   677                 .parseCaseInsensitive()
   453             .append(ISO_LOCAL_DATE)
   678                 .append(ISO_LOCAL_DATE)
   454             .appendOffsetId()
   679                 .appendOffsetId()
   455             .toFormatter();
   680                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   456     }
   681     }
   457 
   682 
   458     //-----------------------------------------------------------------------
   683     //-----------------------------------------------------------------------
   459     /**
   684     /**
   460      * Returns the ISO date formatter that formats or parses a date with the
   685      * The ISO date formatter that formats or parses a date with the
   461      * offset if available, such as '2011-12-03' or '2011-12-03+01:00'.
   686      * offset if available, such as '2011-12-03' or '2011-12-03+01:00'.
   462      * <p>
   687      * <p>
   463      * This returns an immutable formatter capable of formatting and parsing
   688      * This returns an immutable formatter capable of formatting and parsing
   464      * the ISO-8601 extended date format.
   689      * the ISO-8601 extended date format.
   465      * The format consists of:
   690      * The format consists of:
   467      * <li>The {@link #ISO_LOCAL_DATE}
   692      * <li>The {@link #ISO_LOCAL_DATE}
   468      * <li>If the offset is not available then the format is complete.
   693      * <li>If the offset is not available then the format is complete.
   469      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   694      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   470      *  they will be handled even though this is not part of the ISO-8601 standard.
   695      *  they will be handled even though this is not part of the ISO-8601 standard.
   471      *  Parsing is case insensitive.
   696      *  Parsing is case insensitive.
   472      * </ul></p>
   697      * </ul>
   473      * <p>
   698      * <p>
   474      * As this formatter has an optional element, it may be necessary to parse using
   699      * As this formatter has an optional element, it may be necessary to parse using
   475      * {@link DateTimeFormatter#parseBest}.
   700      * {@link DateTimeFormatter#parseBest}.
       
   701      * <p>
       
   702      * The returned formatter has a chronology of ISO set to ensure dates in
       
   703      * other calendar systems are correctly converted.
       
   704      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   476      */
   705      */
   477     public static final DateTimeFormatter ISO_DATE;
   706     public static final DateTimeFormatter ISO_DATE;
   478     static {
   707     static {
   479         ISO_DATE = new DateTimeFormatterBuilder()
   708         ISO_DATE = new DateTimeFormatterBuilder()
   480             .parseCaseInsensitive()
   709                 .parseCaseInsensitive()
   481             .append(ISO_LOCAL_DATE)
   710                 .append(ISO_LOCAL_DATE)
   482             .optionalStart()
   711                 .optionalStart()
   483             .appendOffsetId()
   712                 .appendOffsetId()
   484             .toFormatter();
   713                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   485     }
   714     }
   486 
   715 
   487     //-----------------------------------------------------------------------
   716     //-----------------------------------------------------------------------
   488     /**
   717     /**
   489      * Returns the ISO time formatter that formats or parses a time without an offset,
   718      * The ISO time formatter that formats or parses a time without an
   490      * such as '10:15' or '10:15:30'.
   719      * offset, such as '10:15' or '10:15:30'.
   491      * <p>
   720      * <p>
   492      * This returns an immutable formatter capable of formatting and parsing
   721      * This returns an immutable formatter capable of formatting and parsing
   493      * the ISO-8601 extended local time format.
   722      * the ISO-8601 extended local time format.
   494      * The format consists of:
   723      * The format consists of:
   495      * <p><ul>
   724      * <p><ul>
   504      *  This is pre-padded by zero to ensure two digits.
   733      *  This is pre-padded by zero to ensure two digits.
   505      * <li>If the nano-of-second is zero or not available then the format is complete.
   734      * <li>If the nano-of-second is zero or not available then the format is complete.
   506      * <li>A decimal point
   735      * <li>A decimal point
   507      * <li>One to nine digits for the {@link ChronoField#NANO_OF_SECOND nano-of-second}.
   736      * <li>One to nine digits for the {@link ChronoField#NANO_OF_SECOND nano-of-second}.
   508      *  As many digits will be output as required.
   737      *  As many digits will be output as required.
   509      * </ul></p>
   738      * </ul>
       
   739      * <p>
       
   740      * The returned formatter has no override chronology or zone.
       
   741      * It uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   510      */
   742      */
   511     public static final DateTimeFormatter ISO_LOCAL_TIME;
   743     public static final DateTimeFormatter ISO_LOCAL_TIME;
   512     static {
   744     static {
   513         ISO_LOCAL_TIME = new DateTimeFormatterBuilder()
   745         ISO_LOCAL_TIME = new DateTimeFormatterBuilder()
   514             .appendValue(HOUR_OF_DAY, 2)
   746                 .appendValue(HOUR_OF_DAY, 2)
   515             .appendLiteral(':')
   747                 .appendLiteral(':')
   516             .appendValue(MINUTE_OF_HOUR, 2)
   748                 .appendValue(MINUTE_OF_HOUR, 2)
   517             .optionalStart()
   749                 .optionalStart()
   518             .appendLiteral(':')
   750                 .appendLiteral(':')
   519             .appendValue(SECOND_OF_MINUTE, 2)
   751                 .appendValue(SECOND_OF_MINUTE, 2)
   520             .optionalStart()
   752                 .optionalStart()
   521             .appendFraction(NANO_OF_SECOND, 0, 9, true)
   753                 .appendFraction(NANO_OF_SECOND, 0, 9, true)
   522             .toFormatter();
   754                 .toFormatter(ResolverStyle.STRICT, null);
   523     }
   755     }
   524 
   756 
   525     //-----------------------------------------------------------------------
   757     //-----------------------------------------------------------------------
   526     /**
   758     /**
   527      * Returns the ISO time formatter that formats or parses a time with an offset,
   759      * The ISO time formatter that formats or parses a time with an
   528      * such as '10:15+01:00' or '10:15:30+01:00'.
   760      * offset, such as '10:15+01:00' or '10:15:30+01:00'.
   529      * <p>
   761      * <p>
   530      * This returns an immutable formatter capable of formatting and parsing
   762      * This returns an immutable formatter capable of formatting and parsing
   531      * the ISO-8601 extended offset time format.
   763      * the ISO-8601 extended offset time format.
   532      * The format consists of:
   764      * The format consists of:
   533      * <p><ul>
   765      * <p><ul>
   534      * <li>The {@link #ISO_LOCAL_TIME}
   766      * <li>The {@link #ISO_LOCAL_TIME}
   535      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   767      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   536      *  they will be handled even though this is not part of the ISO-8601 standard.
   768      *  they will be handled even though this is not part of the ISO-8601 standard.
   537      *  Parsing is case insensitive.
   769      *  Parsing is case insensitive.
   538      * </ul></p>
   770      * </ul>
       
   771      * <p>
       
   772      * The returned formatter has no override chronology or zone.
       
   773      * It uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   539      */
   774      */
   540     public static final DateTimeFormatter ISO_OFFSET_TIME;
   775     public static final DateTimeFormatter ISO_OFFSET_TIME;
   541     static {
   776     static {
   542         ISO_OFFSET_TIME = new DateTimeFormatterBuilder()
   777         ISO_OFFSET_TIME = new DateTimeFormatterBuilder()
   543             .parseCaseInsensitive()
   778                 .parseCaseInsensitive()
   544             .append(ISO_LOCAL_TIME)
   779                 .append(ISO_LOCAL_TIME)
   545             .appendOffsetId()
   780                 .appendOffsetId()
   546             .toFormatter();
   781                 .toFormatter(ResolverStyle.STRICT, null);
   547     }
   782     }
   548 
   783 
   549     //-----------------------------------------------------------------------
   784     //-----------------------------------------------------------------------
   550     /**
   785     /**
   551      * Returns the ISO time formatter that formats or parses a time, with the
   786      * The ISO time formatter that formats or parses a time, with the
   552      * offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'.
   787      * offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'.
   553      * <p>
   788      * <p>
   554      * This returns an immutable formatter capable of formatting and parsing
   789      * This returns an immutable formatter capable of formatting and parsing
   555      * the ISO-8601 extended offset time format.
   790      * the ISO-8601 extended offset time format.
   556      * The format consists of:
   791      * The format consists of:
   558      * <li>The {@link #ISO_LOCAL_TIME}
   793      * <li>The {@link #ISO_LOCAL_TIME}
   559      * <li>If the offset is not available then the format is complete.
   794      * <li>If the offset is not available then the format is complete.
   560      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   795      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   561      *  they will be handled even though this is not part of the ISO-8601 standard.
   796      *  they will be handled even though this is not part of the ISO-8601 standard.
   562      *  Parsing is case insensitive.
   797      *  Parsing is case insensitive.
   563      * </ul></p>
   798      * </ul>
   564      * <p>
   799      * <p>
   565      * As this formatter has an optional element, it may be necessary to parse using
   800      * As this formatter has an optional element, it may be necessary to parse using
   566      * {@link DateTimeFormatter#parseBest}.
   801      * {@link DateTimeFormatter#parseBest}.
       
   802      * <p>
       
   803      * The returned formatter has no override chronology or zone.
       
   804      * It uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   567      */
   805      */
   568     public static final DateTimeFormatter ISO_TIME;
   806     public static final DateTimeFormatter ISO_TIME;
   569     static {
   807     static {
   570         ISO_TIME = new DateTimeFormatterBuilder()
   808         ISO_TIME = new DateTimeFormatterBuilder()
   571             .parseCaseInsensitive()
   809                 .parseCaseInsensitive()
   572             .append(ISO_LOCAL_TIME)
   810                 .append(ISO_LOCAL_TIME)
   573             .optionalStart()
   811                 .optionalStart()
   574             .appendOffsetId()
   812                 .appendOffsetId()
   575             .toFormatter();
   813                 .toFormatter(ResolverStyle.STRICT, null);
   576     }
   814     }
   577 
   815 
   578     //-----------------------------------------------------------------------
   816     //-----------------------------------------------------------------------
   579     /**
   817     /**
   580      * Returns the ISO date formatter that formats or parses a date-time
   818      * The ISO date-time formatter that formats or parses a date-time without
   581      * without an offset, such as '2011-12-03T10:15:30'.
   819      * an offset, such as '2011-12-03T10:15:30'.
   582      * <p>
   820      * <p>
   583      * This returns an immutable formatter capable of formatting and parsing
   821      * This returns an immutable formatter capable of formatting and parsing
   584      * the ISO-8601 extended offset date-time format.
   822      * the ISO-8601 extended offset date-time format.
   585      * The format consists of:
   823      * The format consists of:
   586      * <p><ul>
   824      * <p><ul>
   587      * <li>The {@link #ISO_LOCAL_DATE}
   825      * <li>The {@link #ISO_LOCAL_DATE}
   588      * <li>The letter 'T'. Parsing is case insensitive.
   826      * <li>The letter 'T'. Parsing is case insensitive.
   589      * <li>The {@link #ISO_LOCAL_TIME}
   827      * <li>The {@link #ISO_LOCAL_TIME}
   590      * </ul></p>
   828      * </ul>
       
   829      * <p>
       
   830      * The returned formatter has a chronology of ISO set to ensure dates in
       
   831      * other calendar systems are correctly converted.
       
   832      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   591      */
   833      */
   592     public static final DateTimeFormatter ISO_LOCAL_DATE_TIME;
   834     public static final DateTimeFormatter ISO_LOCAL_DATE_TIME;
   593     static {
   835     static {
   594         ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder()
   836         ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder()
   595             .parseCaseInsensitive()
   837                 .parseCaseInsensitive()
   596             .append(ISO_LOCAL_DATE)
   838                 .append(ISO_LOCAL_DATE)
   597             .appendLiteral('T')
   839                 .appendLiteral('T')
   598             .append(ISO_LOCAL_TIME)
   840                 .append(ISO_LOCAL_TIME)
   599             .toFormatter();
   841                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   600     }
   842     }
   601 
   843 
   602     //-----------------------------------------------------------------------
   844     //-----------------------------------------------------------------------
   603     /**
   845     /**
   604      * Returns the ISO date formatter that formats or parses a date-time
   846      * The ISO date-time formatter that formats or parses a date-time with an
   605      * with an offset, such as '2011-12-03T10:15:30+01:00'.
   847      * offset, such as '2011-12-03T10:15:30+01:00'.
   606      * <p>
   848      * <p>
   607      * This returns an immutable formatter capable of formatting and parsing
   849      * This returns an immutable formatter capable of formatting and parsing
   608      * the ISO-8601 extended offset date-time format.
   850      * the ISO-8601 extended offset date-time format.
   609      * The format consists of:
   851      * The format consists of:
   610      * <p><ul>
   852      * <p><ul>
   611      * <li>The {@link #ISO_LOCAL_DATE_TIME}
   853      * <li>The {@link #ISO_LOCAL_DATE_TIME}
   612      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   854      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   613      *  they will be handled even though this is not part of the ISO-8601 standard.
   855      *  they will be handled even though this is not part of the ISO-8601 standard.
   614      *  Parsing is case insensitive.
   856      *  Parsing is case insensitive.
   615      * </ul></p>
   857      * </ul>
       
   858      * <p>
       
   859      * The returned formatter has a chronology of ISO set to ensure dates in
       
   860      * other calendar systems are correctly converted.
       
   861      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   616      */
   862      */
   617     public static final DateTimeFormatter ISO_OFFSET_DATE_TIME;
   863     public static final DateTimeFormatter ISO_OFFSET_DATE_TIME;
   618     static {
   864     static {
   619         ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder()
   865         ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder()
   620             .parseCaseInsensitive()
   866                 .parseCaseInsensitive()
   621             .append(ISO_LOCAL_DATE_TIME)
   867                 .append(ISO_LOCAL_DATE_TIME)
   622             .appendOffsetId()
   868                 .appendOffsetId()
   623             .toFormatter();
   869                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   624     }
   870     }
   625 
   871 
   626     //-----------------------------------------------------------------------
   872     //-----------------------------------------------------------------------
   627     /**
   873     /**
   628      * Returns the ISO date formatter that formats or parses a date-time with
   874      * The ISO-like date-time formatter that formats or parses a date-time with
   629      * offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.
   875      * offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.
   630      * <p>
   876      * <p>
   631      * This returns an immutable formatter capable of formatting and parsing
   877      * This returns an immutable formatter capable of formatting and parsing
   632      * a format that extends the ISO-8601 extended offset date-time format
   878      * a format that extends the ISO-8601 extended offset date-time format
   633      * to add the time-zone.
   879      * to add the time-zone.
       
   880      * The section in square brackets is not part of the ISO-8601 standard.
   634      * The format consists of:
   881      * The format consists of:
   635      * <p><ul>
   882      * <p><ul>
   636      * <li>The {@link #ISO_OFFSET_DATE_TIME}
   883      * <li>The {@link #ISO_OFFSET_DATE_TIME}
   637      * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete.
   884      * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete.
   638      * <li>An open square bracket '['.
   885      * <li>An open square bracket '['.
   639      * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard.
   886      * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard.
   640      *  Parsing is case sensitive.
   887      *  Parsing is case sensitive.
   641      * <li>A close square bracket ']'.
   888      * <li>A close square bracket ']'.
   642      * </ul></p>
   889      * </ul>
       
   890      * <p>
       
   891      * The returned formatter has a chronology of ISO set to ensure dates in
       
   892      * other calendar systems are correctly converted.
       
   893      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   643      */
   894      */
   644     public static final DateTimeFormatter ISO_ZONED_DATE_TIME;
   895     public static final DateTimeFormatter ISO_ZONED_DATE_TIME;
   645     static {
   896     static {
   646         ISO_ZONED_DATE_TIME = new DateTimeFormatterBuilder()
   897         ISO_ZONED_DATE_TIME = new DateTimeFormatterBuilder()
   647             .append(ISO_OFFSET_DATE_TIME)
   898                 .append(ISO_OFFSET_DATE_TIME)
   648             .optionalStart()
   899                 .optionalStart()
   649             .appendLiteral('[')
   900                 .appendLiteral('[')
   650             .parseCaseSensitive()
   901                 .parseCaseSensitive()
   651             .appendZoneRegionId()
   902                 .appendZoneRegionId()
   652             .appendLiteral(']')
   903                 .appendLiteral(']')
   653             .toFormatter();
   904                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   654     }
   905     }
   655 
   906 
   656     //-----------------------------------------------------------------------
   907     //-----------------------------------------------------------------------
   657     /**
   908     /**
   658      * Returns the ISO date formatter that formats or parses a date-time
   909      * The ISO-like date-time formatter that formats or parses a date-time with
   659      * with the offset and zone if available, such as '2011-12-03T10:15:30',
   910      * the offset and zone if available, such as '2011-12-03T10:15:30',
   660      * '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'.
   911      * '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'.
   661      * <p>
   912      * <p>
   662      * This returns an immutable formatter capable of formatting and parsing
   913      * This returns an immutable formatter capable of formatting and parsing
   663      * the ISO-8601 extended offset date-time format.
   914      * the ISO-8601 extended local or offset date-time format, as well as the
       
   915      * extended non-ISO form specifying the time-zone.
   664      * The format consists of:
   916      * The format consists of:
   665      * <p><ul>
   917      * <p><ul>
   666      * <li>The {@link #ISO_LOCAL_DATE_TIME}
   918      * <li>The {@link #ISO_LOCAL_DATE_TIME}
   667      * <li>If the offset is not available to format or parse then the format is complete.
   919      * <li>If the offset is not available to format or parse then the format is complete.
   668      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   920      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   670      * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete.
   922      * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete.
   671      * <li>An open square bracket '['.
   923      * <li>An open square bracket '['.
   672      * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard.
   924      * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard.
   673      *  Parsing is case sensitive.
   925      *  Parsing is case sensitive.
   674      * <li>A close square bracket ']'.
   926      * <li>A close square bracket ']'.
   675      * </ul></p>
   927      * </ul>
   676      * <p>
   928      * <p>
   677      * As this formatter has an optional element, it may be necessary to parse using
   929      * As this formatter has an optional element, it may be necessary to parse using
   678      * {@link DateTimeFormatter#parseBest}.
   930      * {@link DateTimeFormatter#parseBest}.
       
   931      * <p>
       
   932      * The returned formatter has a chronology of ISO set to ensure dates in
       
   933      * other calendar systems are correctly converted.
       
   934      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   679      */
   935      */
   680     public static final DateTimeFormatter ISO_DATE_TIME;
   936     public static final DateTimeFormatter ISO_DATE_TIME;
   681     static {
   937     static {
   682         ISO_DATE_TIME = new DateTimeFormatterBuilder()
   938         ISO_DATE_TIME = new DateTimeFormatterBuilder()
   683             .append(ISO_LOCAL_DATE_TIME)
   939                 .append(ISO_LOCAL_DATE_TIME)
   684             .optionalStart()
   940                 .optionalStart()
   685             .appendOffsetId()
   941                 .appendOffsetId()
   686             .optionalStart()
   942                 .optionalStart()
   687             .appendLiteral('[')
   943                 .appendLiteral('[')
   688             .parseCaseSensitive()
   944                 .parseCaseSensitive()
   689             .appendZoneRegionId()
   945                 .appendZoneRegionId()
   690             .appendLiteral(']')
   946                 .appendLiteral(']')
   691             .toFormatter();
   947                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   692     }
   948     }
   693 
   949 
   694     //-----------------------------------------------------------------------
   950     //-----------------------------------------------------------------------
   695     /**
   951     /**
   696      * Returns the ISO date formatter that formats or parses the ordinal date
   952      * The ISO date formatter that formats or parses the ordinal date
   697      * without an offset, such as '2012-337'.
   953      * without an offset, such as '2012-337'.
   698      * <p>
   954      * <p>
   699      * This returns an immutable formatter capable of formatting and parsing
   955      * This returns an immutable formatter capable of formatting and parsing
   700      * the ISO-8601 extended ordinal date format.
   956      * the ISO-8601 extended ordinal date format.
   701      * The format consists of:
   957      * The format consists of:
   708      *  This is pre-padded by zero to ensure three digits.
   964      *  This is pre-padded by zero to ensure three digits.
   709      * <li>If the offset is not available to format or parse then the format is complete.
   965      * <li>If the offset is not available to format or parse then the format is complete.
   710      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   966      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   711      *  they will be handled even though this is not part of the ISO-8601 standard.
   967      *  they will be handled even though this is not part of the ISO-8601 standard.
   712      *  Parsing is case insensitive.
   968      *  Parsing is case insensitive.
   713      * </ul></p>
   969      * </ul>
   714      * <p>
   970      * <p>
   715      * As this formatter has an optional element, it may be necessary to parse using
   971      * As this formatter has an optional element, it may be necessary to parse using
   716      * {@link DateTimeFormatter#parseBest}.
   972      * {@link DateTimeFormatter#parseBest}.
       
   973      * <p>
       
   974      * The returned formatter has a chronology of ISO set to ensure dates in
       
   975      * other calendar systems are correctly converted.
       
   976      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   717      */
   977      */
   718     public static final DateTimeFormatter ISO_ORDINAL_DATE;
   978     public static final DateTimeFormatter ISO_ORDINAL_DATE;
   719     static {
   979     static {
   720         ISO_ORDINAL_DATE = new DateTimeFormatterBuilder()
   980         ISO_ORDINAL_DATE = new DateTimeFormatterBuilder()
   721             .parseCaseInsensitive()
   981                 .parseCaseInsensitive()
   722             .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
   982                 .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
   723             .appendLiteral('-')
   983                 .appendLiteral('-')
   724             .appendValue(DAY_OF_YEAR, 3)
   984                 .appendValue(DAY_OF_YEAR, 3)
   725             .optionalStart()
   985                 .optionalStart()
   726             .appendOffsetId()
   986                 .appendOffsetId()
   727             .toFormatter();
   987                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   728     }
   988     }
   729 
   989 
   730     //-----------------------------------------------------------------------
   990     //-----------------------------------------------------------------------
   731     /**
   991     /**
   732      * Returns the ISO date formatter that formats or parses the week-based date
   992      * The ISO date formatter that formats or parses the week-based date
   733      * without an offset, such as '2012-W48-6'.
   993      * without an offset, such as '2012-W48-6'.
   734      * <p>
   994      * <p>
   735      * This returns an immutable formatter capable of formatting and parsing
   995      * This returns an immutable formatter capable of formatting and parsing
   736      * the ISO-8601 extended week-based date format.
   996      * the ISO-8601 extended week-based date format.
   737      * The format consists of:
   997      * The format consists of:
   748      *  The value run from Monday (1) to Sunday (7).
  1008      *  The value run from Monday (1) to Sunday (7).
   749      * <li>If the offset is not available to format or parse then the format is complete.
  1009      * <li>If the offset is not available to format or parse then the format is complete.
   750      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
  1010      * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
   751      *  they will be handled even though this is not part of the ISO-8601 standard.
  1011      *  they will be handled even though this is not part of the ISO-8601 standard.
   752      *  Parsing is case insensitive.
  1012      *  Parsing is case insensitive.
   753      * </ul></p>
  1013      * </ul>
   754      * <p>
  1014      * <p>
   755      * As this formatter has an optional element, it may be necessary to parse using
  1015      * As this formatter has an optional element, it may be necessary to parse using
   756      * {@link DateTimeFormatter#parseBest}.
  1016      * {@link DateTimeFormatter#parseBest}.
       
  1017      * <p>
       
  1018      * The returned formatter has a chronology of ISO set to ensure dates in
       
  1019      * other calendar systems are correctly converted.
       
  1020      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   757      */
  1021      */
   758     public static final DateTimeFormatter ISO_WEEK_DATE;
  1022     public static final DateTimeFormatter ISO_WEEK_DATE;
   759     static {
  1023     static {
   760         ISO_WEEK_DATE = new DateTimeFormatterBuilder()
  1024         ISO_WEEK_DATE = new DateTimeFormatterBuilder()
   761             .parseCaseInsensitive()
  1025                 .parseCaseInsensitive()
   762             .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
  1026                 .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
   763             .appendLiteral("-W")
  1027                 .appendLiteral("-W")
   764             .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2)
  1028                 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2)
   765             .appendLiteral('-')
  1029                 .appendLiteral('-')
   766             .appendValue(DAY_OF_WEEK, 1)
  1030                 .appendValue(DAY_OF_WEEK, 1)
   767             .optionalStart()
  1031                 .optionalStart()
   768             .appendOffsetId()
  1032                 .appendOffsetId()
   769             .toFormatter();
  1033                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   770     }
  1034     }
   771 
  1035 
   772     //-----------------------------------------------------------------------
  1036     //-----------------------------------------------------------------------
   773     /**
  1037     /**
   774      * Returns the ISO instant formatter that formats or parses an instant in UTC.
  1038      * The ISO instant formatter that formats or parses an instant in UTC,
       
  1039      * such as '2011-12-03T10:15:30Z'.
   775      * <p>
  1040      * <p>
   776      * This returns an immutable formatter capable of formatting and parsing
  1041      * This returns an immutable formatter capable of formatting and parsing
   777      * the ISO-8601 instant format.
  1042      * the ISO-8601 instant format.
       
  1043      * <p>
       
  1044      * This is a special case formatter intended to allow a human readable form
       
  1045      * of an {@link java.time.Instant}. The {@code Instant} class is designed to
       
  1046      * only represent a point in time and internally stores a value in nanoseconds
       
  1047      * from a fixed epoch of 1970-01-01Z. As such, an {@code Instant} cannot be
       
  1048      * formatted as a date or time without providing some form of time-zone.
       
  1049      * This formatter allows the {@code Instant} to be formatted, by providing
       
  1050      * a suitable conversion using {@code ZoneOffset.UTC}.
       
  1051      * <p>
   778      * The format consists of:
  1052      * The format consists of:
   779      * <p><ul>
  1053      * <p><ul>
   780      * <li>The {@link #ISO_OFFSET_DATE_TIME} where the instant is converted from
  1054      * <li>The {@link #ISO_OFFSET_DATE_TIME} where the instant is converted from
   781      *  {@link ChronoField#INSTANT_SECONDS} and {@link ChronoField#NANO_OF_SECOND}
  1055      *  {@link ChronoField#INSTANT_SECONDS} and {@link ChronoField#NANO_OF_SECOND}
   782      *  using the {@code UTC} offset. Parsing is case insensitive.
  1056      *  using the {@code UTC} offset. Parsing is case insensitive.
   783      * </ul></p>
  1057      * </ul>
       
  1058      * <p>
       
  1059      * The returned formatter has no override chronology or zone.
       
  1060      * It uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   784      */
  1061      */
   785     public static final DateTimeFormatter ISO_INSTANT;
  1062     public static final DateTimeFormatter ISO_INSTANT;
   786     static {
  1063     static {
   787         ISO_INSTANT = new DateTimeFormatterBuilder()
  1064         ISO_INSTANT = new DateTimeFormatterBuilder()
   788             .parseCaseInsensitive()
  1065                 .parseCaseInsensitive()
   789             .appendInstant()
  1066                 .appendInstant()
   790             .toFormatter();
  1067                 .toFormatter(ResolverStyle.STRICT, null);
   791     }
  1068     }
   792 
  1069 
   793     //-----------------------------------------------------------------------
  1070     //-----------------------------------------------------------------------
   794     /**
  1071     /**
   795      * Returns the ISO date formatter that formats or parses a date without an offset,
  1072      * The ISO date formatter that formats or parses a date without an
   796      * such as '20111203'.
  1073      * offset, such as '20111203'.
   797      * <p>
  1074      * <p>
   798      * This returns an immutable formatter capable of formatting and parsing
  1075      * This returns an immutable formatter capable of formatting and parsing
   799      * the ISO-8601 basic local date format.
  1076      * the ISO-8601 basic local date format.
   800      * The format consists of:
  1077      * The format consists of:
   801      * <p><ul>
  1078      * <p><ul>
   807      *  This is pre-padded by zero to ensure two digits.
  1084      *  This is pre-padded by zero to ensure two digits.
   808      * <li>If the offset is not available to format or parse then the format is complete.
  1085      * <li>If the offset is not available to format or parse then the format is complete.
   809      * <li>The {@link ZoneOffset#getId() offset ID} without colons. If the offset has
  1086      * <li>The {@link ZoneOffset#getId() offset ID} without colons. If the offset has
   810      *  seconds then they will be handled even though this is not part of the ISO-8601 standard.
  1087      *  seconds then they will be handled even though this is not part of the ISO-8601 standard.
   811      *  Parsing is case insensitive.
  1088      *  Parsing is case insensitive.
   812      * </ul></p>
  1089      * </ul>
   813      * <p>
  1090      * <p>
   814      * As this formatter has an optional element, it may be necessary to parse using
  1091      * As this formatter has an optional element, it may be necessary to parse using
   815      * {@link DateTimeFormatter#parseBest}.
  1092      * {@link DateTimeFormatter#parseBest}.
       
  1093      * <p>
       
  1094      * The returned formatter has a chronology of ISO set to ensure dates in
       
  1095      * other calendar systems are correctly converted.
       
  1096      * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
   816      */
  1097      */
   817     public static final DateTimeFormatter BASIC_ISO_DATE;
  1098     public static final DateTimeFormatter BASIC_ISO_DATE;
   818     static {
  1099     static {
   819         BASIC_ISO_DATE = new DateTimeFormatterBuilder()
  1100         BASIC_ISO_DATE = new DateTimeFormatterBuilder()
   820             .parseCaseInsensitive()
  1101                 .parseCaseInsensitive()
   821             .appendValue(YEAR, 4)
  1102                 .appendValue(YEAR, 4)
   822             .appendValue(MONTH_OF_YEAR, 2)
  1103                 .appendValue(MONTH_OF_YEAR, 2)
   823             .appendValue(DAY_OF_MONTH, 2)
  1104                 .appendValue(DAY_OF_MONTH, 2)
   824             .optionalStart()
  1105                 .optionalStart()
   825             .appendOffset("+HHMMss", "Z")
  1106                 .appendOffset("+HHMMss", "Z")
   826             .toFormatter();
  1107                 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
   827     }
  1108     }
   828 
  1109 
   829     //-----------------------------------------------------------------------
  1110     //-----------------------------------------------------------------------
   830     /**
  1111     /**
   831      * The RFC-1123 date-time formatter, such as 'Tue, 3 Jun 2008 11:05:30 GMT'.
  1112      * The RFC-1123 date-time formatter, such as 'Tue, 3 Jun 2008 11:05:30 GMT'.
   860      * <li>Two digits for the {@link ChronoField#SECOND_OF_MINUTE second-of-minute}.
  1141      * <li>Two digits for the {@link ChronoField#SECOND_OF_MINUTE second-of-minute}.
   861      *  This is pre-padded by zero to ensure two digits.
  1142      *  This is pre-padded by zero to ensure two digits.
   862      * <li>A space
  1143      * <li>A space
   863      * <li>The {@link ZoneOffset#getId() offset ID} without colons or seconds.
  1144      * <li>The {@link ZoneOffset#getId() offset ID} without colons or seconds.
   864      *  An offset of zero uses "GMT". North American zone names and military zone names are not handled.
  1145      *  An offset of zero uses "GMT". North American zone names and military zone names are not handled.
   865      * </ul></p>
  1146      * </ul>
   866      * <p>
  1147      * <p>
   867      * Parsing is case insensitive.
  1148      * Parsing is case insensitive.
       
  1149      * <p>
       
  1150      * The returned formatter has a chronology of ISO set to ensure dates in
       
  1151      * other calendar systems are correctly converted.
       
  1152      * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
   868      */
  1153      */
   869     public static final DateTimeFormatter RFC_1123_DATE_TIME;
  1154     public static final DateTimeFormatter RFC_1123_DATE_TIME;
   870     static {
  1155     static {
   871         // manually code maps to ensure correct data always used
  1156         // manually code maps to ensure correct data always used
   872         // (locale data can be changed by application code)
  1157         // (locale data can be changed by application code)
   890         moy.put(9L, "Sep");
  1175         moy.put(9L, "Sep");
   891         moy.put(10L, "Oct");
  1176         moy.put(10L, "Oct");
   892         moy.put(11L, "Nov");
  1177         moy.put(11L, "Nov");
   893         moy.put(12L, "Dec");
  1178         moy.put(12L, "Dec");
   894         RFC_1123_DATE_TIME = new DateTimeFormatterBuilder()
  1179         RFC_1123_DATE_TIME = new DateTimeFormatterBuilder()
   895             .parseCaseInsensitive()
  1180                 .parseCaseInsensitive()
   896             .parseLenient()
  1181                 .parseLenient()
   897             .optionalStart()
  1182                 .optionalStart()
   898             .appendText(DAY_OF_WEEK, dow)
  1183                 .appendText(DAY_OF_WEEK, dow)
   899             .appendLiteral(", ")
  1184                 .appendLiteral(", ")
   900             .optionalEnd()
  1185                 .optionalEnd()
   901             .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
  1186                 .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
   902             .appendLiteral(' ')
  1187                 .appendLiteral(' ')
   903             .appendText(MONTH_OF_YEAR, moy)
  1188                 .appendText(MONTH_OF_YEAR, moy)
   904             .appendLiteral(' ')
  1189                 .appendLiteral(' ')
   905             .appendValue(YEAR, 4)  // 2 digit year not handled
  1190                 .appendValue(YEAR, 4)  // 2 digit year not handled
   906             .appendLiteral(' ')
  1191                 .appendLiteral(' ')
   907             .appendValue(HOUR_OF_DAY, 2)
  1192                 .appendValue(HOUR_OF_DAY, 2)
   908             .appendLiteral(':')
  1193                 .appendLiteral(':')
   909             .appendValue(MINUTE_OF_HOUR, 2)
  1194                 .appendValue(MINUTE_OF_HOUR, 2)
   910             .optionalStart()
  1195                 .optionalStart()
   911             .appendLiteral(':')
  1196                 .appendLiteral(':')
   912             .appendValue(SECOND_OF_MINUTE, 2)
  1197                 .appendValue(SECOND_OF_MINUTE, 2)
   913             .optionalEnd()
  1198                 .optionalEnd()
   914             .appendLiteral(' ')
  1199                 .appendLiteral(' ')
   915             .appendOffset("+HHMM", "GMT")  // should handle UT/Z/EST/EDT/CST/CDT/MST/MDT/PST/MDT
  1200                 .appendOffset("+HHMM", "GMT")  // should handle UT/Z/EST/EDT/CST/CDT/MST/MDT/PST/MDT
   916             .toFormatter();
  1201                 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
   917     }
  1202     }
   918 
  1203 
   919     /**
  1204     /**
   920      * Constructor.
  1205      * Constructor.
   921      *
  1206      *
   922      * @param printerParser  the printer/parser to use, not null
  1207      * @param printerParser  the printer/parser to use, not null
   923      * @param locale  the locale to use, not null
  1208      * @param locale  the locale to use, not null
   924      * @param symbols  the symbols to use, not null
  1209      * @param symbols  the symbols to use, not null
       
  1210      * @param resolverStyle  the resolver style to use, not null
       
  1211      * @param resolverFields  the fields to use during resolving, null for all fields
   925      * @param chrono  the chronology to use, null for no override
  1212      * @param chrono  the chronology to use, null for no override
   926      * @param zone  the zone to use, null for no override
  1213      * @param zone  the zone to use, null for no override
   927      */
  1214      */
   928     DateTimeFormatter(CompositePrinterParser printerParser, Locale locale,
  1215     DateTimeFormatter(CompositePrinterParser printerParser,
   929                       DateTimeFormatSymbols symbols, Chronology chrono, ZoneId zone) {
  1216             Locale locale, DateTimeFormatSymbols symbols,
       
  1217             ResolverStyle resolverStyle, Set<TemporalField> resolverFields,
       
  1218             Chronology chrono, ZoneId zone) {
   930         this.printerParser = Objects.requireNonNull(printerParser, "printerParser");
  1219         this.printerParser = Objects.requireNonNull(printerParser, "printerParser");
       
  1220         this.resolverFields = resolverFields;
   931         this.locale = Objects.requireNonNull(locale, "locale");
  1221         this.locale = Objects.requireNonNull(locale, "locale");
   932         this.symbols = Objects.requireNonNull(symbols, "symbols");
  1222         this.symbols = Objects.requireNonNull(symbols, "symbols");
       
  1223         this.resolverStyle = Objects.requireNonNull(resolverStyle, "resolverStyle");
   933         this.chrono = chrono;
  1224         this.chrono = chrono;
   934         this.zone = zone;
  1225         this.zone = zone;
   935     }
  1226     }
   936 
  1227 
   937     //-----------------------------------------------------------------------
  1228     //-----------------------------------------------------------------------
   960      */
  1251      */
   961     public DateTimeFormatter withLocale(Locale locale) {
  1252     public DateTimeFormatter withLocale(Locale locale) {
   962         if (this.locale.equals(locale)) {
  1253         if (this.locale.equals(locale)) {
   963             return this;
  1254             return this;
   964         }
  1255         }
   965         return new DateTimeFormatter(printerParser, locale, symbols, chrono, zone);
  1256         return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone);
   966     }
  1257     }
   967 
  1258 
   968     //-----------------------------------------------------------------------
  1259     //-----------------------------------------------------------------------
   969     /**
  1260     /**
   970      * Gets the set of symbols to be used during formatting.
  1261      * Gets the set of symbols to be used during formatting.
   985      */
  1276      */
   986     public DateTimeFormatter withSymbols(DateTimeFormatSymbols symbols) {
  1277     public DateTimeFormatter withSymbols(DateTimeFormatSymbols symbols) {
   987         if (this.symbols.equals(symbols)) {
  1278         if (this.symbols.equals(symbols)) {
   988             return this;
  1279             return this;
   989         }
  1280         }
   990         return new DateTimeFormatter(printerParser, locale, symbols, chrono, zone);
  1281         return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone);
   991     }
  1282     }
   992 
  1283 
   993     //-----------------------------------------------------------------------
  1284     //-----------------------------------------------------------------------
   994     /**
  1285     /**
   995      * Gets the overriding chronology to be used during formatting.
  1286      * Gets the overriding chronology to be used during formatting.
   996      * <p>
  1287      * <p>
   997      * This returns the override chronology, used to convert dates.
  1288      * This returns the override chronology, used to convert dates.
   998      * By default, a formatter has no override chronology, returning null.
  1289      * By default, a formatter has no override chronology, returning null.
   999      * See {@link #withChronology(Chronology)} for more details on overriding.
  1290      * See {@link #withChronology(Chronology)} for more details on overriding.
  1000      *
  1291      *
  1001      * @return the chronology of this formatter, null if no override
  1292      * @return the override chronology of this formatter, null if no override
  1002      */
  1293      */
  1003     public Chronology getChronology() {
  1294     public Chronology getChronology() {
  1004         return chrono;
  1295         return chrono;
  1005     }
  1296     }
  1006 
  1297 
  1011      * with the override chronology set.
  1302      * with the override chronology set.
  1012      * By default, a formatter has no override chronology, returning null.
  1303      * By default, a formatter has no override chronology, returning null.
  1013      * <p>
  1304      * <p>
  1014      * If an override is added, then any date that is formatted or parsed will be affected.
  1305      * If an override is added, then any date that is formatted or parsed will be affected.
  1015      * <p>
  1306      * <p>
  1016      * When formatting, if the {@code Temporal} object contains a date then it will
  1307      * When formatting, if the temporal object contains a date, then it will
  1017      * be converted to a date in the override chronology.
  1308      * be converted to a date in the override chronology.
  1018      * Any time or zone will be retained unless overridden.
  1309      * Whether the temporal contains a date is determined by querying the
  1019      * The converted result will behave in a manner equivalent to an implementation
  1310      * {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
  1020      * of {@code ChronoLocalDate},{@code ChronoLocalDateTime} or {@code ChronoZonedDateTime}.
  1311      * Any time or zone will be retained unaltered unless overridden.
  1021      * <p>
  1312      * <p>
  1022      * When parsing, the override chronology will be used to interpret the
  1313      * If the temporal object does not contain a date, but does contain one
  1023      * {@link java.time.temporal.ChronoField fields} into a date unless the
  1314      * or more {@code ChronoField} date fields, then a {@code DateTimeException}
  1024      * formatter directly parses a valid chronology.
  1315      * is thrown. In all other cases, the override chronology is added to the temporal,
       
  1316      * replacing any previous chronology, but without changing the date/time.
       
  1317      * <p>
       
  1318      * When parsing, there are two distinct cases to consider.
       
  1319      * If a chronology has been parsed directly from the text, perhaps because
       
  1320      * {@link DateTimeFormatterBuilder#appendChronologyId()} was used, then
       
  1321      * this override chronology has no effect.
       
  1322      * If no zone has been parsed, then this override chronology will be used
       
  1323      * to interpret the {@code ChronoField} values into a date according to the
       
  1324      * date resolving rules of the chronology.
  1025      * <p>
  1325      * <p>
  1026      * This instance is immutable and unaffected by this method call.
  1326      * This instance is immutable and unaffected by this method call.
  1027      *
  1327      *
  1028      * @param chrono  the new chronology, not null
  1328      * @param chrono  the new chronology, null if no override
  1029      * @return a formatter based on this formatter with the requested override chronology, not null
  1329      * @return a formatter based on this formatter with the requested override chronology, not null
  1030      */
  1330      */
  1031     public DateTimeFormatter withChronology(Chronology chrono) {
  1331     public DateTimeFormatter withChronology(Chronology chrono) {
  1032         if (Objects.equals(this.chrono, chrono)) {
  1332         if (Objects.equals(this.chrono, chrono)) {
  1033             return this;
  1333             return this;
  1034         }
  1334         }
  1035         return new DateTimeFormatter(printerParser, locale, symbols, chrono, zone);
  1335         return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone);
  1036     }
  1336     }
  1037 
  1337 
  1038     //-----------------------------------------------------------------------
  1338     //-----------------------------------------------------------------------
  1039     /**
  1339     /**
  1040      * Gets the overriding zone to be used during formatting.
  1340      * Gets the overriding zone to be used during formatting.
  1041      * <p>
  1341      * <p>
  1042      * This returns the override zone, used to convert instants.
  1342      * This returns the override zone, used to convert instants.
  1043      * By default, a formatter has no override zone, returning null.
  1343      * By default, a formatter has no override zone, returning null.
  1044      * See {@link #withZone(ZoneId)} for more details on overriding.
  1344      * See {@link #withZone(ZoneId)} for more details on overriding.
  1045      *
  1345      *
  1046      * @return the chronology of this formatter, null if no override
  1346      * @return the override zone of this formatter, null if no override
  1047      */
  1347      */
  1048     public ZoneId getZone() {
  1348     public ZoneId getZone() {
  1049         return zone;
  1349         return zone;
  1050     }
  1350     }
  1051 
  1351 
  1056      * with the override zone set.
  1356      * with the override zone set.
  1057      * By default, a formatter has no override zone, returning null.
  1357      * By default, a formatter has no override zone, returning null.
  1058      * <p>
  1358      * <p>
  1059      * If an override is added, then any instant that is formatted or parsed will be affected.
  1359      * If an override is added, then any instant that is formatted or parsed will be affected.
  1060      * <p>
  1360      * <p>
  1061      * When formatting, if the {@code Temporal} object contains an instant then it will
  1361      * When formatting, if the temporal object contains an instant, then it will
  1062      * be converted to a zoned date-time using the override zone.
  1362      * be converted to a zoned date-time using the override zone.
       
  1363      * Whether the temporal is an instant is determined by querying the
       
  1364      * {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS} field.
  1063      * If the input has a chronology then it will be retained unless overridden.
  1365      * If the input has a chronology then it will be retained unless overridden.
  1064      * If the input does not have a chronology, such as {@code Instant}, then
  1366      * If the input does not have a chronology, such as {@code Instant}, then
  1065      * the ISO chronology will be used.
  1367      * the ISO chronology will be used.
  1066      * The converted result will behave in a manner equivalent to an implementation
  1368      * <p>
  1067      * of {@code ChronoZonedDateTime}.
  1369      * If the temporal object does not contain an instant, but does contain
  1068      * <p>
  1370      * an offset then an additional check is made. If the normalized override
  1069      * When parsing, the override zone will be used to interpret the
  1371      * zone is an offset that differs from the offset of the temporal, then
  1070      * {@link java.time.temporal.ChronoField fields} into an instant unless the
  1372      * a {@code DateTimeException} is thrown. In all other cases, the override
  1071      * formatter directly parses a valid zone.
  1373      * zone is added to the temporal, replacing any previous zone, but without
       
  1374      * changing the date/time.
       
  1375      * <p>
       
  1376      * When parsing, there are two distinct cases to consider.
       
  1377      * If a zone has been parsed directly from the text, perhaps because
       
  1378      * {@link DateTimeFormatterBuilder#appendZoneId()} was used, then
       
  1379      * this override zone has no effect.
       
  1380      * If no zone has been parsed, then this override zone will be included in
       
  1381      * the result of the parse where it can be used to build instants and date-times.
  1072      * <p>
  1382      * <p>
  1073      * This instance is immutable and unaffected by this method call.
  1383      * This instance is immutable and unaffected by this method call.
  1074      *
  1384      *
  1075      * @param zone  the new override zone, not null
  1385      * @param zone  the new override zone, null if no override
  1076      * @return a formatter based on this formatter with the requested override zone, not null
  1386      * @return a formatter based on this formatter with the requested override zone, not null
  1077      */
  1387      */
  1078     public DateTimeFormatter withZone(ZoneId zone) {
  1388     public DateTimeFormatter withZone(ZoneId zone) {
  1079         if (Objects.equals(this.zone, zone)) {
  1389         if (Objects.equals(this.zone, zone)) {
  1080             return this;
  1390             return this;
  1081         }
  1391         }
  1082         return new DateTimeFormatter(printerParser, locale, symbols, chrono, zone);
  1392         return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone);
       
  1393     }
       
  1394 
       
  1395     //-----------------------------------------------------------------------
       
  1396     /**
       
  1397      * Gets the resolver style to use during parsing.
       
  1398      * <p>
       
  1399      * This returns the resolver style, used during the second phase of parsing
       
  1400      * when fields are resolved into dates and times.
       
  1401      * By default, a formatter has the {@link ResolverStyle#SMART SMART} resolver style.
       
  1402      * See {@link #withResolverStyle(ResolverStyle)} for more details.
       
  1403      *
       
  1404      * @return the resolver style of this formatter, not null
       
  1405      */
       
  1406     public ResolverStyle getResolverStyle() {
       
  1407         return resolverStyle;
       
  1408     }
       
  1409 
       
  1410     /**
       
  1411      * Returns a copy of this formatter with a new resolver style.
       
  1412      * <p>
       
  1413      * This returns a formatter with similar state to this formatter but
       
  1414      * with the resolver style set. By default, a formatter has the
       
  1415      * {@link ResolverStyle#SMART SMART} resolver style.
       
  1416      * <p>
       
  1417      * Changing the resolver style only has an effect during parsing.
       
  1418      * Parsing a text string occurs in two phases.
       
  1419      * Phase 1 is a basic text parse according to the fields added to the builder.
       
  1420      * Phase 2 resolves the parsed field-value pairs into date and/or time objects.
       
  1421      * The resolver style is used to control how phase 2, resolving, happens.
       
  1422      * See {@code ResolverStyle} for more information on the options available.
       
  1423      * <p>
       
  1424      * This instance is immutable and unaffected by this method call.
       
  1425      *
       
  1426      * @param resolverStyle  the new resolver style, not null
       
  1427      * @return a formatter based on this formatter with the requested resolver style, not null
       
  1428      */
       
  1429     public DateTimeFormatter withResolverStyle(ResolverStyle resolverStyle) {
       
  1430         Objects.requireNonNull(resolverStyle, "resolverStyle");
       
  1431         if (Objects.equals(this.resolverStyle, resolverStyle)) {
       
  1432             return this;
       
  1433         }
       
  1434         return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone);
       
  1435     }
       
  1436 
       
  1437     //-----------------------------------------------------------------------
       
  1438     /**
       
  1439      * Gets the resolver fields to use during parsing.
       
  1440      * <p>
       
  1441      * This returns the resolver fields, used during the second phase of parsing
       
  1442      * when fields are resolved into dates and times.
       
  1443      * By default, a formatter has no resolver fields, and thus returns null.
       
  1444      * See {@link #withResolverFields(Set)} for more details.
       
  1445      *
       
  1446      * @return the immutable set of resolver fields of this formatter, null if no fields
       
  1447      */
       
  1448     public Set<TemporalField> getResolverFields() {
       
  1449         return resolverFields;
       
  1450     }
       
  1451 
       
  1452     /**
       
  1453      * Returns a copy of this formatter with a new set of resolver fields.
       
  1454      * <p>
       
  1455      * This returns a formatter with similar state to this formatter but with
       
  1456      * the resolver fields set. By default, a formatter has no resolver fields.
       
  1457      * <p>
       
  1458      * Changing the resolver fields only has an effect during parsing.
       
  1459      * Parsing a text string occurs in two phases.
       
  1460      * Phase 1 is a basic text parse according to the fields added to the builder.
       
  1461      * Phase 2 resolves the parsed field-value pairs into date and/or time objects.
       
  1462      * The resolver fields are used to filter the field-value pairs between phase 1 and 2.
       
  1463      * <p>
       
  1464      * This can be used to select between two or more ways that a date or time might
       
  1465      * be resolved. For example, if the formatter consists of year, month, day-of-month
       
  1466      * and day-of-year, then there are two ways to resolve a date.
       
  1467      * Calling this method with the arguments {@link ChronoField#YEAR YEAR} and
       
  1468      * {@link ChronoField#DAY_OF_YEAR DAY_OF_YEAR} will ensure that the date is
       
  1469      * resolved using the year and day-of-year, effectively meaning that the month
       
  1470      * and day-of-month are ignored during the resolving phase.
       
  1471      * <p>
       
  1472      * In a similar manner, this method can be used to ignore secondary fields that
       
  1473      * would otherwise be cross-checked. For example, if the formatter consists of year,
       
  1474      * month, day-of-month and day-of-week, then there is only one way to resolve a
       
  1475      * date, but the parsed value for day-of-week will be cross-checked against the
       
  1476      * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR},
       
  1477      * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and
       
  1478      * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is
       
  1479      * resolved correctly, but without any cross-check for the day-of-week.
       
  1480      * <p>
       
  1481      * In implementation terms, this method behaves as follows. The result of the
       
  1482      * parsing phase can be considered to be a map of field to value. The behavior
       
  1483      * of this method is to cause that map to be filtered between phase 1 and 2,
       
  1484      * removing all fields other than those specified as arguments to this method.
       
  1485      * <p>
       
  1486      * This instance is immutable and unaffected by this method call.
       
  1487      *
       
  1488      * @param resolverFields  the new set of resolver fields, null if no fields
       
  1489      * @return a formatter based on this formatter with the requested resolver style, not null
       
  1490      */
       
  1491     public DateTimeFormatter withResolverFields(TemporalField... resolverFields) {
       
  1492         Objects.requireNonNull(resolverFields, "resolverFields");
       
  1493         Set<TemporalField> fields = new HashSet<>(Arrays.asList(resolverFields));
       
  1494         if (Objects.equals(this.resolverFields, fields)) {
       
  1495             return this;
       
  1496         }
       
  1497         fields = Collections.unmodifiableSet(fields);
       
  1498         return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, fields, chrono, zone);
       
  1499     }
       
  1500 
       
  1501     /**
       
  1502      * Returns a copy of this formatter with a new set of resolver fields.
       
  1503      * <p>
       
  1504      * This returns a formatter with similar state to this formatter but with
       
  1505      * the resolver fields set. By default, a formatter has no resolver fields.
       
  1506      * <p>
       
  1507      * Changing the resolver fields only has an effect during parsing.
       
  1508      * Parsing a text string occurs in two phases.
       
  1509      * Phase 1 is a basic text parse according to the fields added to the builder.
       
  1510      * Phase 2 resolves the parsed field-value pairs into date and/or time objects.
       
  1511      * The resolver fields are used to filter the field-value pairs between phase 1 and 2.
       
  1512      * <p>
       
  1513      * This can be used to select between two or more ways that a date or time might
       
  1514      * be resolved. For example, if the formatter consists of year, month, day-of-month
       
  1515      * and day-of-year, then there are two ways to resolve a date.
       
  1516      * Calling this method with the arguments {@link ChronoField#YEAR YEAR} and
       
  1517      * {@link ChronoField#DAY_OF_YEAR DAY_OF_YEAR} will ensure that the date is
       
  1518      * resolved using the year and day-of-year, effectively meaning that the month
       
  1519      * and day-of-month are ignored during the resolving phase.
       
  1520      * <p>
       
  1521      * In a similar manner, this method can be used to ignore secondary fields that
       
  1522      * would otherwise be cross-checked. For example, if the formatter consists of year,
       
  1523      * month, day-of-month and day-of-week, then there is only one way to resolve a
       
  1524      * date, but the parsed value for day-of-week will be cross-checked against the
       
  1525      * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR},
       
  1526      * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and
       
  1527      * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is
       
  1528      * resolved correctly, but without any cross-check for the day-of-week.
       
  1529      * <p>
       
  1530      * In implementation terms, this method behaves as follows. The result of the
       
  1531      * parsing phase can be considered to be a map of field to value. The behavior
       
  1532      * of this method is to cause that map to be filtered between phase 1 and 2,
       
  1533      * removing all fields other than those specified as arguments to this method.
       
  1534      * <p>
       
  1535      * This instance is immutable and unaffected by this method call.
       
  1536      *
       
  1537      * @param resolverFields  the new set of resolver fields, null if no fields
       
  1538      * @return a formatter based on this formatter with the requested resolver style, not null
       
  1539      */
       
  1540     public DateTimeFormatter withResolverFields(Set<TemporalField> resolverFields) {
       
  1541         Objects.requireNonNull(resolverFields, "resolverFields");
       
  1542         if (Objects.equals(this.resolverFields, resolverFields)) {
       
  1543             return this;
       
  1544         }
       
  1545         resolverFields = Collections.unmodifiableSet(new HashSet<>(resolverFields));
       
  1546         return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone);
  1083     }
  1547     }
  1084 
  1548 
  1085     //-----------------------------------------------------------------------
  1549     //-----------------------------------------------------------------------
  1086     /**
  1550     /**
  1087      * Formats a date-time object using this formatter.
  1551      * Formats a date-time object using this formatter.
  1149      * @throws DateTimeParseException if unable to parse the requested result
  1613      * @throws DateTimeParseException if unable to parse the requested result
  1150      */
  1614      */
  1151     public TemporalAccessor parse(CharSequence text) {
  1615     public TemporalAccessor parse(CharSequence text) {
  1152         Objects.requireNonNull(text, "text");
  1616         Objects.requireNonNull(text, "text");
  1153         try {
  1617         try {
  1154             return parseToBuilder(text, null).resolve();
  1618             return parseResolved0(text, null);
  1155         } catch (DateTimeParseException ex) {
  1619         } catch (DateTimeParseException ex) {
  1156             throw ex;
  1620             throw ex;
  1157         } catch (RuntimeException ex) {
  1621         } catch (RuntimeException ex) {
  1158             throw createError(text, ex);
  1622             throw createError(text, ex);
  1159         }
  1623         }
  1191      */
  1655      */
  1192     public TemporalAccessor parse(CharSequence text, ParsePosition position) {
  1656     public TemporalAccessor parse(CharSequence text, ParsePosition position) {
  1193         Objects.requireNonNull(text, "text");
  1657         Objects.requireNonNull(text, "text");
  1194         Objects.requireNonNull(position, "position");
  1658         Objects.requireNonNull(position, "position");
  1195         try {
  1659         try {
  1196             return parseToBuilder(text, position).resolve();
  1660             return parseResolved0(text, position);
  1197         } catch (DateTimeParseException | IndexOutOfBoundsException ex) {
  1661         } catch (DateTimeParseException | IndexOutOfBoundsException ex) {
  1198             throw ex;
  1662             throw ex;
  1199         } catch (RuntimeException ex) {
  1663         } catch (RuntimeException ex) {
  1200             throw createError(text, ex);
  1664             throw createError(text, ex);
  1201         }
  1665         }
  1223      */
  1687      */
  1224     public <T> T parse(CharSequence text, TemporalQuery<T> query) {
  1688     public <T> T parse(CharSequence text, TemporalQuery<T> query) {
  1225         Objects.requireNonNull(text, "text");
  1689         Objects.requireNonNull(text, "text");
  1226         Objects.requireNonNull(query, "query");
  1690         Objects.requireNonNull(query, "query");
  1227         try {
  1691         try {
  1228             DateTimeBuilder builder = parseToBuilder(text, null).resolve();
  1692             return parseResolved0(text, null).query(query);
  1229             return builder.query(query);
       
  1230         } catch (DateTimeParseException ex) {
  1693         } catch (DateTimeParseException ex) {
  1231             throw ex;
  1694             throw ex;
  1232         } catch (RuntimeException ex) {
  1695         } catch (RuntimeException ex) {
  1233             throw createError(text, ex);
  1696             throw createError(text, ex);
  1234         }
  1697         }
  1236 
  1699 
  1237     /**
  1700     /**
  1238      * Fully parses the text producing an object of one of the specified types.
  1701      * Fully parses the text producing an object of one of the specified types.
  1239      * <p>
  1702      * <p>
  1240      * This parse method is convenient for use when the parser can handle optional elements.
  1703      * This parse method is convenient for use when the parser can handle optional elements.
  1241      * For example, a pattern of 'yyyy-MM-dd HH.mm[Z]]' can be fully parsed to a {@code ZonedDateTime},
  1704      * For example, a pattern of 'uuuu-MM-dd HH.mm[ VV]' can be fully parsed to a {@code ZonedDateTime},
  1242      * or partially parsed to a {@code LocalDateTime}.
  1705      * or partially parsed to a {@code LocalDateTime}.
  1243      * The queries must be specified in order, starting from the best matching full-parse option
  1706      * The queries must be specified in order, starting from the best matching full-parse option
  1244      * and ending with the worst matching minimal parse option.
  1707      * and ending with the worst matching minimal parse option.
  1245      * The query is typically a method reference to a {@code from(TemporalAccessor)} method.
  1708      * The query is typically a method reference to a {@code from(TemporalAccessor)} method.
  1246      * <p>
  1709      * <p>
  1270         Objects.requireNonNull(queries, "queries");
  1733         Objects.requireNonNull(queries, "queries");
  1271         if (queries.length < 2) {
  1734         if (queries.length < 2) {
  1272             throw new IllegalArgumentException("At least two queries must be specified");
  1735             throw new IllegalArgumentException("At least two queries must be specified");
  1273         }
  1736         }
  1274         try {
  1737         try {
  1275             DateTimeBuilder builder = parseToBuilder(text, null).resolve();
  1738             TemporalAccessor resolved = parseResolved0(text, null);
  1276             for (TemporalQuery<?> query : queries) {
  1739             for (TemporalQuery<?> query : queries) {
  1277                 try {
  1740                 try {
  1278                     return (TemporalAccessor) builder.query(query);
  1741                     return (TemporalAccessor) resolved.query(query);
  1279                 } catch (RuntimeException ex) {
  1742                 } catch (RuntimeException ex) {
  1280                     // continue
  1743                     // continue
  1281                 }
  1744                 }
  1282             }
  1745             }
  1283             throw new DateTimeException("Unable to convert parsed text using any of the specified queries");
  1746             throw new DateTimeException("Unable to convert parsed text using any of the specified queries");
  1287             throw createError(text, ex);
  1750             throw createError(text, ex);
  1288         }
  1751         }
  1289     }
  1752     }
  1290 
  1753 
  1291     private DateTimeParseException createError(CharSequence text, RuntimeException ex) {
  1754     private DateTimeParseException createError(CharSequence text, RuntimeException ex) {
  1292         String abbr = "";
  1755         String abbr;
  1293         if (text.length() > 64) {
  1756         if (text.length() > 64) {
  1294             abbr = text.subSequence(0, 64).toString() + "...";
  1757             abbr = text.subSequence(0, 64).toString() + "...";
  1295         } else {
  1758         } else {
  1296             abbr = text.toString();
  1759             abbr = text.toString();
  1297         }
  1760         }
  1298         return new DateTimeParseException("Text '" + abbr + "' could not be parsed: " + ex.getMessage(), text, 0, ex);
  1761         return new DateTimeParseException("Text '" + abbr + "' could not be parsed: " + ex.getMessage(), text, 0, ex);
  1299     }
  1762     }
  1300 
  1763 
  1301     //-----------------------------------------------------------------------
  1764     //-----------------------------------------------------------------------
  1302     /**
  1765     /**
  1303      * Parses the text to a builder.
  1766      * Parses and resolves the specified text.
  1304      * <p>
  1767      * <p>
  1305      * This parses to a {@code DateTimeBuilder} ensuring that the text is fully parsed.
  1768      * This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed.
  1306      * This method throws {@link DateTimeParseException} if unable to parse, or
       
  1307      * some other {@code DateTimeException} if another date/time problem occurs.
       
  1308      *
  1769      *
  1309      * @param text  the text to parse, not null
  1770      * @param text  the text to parse, not null
  1310      * @param position  the position to parse from, updated with length parsed
  1771      * @param position  the position to parse from, updated with length parsed
  1311      *  and the index of any error, null if parsing whole string
  1772      *  and the index of any error, null if parsing whole string
  1312      * @return the engine representing the result of the parse, not null
  1773      * @return the resolved result of the parse, not null
  1313      * @throws DateTimeParseException if the parse fails
  1774      * @throws DateTimeParseException if the parse fails
  1314      */
  1775      * @throws DateTimeException if an error occurs while resolving the date or time
  1315     private DateTimeBuilder parseToBuilder(final CharSequence text, final ParsePosition position) {
  1776      * @throws IndexOutOfBoundsException if the position is invalid
       
  1777      */
       
  1778     private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) {
  1316         ParsePosition pos = (position != null ? position : new ParsePosition(0));
  1779         ParsePosition pos = (position != null ? position : new ParsePosition(0));
  1317         DateTimeParseContext result = parseUnresolved0(text, pos);
  1780         Parsed unresolved = parseUnresolved0(text, pos);
  1318         if (result == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
  1781         if (unresolved == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
  1319             String abbr = "";
  1782             String abbr;
  1320             if (text.length() > 64) {
  1783             if (text.length() > 64) {
  1321                 abbr = text.subSequence(0, 64).toString() + "...";
  1784                 abbr = text.subSequence(0, 64).toString() + "...";
  1322             } else {
  1785             } else {
  1323                 abbr = text.toString();
  1786                 abbr = text.toString();
  1324             }
  1787             }
  1328             } else {
  1791             } else {
  1329                 throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " +
  1792                 throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " +
  1330                         pos.getIndex(), text, pos.getIndex());
  1793                         pos.getIndex(), text, pos.getIndex());
  1331             }
  1794             }
  1332         }
  1795         }
  1333         return result.resolveFields().toBuilder();
  1796         return unresolved.resolve(resolverStyle, resolverFields);
  1334     }
  1797     }
  1335 
  1798 
  1336     /**
  1799     /**
  1337      * Parses the text using this formatter, without resolving the result, intended
  1800      * Parses the text using this formatter, without resolving the result, intended
  1338      * for advanced use cases.
  1801      * for advanced use cases.
  1374      */
  1837      */
  1375     public TemporalAccessor parseUnresolved(CharSequence text, ParsePosition position) {
  1838     public TemporalAccessor parseUnresolved(CharSequence text, ParsePosition position) {
  1376         return parseUnresolved0(text, position);
  1839         return parseUnresolved0(text, position);
  1377     }
  1840     }
  1378 
  1841 
  1379     private DateTimeParseContext parseUnresolved0(CharSequence text, ParsePosition position) {
  1842     private Parsed parseUnresolved0(CharSequence text, ParsePosition position) {
  1380         Objects.requireNonNull(text, "text");
  1843         Objects.requireNonNull(text, "text");
  1381         Objects.requireNonNull(position, "position");
  1844         Objects.requireNonNull(position, "position");
  1382         DateTimeParseContext context = new DateTimeParseContext(this);
  1845         DateTimeParseContext context = new DateTimeParseContext(this);
  1383         int pos = position.getIndex();
  1846         int pos = position.getIndex();
  1384         pos = printerParser.parse(context, text, pos);
  1847         pos = printerParser.parse(context, text, pos);
  1385         if (pos < 0) {
  1848         if (pos < 0) {
  1386             position.setErrorIndex(~pos);  // index not updated from input
  1849             position.setErrorIndex(~pos);  // index not updated from input
  1387             return null;
  1850             return null;
  1388         }
  1851         }
  1389         position.setIndex(pos);  // errorIndex not updated from input
  1852         position.setIndex(pos);  // errorIndex not updated from input
  1390         return context;
  1853         return context.toParsed();
  1391     }
  1854     }
  1392 
  1855 
  1393     //-----------------------------------------------------------------------
  1856     //-----------------------------------------------------------------------
  1394     /**
  1857     /**
  1395      * Returns the formatter as a composite printer parser.
  1858      * Returns the formatter as a composite printer parser.
  1494         @Override
  1957         @Override
  1495         public Object parseObject(String text) throws ParseException {
  1958         public Object parseObject(String text) throws ParseException {
  1496             Objects.requireNonNull(text, "text");
  1959             Objects.requireNonNull(text, "text");
  1497             try {
  1960             try {
  1498                 if (parseType == null) {
  1961                 if (parseType == null) {
  1499                     return formatter.parseToBuilder(text, null).resolve();
  1962                     return formatter.parseResolved0(text, null);
  1500                 }
  1963                 }
  1501                 return formatter.parse(text, parseType);
  1964                 return formatter.parse(text, parseType);
  1502             } catch (DateTimeParseException ex) {
  1965             } catch (DateTimeParseException ex) {
  1503                 throw new ParseException(ex.getMessage(), ex.getErrorIndex());
  1966                 throw new ParseException(ex.getMessage(), ex.getErrorIndex());
  1504             } catch (RuntimeException ex) {
  1967             } catch (RuntimeException ex) {
  1506             }
  1969             }
  1507         }
  1970         }
  1508         @Override
  1971         @Override
  1509         public Object parseObject(String text, ParsePosition pos) {
  1972         public Object parseObject(String text, ParsePosition pos) {
  1510             Objects.requireNonNull(text, "text");
  1973             Objects.requireNonNull(text, "text");
  1511             DateTimeParseContext unresolved;
  1974             Parsed unresolved;
  1512             try {
  1975             try {
  1513                 unresolved = formatter.parseUnresolved0(text, pos);
  1976                 unresolved = formatter.parseUnresolved0(text, pos);
  1514             } catch (IndexOutOfBoundsException ex) {
  1977             } catch (IndexOutOfBoundsException ex) {
  1515                 if (pos.getErrorIndex() < 0) {
  1978                 if (pos.getErrorIndex() < 0) {
  1516                     pos.setErrorIndex(0);
  1979                     pos.setErrorIndex(0);
  1522                     pos.setErrorIndex(0);
  1985                     pos.setErrorIndex(0);
  1523                 }
  1986                 }
  1524                 return null;
  1987                 return null;
  1525             }
  1988             }
  1526             try {
  1989             try {
  1527                 DateTimeBuilder builder = unresolved.resolveFields().toBuilder().resolve();
  1990                 TemporalAccessor resolved = unresolved.resolve(formatter.resolverStyle, formatter.resolverFields);
  1528                 if (parseType == null) {
  1991                 if (parseType == null) {
  1529                     return builder;
  1992                     return resolved;
  1530                 }
  1993                 }
  1531                 return builder.query(parseType);
  1994                 return resolved.query(parseType);
  1532             } catch (RuntimeException ex) {
  1995             } catch (RuntimeException ex) {
  1533                 pos.setErrorIndex(0);
  1996                 pos.setErrorIndex(0);
  1534                 return null;
  1997                 return null;
  1535             }
  1998             }
  1536         }
  1999         }