1 /* 2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 33 * 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions are met: 38 * 39 * * Redistributions of source code must retain the above copyright notice, 40 * this list of conditions and the following disclaimer. 41 * 42 * * Redistributions in binary form must reproduce the above copyright notice, 43 * this list of conditions and the following disclaimer in the documentation 44 * and/or other materials provided with the distribution. 45 * 46 * * Neither the name of JSR-310 nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package java.time.format; 63 64 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 65 import static java.time.temporal.ChronoField.DAY_OF_WEEK; 66 import static java.time.temporal.ChronoField.DAY_OF_YEAR; 67 import static java.time.temporal.ChronoField.HOUR_OF_DAY; 68 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; 69 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 70 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 71 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; 72 import static java.time.temporal.ChronoField.YEAR; 73 74 import java.io.IOException; 75 import java.text.FieldPosition; 76 import java.text.Format; 77 import java.text.ParseException; 78 import java.text.ParsePosition; 79 import java.time.DateTimeException; 80 import java.time.Period; 81 import java.time.ZoneId; 82 import java.time.ZoneOffset; 83 import java.time.chrono.ChronoLocalDateTime; 84 import java.time.chrono.Chronology; 85 import java.time.chrono.IsoChronology; 86 import java.time.format.DateTimeFormatterBuilder.CompositePrinterParser; 87 import java.time.temporal.ChronoField; 88 import java.time.temporal.IsoFields; 89 import java.time.temporal.TemporalAccessor; 90 import java.time.temporal.TemporalField; 91 import java.time.temporal.TemporalQuery; 92 import java.util.Arrays; 93 import java.util.Collections; 94 import java.util.HashMap; 95 import java.util.HashSet; 96 import java.util.Locale; 97 import java.util.Map; 98 import java.util.Objects; 99 import java.util.Optional; 100 import java.util.Set; 101 import libcore.icu.ICU; 102 103 /** 104 * Formatter for printing and parsing date-time objects. 105 * <p> 106 * This class provides the main application entry point for printing and parsing 107 * and provides common implementations of {@code DateTimeFormatter}: 108 * <ul> 109 * <li>Using predefined constants, such as {@link #ISO_LOCAL_DATE}</li> 110 * <li>Using pattern letters, such as {@code uuuu-MMM-dd}</li> 111 * <li>Using localized styles, such as {@code long} or {@code medium}</li> 112 * </ul> 113 * <p> 114 * More complex formatters are provided by 115 * {@link DateTimeFormatterBuilder DateTimeFormatterBuilder}. 116 * 117 * <p> 118 * The main date-time classes provide two methods - one for formatting, 119 * {@code format(DateTimeFormatter formatter)}, and one for parsing, 120 * {@code parse(CharSequence text, DateTimeFormatter formatter)}. 121 * <p>For example: 122 * <blockquote><pre> 123 * LocalDate date = LocalDate.now(); 124 * String text = date.format(formatter); 125 * LocalDate parsedDate = LocalDate.parse(text, formatter); 126 * </pre></blockquote> 127 * <p> 128 * In addition to the format, formatters can be created with desired Locale, 129 * Chronology, ZoneId, and DecimalStyle. 130 * <p> 131 * The {@link #withLocale withLocale} method returns a new formatter that 132 * overrides the locale. The locale affects some aspects of formatting and 133 * parsing. For example, the {@link #ofLocalizedDate ofLocalizedDate} provides a 134 * formatter that uses the locale specific date format. 135 * <p> 136 * The {@link #withChronology withChronology} method returns a new formatter 137 * that overrides the chronology. If overridden, the date-time value is 138 * converted to the chronology before formatting. During parsing the date-time 139 * value is converted to the chronology before it is returned. 140 * <p> 141 * The {@link #withZone withZone} method returns a new formatter that overrides 142 * the zone. If overridden, the date-time value is converted to a ZonedDateTime 143 * with the requested ZoneId before formatting. During parsing the ZoneId is 144 * applied before the value is returned. 145 * <p> 146 * The {@link #withDecimalStyle withDecimalStyle} method returns a new formatter that 147 * overrides the {@link DecimalStyle}. The DecimalStyle symbols are used for 148 * formatting and parsing. 149 * <p> 150 * Some applications may need to use the older {@link Format java.text.Format} 151 * class for formatting. The {@link #toFormat()} method returns an 152 * implementation of {@code java.text.Format}. 153 * 154 * <h3 id="predefined">Predefined Formatters</h3> 155 * <table class="striped" style="text-align:left"> 156 * <caption>Predefined Formatters</caption> 157 * <thead> 158 * <tr> 159 * <th scope="col">Formatter</th> 160 * <th scope="col">Description</th> 161 * <th scope="col">Example</th> 162 * </tr> 163 * </thead> 164 * <tbody> 165 * <tr> 166 * <th scope="row">{@link #ofLocalizedDate ofLocalizedDate(dateStyle)} </th> 167 * <td> Formatter with date style from the locale </td> 168 * <td> '2011-12-03'</td> 169 * </tr> 170 * <tr> 171 * <th scope="row"> {@link #ofLocalizedTime ofLocalizedTime(timeStyle)} </th> 172 * <td> Formatter with time style from the locale </td> 173 * <td> '10:15:30'</td> 174 * </tr> 175 * <tr> 176 * <th scope="row"> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateTimeStyle)} </th> 177 * <td> Formatter with a style for date and time from the locale</td> 178 * <td> '3 Jun 2008 11:05:30'</td> 179 * </tr> 180 * <tr> 181 * <th scope="row"> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateStyle,timeStyle)} 182 * </th> 183 * <td> Formatter with date and time styles from the locale </td> 184 * <td> '3 Jun 2008 11:05'</td> 185 * </tr> 186 * <tr> 187 * <th scope="row"> {@link #BASIC_ISO_DATE}</th> 188 * <td>Basic ISO date </td> <td>'20111203'</td> 189 * </tr> 190 * <tr> 191 * <th scope="row"> {@link #ISO_LOCAL_DATE}</th> 192 * <td> ISO Local Date </td> 193 * <td>'2011-12-03'</td> 194 * </tr> 195 * <tr> 196 * <th scope="row"> {@link #ISO_OFFSET_DATE}</th> 197 * <td> ISO Date with offset </td> 198 * <td>'2011-12-03+01:00'</td> 199 * </tr> 200 * <tr> 201 * <th scope="row"> {@link #ISO_DATE}</th> 202 * <td> ISO Date with or without offset </td> 203 * <td> '2011-12-03+01:00'; '2011-12-03'</td> 204 * </tr> 205 * <tr> 206 * <th scope="row"> {@link #ISO_LOCAL_TIME}</th> 207 * <td> Time without offset </td> 208 * <td>'10:15:30'</td> 209 * </tr> 210 * <tr> 211 * <th scope="row"> {@link #ISO_OFFSET_TIME}</th> 212 * <td> Time with offset </td> 213 * <td>'10:15:30+01:00'</td> 214 * </tr> 215 * <tr> 216 * <th scope="row"> {@link #ISO_TIME}</th> 217 * <td> Time with or without offset </td> 218 * <td>'10:15:30+01:00'; '10:15:30'</td> 219 * </tr> 220 * <tr> 221 * <th scope="row"> {@link #ISO_LOCAL_DATE_TIME}</th> 222 * <td> ISO Local Date and Time </td> 223 * <td>'2011-12-03T10:15:30'</td> 224 * </tr> 225 * <tr> 226 * <th scope="row"> {@link #ISO_OFFSET_DATE_TIME}</th> 227 * <td> Date Time with Offset 228 * </td><td>'2011-12-03T10:15:30+01:00'</td> 229 * </tr> 230 * <tr> 231 * <th scope="row"> {@link #ISO_ZONED_DATE_TIME}</th> 232 * <td> Zoned Date Time </td> 233 * <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td> 234 * </tr> 235 * <tr> 236 * <th scope="row"> {@link #ISO_DATE_TIME}</th> 237 * <td> Date and time with ZoneId </td> 238 * <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td> 239 * </tr> 240 * <tr> 241 * <th scope="row"> {@link #ISO_ORDINAL_DATE}</th> 242 * <td> Year and day of year </td> 243 * <td>'2012-337'</td> 244 * </tr> 245 * <tr> 246 * <th scope="row"> {@link #ISO_WEEK_DATE}</th> 247 * <td> Year and Week </td> 248 * <td>'2012-W48-6'</td></tr> 249 * <tr> 250 * <th scope="row"> {@link #ISO_INSTANT}</th> 251 * <td> Date and Time of an Instant </td> 252 * <td>'2011-12-03T10:15:30Z' </td> 253 * </tr> 254 * <tr> 255 * <th scope="row"> {@link #RFC_1123_DATE_TIME}</th> 256 * <td> RFC 1123 / RFC 822 </td> 257 * <td>'Tue, 3 Jun 2008 11:05:30 GMT'</td> 258 * </tr> 259 * </tbody> 260 * </table> 261 * 262 * <h3 id="patterns">Patterns for Formatting and Parsing</h3> 263 * Patterns are based on a simple sequence of letters and symbols. 264 * A pattern is used to create a Formatter using the 265 * {@link #ofPattern(String)} and {@link #ofPattern(String, Locale)} methods. 266 * For example, 267 * {@code "d MMM uuuu"} will format 2011-12-03 as '3 Dec 2011'. 268 * A formatter created from a pattern can be used as many times as necessary, 269 * it is immutable and is thread-safe. 270 * <p> 271 * For example: 272 * <blockquote><pre> 273 * LocalDate date = LocalDate.now(); 274 * DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd"); 275 * String text = date.format(formatter); 276 * LocalDate parsedDate = LocalDate.parse(text, formatter); 277 * </pre></blockquote> 278 * <p> 279 * All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The 280 * following pattern letters are defined: 281 * <table class="striped"> 282 * <caption>Pattern Letters and Symbols</caption> 283 * <thead> 284 * <tr><th scope="col">Symbol</th> <th scope="col">Meaning</th> <th scope="col">Presentation</th> <th scope="col">Examples</th> 285 * </thead> 286 * <tbody> 287 * <tr><th scope="row">G</th> <td>era</td> <td>text</td> <td>AD; Anno Domini; A</td> 288 * <tr><th scope="row">u</th> <td>year</td> <td>year</td> <td>2004; 04</td> 289 * <tr><th scope="row">y</th> <td>year-of-era</td> <td>year</td> <td>2004; 04</td> 290 * <tr><th scope="row">D</th> <td>day-of-year</td> <td>number</td> <td>189</td> 291 * <tr><th scope="row">M/L</th> <td>month-of-year</td> <td>number/text</td> <td>7; 07; Jul; July; J</td> 292 * <tr><th scope="row">d</th> <td>day-of-month</td> <td>number</td> <td>10</td> 293 * <tr><th scope="row">g</th> <td>modified-julian-day</td> <td>number</td> <td>2451334</td> 294 * 295 * <tr><th scope="row">Q/q</th> <td>quarter-of-year</td> <td>number/text</td> <td>3; 03; Q3; 3rd quarter</td> 296 * <tr><th scope="row">Y</th> <td>week-based-year</td> <td>year</td> <td>1996; 96</td> 297 * <tr><th scope="row">w</th> <td>week-of-week-based-year</td> <td>number</td> <td>27</td> 298 * <tr><th scope="row">W</th> <td>week-of-month</td> <td>number</td> <td>4</td> 299 * <tr><th scope="row">E</th> <td>day-of-week</td> <td>text</td> <td>Tue; Tuesday; T</td> 300 * <tr><th scope="row">e/c</th> <td>localized day-of-week</td> <td>number/text</td> <td>2; 02; Tue; Tuesday; T</td> 301 * <tr><th scope="row">F</th> <td>day-of-week-in-month</td> <td>number</td> <td>3</td> 302 * 303 * <tr><th scope="row">a</th> <td>am-pm-of-day</td> <td>text</td> <td>PM</td> 304 * <tr><th scope="row">h</th> <td>clock-hour-of-am-pm (1-12)</td> <td>number</td> <td>12</td> 305 * <tr><th scope="row">K</th> <td>hour-of-am-pm (0-11)</td> <td>number</td> <td>0</td> 306 * <tr><th scope="row">k</th> <td>clock-hour-of-day (1-24)</td> <td>number</td> <td>24</td> 307 * 308 * <tr><th scope="row">H</th> <td>hour-of-day (0-23)</td> <td>number</td> <td>0</td> 309 * <tr><th scope="row">m</th> <td>minute-of-hour</td> <td>number</td> <td>30</td> 310 * <tr><th scope="row">s</th> <td>second-of-minute</td> <td>number</td> <td>55</td> 311 * <tr><th scope="row">S</th> <td>fraction-of-second</td> <td>fraction</td> <td>978</td> 312 * <tr><th scope="row">A</th> <td>milli-of-day</td> <td>number</td> <td>1234</td> 313 * <tr><th scope="row">n</th> <td>nano-of-second</td> <td>number</td> <td>987654321</td> 314 * <tr><th scope="row">N</th> <td>nano-of-day</td> <td>number</td> <td>1234000000</td> 315 * 316 * <tr><th scope="row">V</th> <td>time-zone ID</td> <td>zone-id</td> <td>America/Los_Angeles; Z; -08:30</td> 317 * <tr><th scope="row">v</th> <td>generic time-zone name</td> <td>zone-name</td> <td>Pacific Time; PT</td> 318 * <tr><th scope="row">z</th> <td>time-zone name</td> <td>zone-name</td> <td>Pacific Standard Time; PST</td> 319 * <tr><th scope="row">O</th> <td>localized zone-offset</td> <td>offset-O</td> <td>GMT+8; GMT+08:00; UTC-08:00</td> 320 * <tr><th scope="row">X</th> <td>zone-offset 'Z' for zero</td> <td>offset-X</td> <td>Z; -08; -0830; -08:30; -083015; -08:30:15</td> 321 * <tr><th scope="row">x</th> <td>zone-offset</td> <td>offset-x</td> <td>+0000; -08; -0830; -08:30; -083015; -08:30:15</td> 322 * <tr><th scope="row">Z</th> <td>zone-offset</td> <td>offset-Z</td> <td>+0000; -0800; -08:00</td> 323 * 324 * <tr><th scope="row">p</th> <td>pad next</td> <td>pad modifier</td> <td>1</td> 325 * 326 * <tr><th scope="row">'</th> <td>escape for text</td> <td>delimiter</td> <td></td> 327 * <tr><th scope="row">''</th> <td>single quote</td> <td>literal</td> <td>'</td> 328 * <tr><th scope="row">[</th> <td>optional section start</td> <td></td> <td></td> 329 * <tr><th scope="row">]</th> <td>optional section end</td> <td></td> <td></td> 330 * <tr><th scope="row">#</th> <td>reserved for future use</td> <td></td> <td></td> 331 * <tr><th scope="row">{</th> <td>reserved for future use</td> <td></td> <td></td> 332 * <tr><th scope="row">}</th> <td>reserved for future use</td> <td></td> <td></td> 333 * </tbody> 334 * </table> 335 * <p> 336 * The count of pattern letters determines the format. 337 * <p> 338 * <b>Text</b>: The text style is determined based on the number of pattern 339 * letters used. Less than 4 pattern letters will use the 340 * {@link TextStyle#SHORT short form}. Exactly 4 pattern letters will use the 341 * {@link TextStyle#FULL full form}. Exactly 5 pattern letters will use the 342 * {@link TextStyle#NARROW narrow form}. 343 * Pattern letters 'L', 'c', and 'q' specify the stand-alone form of the text styles. 344 * <p> 345 * <b>Number</b>: If the count of letters is one, then the value is output using 346 * the minimum number of digits and without padding. Otherwise, the count of digits 347 * is used as the width of the output field, with the value zero-padded as necessary. 348 * The following pattern letters have constraints on the count of letters. 349 * Only one letter of 'c' and 'F' can be specified. 350 * Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified. 351 * Up to three letters of 'D' can be specified. 352 * <p> 353 * <b>Number/Text</b>: If the count of pattern letters is 3 or greater, use the 354 * Text rules above. Otherwise use the Number rules above. 355 * <p> 356 * <b>Fraction</b>: Outputs the nano-of-second field as a fraction-of-second. 357 * The nano-of-second value has nine digits, thus the count of pattern letters 358 * is from 1 to 9. If it is less than 9, then the nano-of-second value is 359 * truncated, with only the most significant digits being output. 360 * <p> 361 * <b>Year</b>: The count of letters determines the minimum field width below 362 * which padding is used. If the count of letters is two, then a 363 * {@link DateTimeFormatterBuilder#appendValueReduced reduced} two digit form is 364 * used. For printing, this outputs the rightmost two digits. For parsing, this 365 * will parse using the base value of 2000, resulting in a year within the range 366 * 2000 to 2099 inclusive. If the count of letters is less than four (but not 367 * two), then the sign is only output for negative years as per 368 * {@link SignStyle#NORMAL}. Otherwise, the sign is output if the pad width is 369 * exceeded, as per {@link SignStyle#EXCEEDS_PAD}. 370 * <p> 371 * <b>ZoneId</b>: This outputs the time-zone ID, such as 'Europe/Paris'. If the 372 * count of letters is two, then the time-zone ID is output. Any other count of 373 * letters throws {@code IllegalArgumentException}. 374 * <p> 375 * <b>Zone names</b>: This outputs the display name of the time-zone ID. If the 376 * pattern letter is 'z' the output is the daylight savings aware zone name. 377 * If there is insufficient information to determine whether DST applies, 378 * the name ignoring daylight savings time will be used. 379 * If the count of letters is one, two or three, then the short name is output. 380 * If the count of letters is four, then the full name is output. 381 * Five or more letters throws {@code IllegalArgumentException}. 382 * <p> 383 * If the pattern letter is 'v' the output provides the zone name ignoring 384 * daylight savings time. If the count of letters is one, then the short name is output. 385 * If the count of letters is four, then the full name is output. 386 * Two, three and five or more letters throw {@code IllegalArgumentException}. 387 * <p> 388 * <b>Offset X and x</b>: This formats the offset based on the number of pattern 389 * letters. One letter outputs just the hour, such as '+01', unless the minute 390 * is non-zero in which case the minute is also output, such as '+0130'. Two 391 * letters outputs the hour and minute, without a colon, such as '+0130'. Three 392 * letters outputs the hour and minute, with a colon, such as '+01:30'. Four 393 * letters outputs the hour and minute and optional second, without a colon, 394 * such as '+013015'. Five letters outputs the hour and minute and optional 395 * second, with a colon, such as '+01:30:15'. Six or more letters throws 396 * {@code IllegalArgumentException}. Pattern letter 'X' (upper case) will output 397 * 'Z' when the offset to be output would be zero, whereas pattern letter 'x' 398 * (lower case) will output '+00', '+0000', or '+00:00'. 399 * <p> 400 * <b>Offset O</b>: This formats the localized offset based on the number of 401 * pattern letters. One letter outputs the {@linkplain TextStyle#SHORT short} 402 * form of the localized offset, which is localized offset text, such as 'GMT', 403 * with hour without leading zero, optional 2-digit minute and second if 404 * non-zero, and colon, for example 'GMT+8'. Four letters outputs the 405 * {@linkplain TextStyle#FULL full} form, which is localized offset text, 406 * such as 'GMT, with 2-digit hour and minute field, optional second field 407 * if non-zero, and colon, for example 'GMT+08:00'. Any other count of letters 408 * throws {@code IllegalArgumentException}. 409 * <p> 410 * <b>Offset Z</b>: This formats the offset based on the number of pattern 411 * letters. One, two or three letters outputs the hour and minute, without a 412 * colon, such as '+0130'. The output will be '+0000' when the offset is zero. 413 * Four letters outputs the {@linkplain TextStyle#FULL full} form of localized 414 * offset, equivalent to four letters of Offset-O. The output will be the 415 * corresponding localized offset text if the offset is zero. Five 416 * letters outputs the hour, minute, with optional second if non-zero, with 417 * colon. It outputs 'Z' if the offset is zero. 418 * Six or more letters throws {@code IllegalArgumentException}. 419 * <p> 420 * <b>Optional section</b>: The optional section markers work exactly like 421 * calling {@link DateTimeFormatterBuilder#optionalStart()} and 422 * {@link DateTimeFormatterBuilder#optionalEnd()}. 423 * <p> 424 * <b>Pad modifier</b>: Modifies the pattern that immediately follows to be 425 * padded with spaces. The pad width is determined by the number of pattern 426 * letters. This is the same as calling 427 * {@link DateTimeFormatterBuilder#padNext(int)}. 428 * <p> 429 * For example, 'ppH' outputs the hour-of-day padded on the left with spaces to 430 * a width of 2. 431 * <p> 432 * Any unrecognized letter is an error. Any non-letter character, other than 433 * '[', ']', '{', '}', '#' and the single quote will be output directly. 434 * Despite this, it is recommended to use single quotes around all characters 435 * that you want to output directly to ensure that future changes do not break 436 * your application. 437 * 438 * <h3 id="resolving">Resolving</h3> 439 * Parsing is implemented as a two-phase operation. 440 * First, the text is parsed using the layout defined by the formatter, producing 441 * a {@code Map} of field to value, a {@code ZoneId} and a {@code Chronology}. 442 * Second, the parsed data is <em>resolved</em>, by validating, combining and 443 * simplifying the various fields into more useful ones. 444 * <p> 445 * Five parsing methods are supplied by this class. 446 * Four of these perform both the parse and resolve phases. 447 * The fifth method, {@link #parseUnresolved(CharSequence, ParsePosition)}, 448 * only performs the first phase, leaving the result unresolved. 449 * As such, it is essentially a low-level operation. 450 * <p> 451 * The resolve phase is controlled by two parameters, set on this class. 452 * <p> 453 * The {@link ResolverStyle} is an enum that offers three different approaches, 454 * strict, smart and lenient. The smart option is the default. 455 * It can be set using {@link #withResolverStyle(ResolverStyle)}. 456 * <p> 457 * The {@link #withResolverFields(TemporalField...)} parameter allows the 458 * set of fields that will be resolved to be filtered before resolving starts. 459 * For example, if the formatter has parsed a year, month, day-of-month 460 * and day-of-year, then there are two approaches to resolve a date: 461 * (year + month + day-of-month) and (year + day-of-year). 462 * The resolver fields allows one of the two approaches to be selected. 463 * If no resolver fields are set then both approaches must result in the same date. 464 * <p> 465 * Resolving separate fields to form a complete date and time is a complex 466 * process with behaviour distributed across a number of classes. 467 * It follows these steps: 468 * <ol> 469 * <li>The chronology is determined. 470 * The chronology of the result is either the chronology that was parsed, 471 * or if no chronology was parsed, it is the chronology set on this class, 472 * or if that is null, it is {@code IsoChronology}. 473 * <li>The {@code ChronoField} date fields are resolved. 474 * This is achieved using {@link Chronology#resolveDate(Map, ResolverStyle)}. 475 * Documentation about field resolution is located in the implementation 476 * of {@code Chronology}. 477 * <li>The {@code ChronoField} time fields are resolved. 478 * This is documented on {@link ChronoField} and is the same for all chronologies. 479 * <li>Any fields that are not {@code ChronoField} are processed. 480 * This is achieved using {@link TemporalField#resolve(Map, TemporalAccessor, ResolverStyle)}. 481 * Documentation about field resolution is located in the implementation 482 * of {@code TemporalField}. 483 * <li>The {@code ChronoField} date and time fields are re-resolved. 484 * This allows fields in step four to produce {@code ChronoField} values 485 * and have them be processed into dates and times. 486 * <li>A {@code LocalTime} is formed if there is at least an hour-of-day available. 487 * This involves providing default values for minute, second and fraction of second. 488 * <li>Any remaining unresolved fields are cross-checked against any 489 * date and/or time that was resolved. Thus, an earlier stage would resolve 490 * (year + month + day-of-month) to a date, and this stage would check that 491 * day-of-week was valid for the date. 492 * <li>If an {@linkplain #parsedExcessDays() excess number of days} 493 * was parsed then it is added to the date if a date is available. 494 * <li> If a second-based field is present, but {@code LocalTime} was not parsed, 495 * then the resolver ensures that milli, micro and nano second values are 496 * available to meet the contract of {@link ChronoField}. 497 * These will be set to zero if missing. 498 * <li>If both date and time were parsed and either an offset or zone is present, 499 * the field {@link ChronoField#INSTANT_SECONDS} is created. 500 * If an offset was parsed then the offset will be combined with the 501 * {@code LocalDateTime} to form the instant, with any zone ignored. 502 * If a {@code ZoneId} was parsed without an offset then the zone will be 503 * combined with the {@code LocalDateTime} to form the instant using the rules 504 * of {@link ChronoLocalDateTime#atZone(ZoneId)}. 505 * </ol> 506 * 507 * @implSpec 508 * This class is immutable and thread-safe. 509 * 510 * @since 1.8 511 */ 512 public final class DateTimeFormatter { 513 514 /** 515 * The printer and/or parser to use, not null. 516 */ 517 private final CompositePrinterParser printerParser; 518 /** 519 * The locale to use for formatting, not null. 520 */ 521 private final Locale locale; 522 /** 523 * The symbols to use for formatting, not null. 524 */ 525 private final DecimalStyle decimalStyle; 526 /** 527 * The resolver style to use, not null. 528 */ 529 private final ResolverStyle resolverStyle; 530 /** 531 * The fields to use in resolving, null for all fields. 532 */ 533 private final Set<TemporalField> resolverFields; 534 /** 535 * The chronology to use for formatting, null for no override. 536 */ 537 private final Chronology chrono; 538 /** 539 * The zone to use for formatting, null for no override. 540 */ 541 private final ZoneId zone; 542 543 //----------------------------------------------------------------------- 544 /** 545 * Creates a formatter using the specified pattern. 546 * <p> 547 * This method will create a formatter based on a simple 548 * <a href="#patterns">pattern of letters and symbols</a> 549 * as described in the class documentation. 550 * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. 551 * <p> 552 * The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. 553 * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter. 554 * Alternatively use the {@link #ofPattern(String, Locale)} variant of this method. 555 * <p> 556 * The returned formatter has no override chronology or zone. 557 * It uses {@link ResolverStyle#SMART SMART} resolver style. 558 * 559 * @param pattern the pattern to use, not null 560 * @return the formatter based on the pattern, not null 561 * @throws IllegalArgumentException if the pattern is invalid 562 * @see DateTimeFormatterBuilder#appendPattern(String) 563 */ ofPattern(String pattern)564 public static DateTimeFormatter ofPattern(String pattern) { 565 return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(); 566 } 567 568 /** 569 * Creates a formatter using the specified pattern and locale. 570 * <p> 571 * This method will create a formatter based on a simple 572 * <a href="#patterns">pattern of letters and symbols</a> 573 * as described in the class documentation. 574 * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. 575 * <p> 576 * The formatter will use the specified locale. 577 * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter. 578 * <p> 579 * The returned formatter has no override chronology or zone. 580 * It uses {@link ResolverStyle#SMART SMART} resolver style. 581 * 582 * @param pattern the pattern to use, not null 583 * @param locale the locale to use, not null 584 * @return the formatter based on the pattern, not null 585 * @throws IllegalArgumentException if the pattern is invalid 586 * @see DateTimeFormatterBuilder#appendPattern(String) 587 */ ofPattern(String pattern, Locale locale)588 public static DateTimeFormatter ofPattern(String pattern, Locale locale) { 589 return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale); 590 } 591 592 //----------------------------------------------------------------------- 593 /** 594 * Returns a locale specific date format for the ISO chronology. 595 * <p> 596 * This returns a formatter that will format or parse a date. 597 * The exact format pattern used varies by locale. 598 * <p> 599 * The locale is determined from the formatter. The formatter returned directly by 600 * this method will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. 601 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)} 602 * on the result of this method. 603 * <p> 604 * Note that the localized pattern is looked up lazily. 605 * This {@code DateTimeFormatter} holds the style required and the locale, 606 * looking up the pattern required on demand. 607 * <p> 608 * The returned formatter has a chronology of ISO set to ensure dates in 609 * other calendar systems are correctly converted. 610 * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style. 611 * 612 * @param dateStyle the formatter style to obtain, not null 613 * @return the date formatter, not null 614 */ ofLocalizedDate(FormatStyle dateStyle)615 public static DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle) { 616 Objects.requireNonNull(dateStyle, "dateStyle"); 617 return new DateTimeFormatterBuilder().appendLocalized(dateStyle, null) 618 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE); 619 } 620 621 /** 622 * Returns a locale specific time format for the ISO chronology. 623 * <p> 624 * This returns a formatter that will format or parse a time. 625 * The exact format pattern used varies by locale. 626 * <p> 627 * The locale is determined from the formatter. The formatter returned directly by 628 * this method will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. 629 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)} 630 * on the result of this method. 631 * <p> 632 * Note that the localized pattern is looked up lazily. 633 * This {@code DateTimeFormatter} holds the style required and the locale, 634 * looking up the pattern required on demand. 635 * <p> 636 * The returned formatter has a chronology of ISO set to ensure dates in 637 * other calendar systems are correctly converted. 638 * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style. 639 * The {@code FULL} and {@code LONG} styles typically require a time-zone. 640 * When formatting using these styles, a {@code ZoneId} must be available, 641 * either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}. 642 * 643 * @param timeStyle the formatter style to obtain, not null 644 * @return the time formatter, not null 645 */ ofLocalizedTime(FormatStyle timeStyle)646 public static DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle) { 647 Objects.requireNonNull(timeStyle, "timeStyle"); 648 return new DateTimeFormatterBuilder().appendLocalized(null, timeStyle) 649 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE); 650 } 651 652 /** 653 * Returns a locale specific date-time formatter for the ISO chronology. 654 * <p> 655 * This returns a formatter that will format or parse a date-time. 656 * The exact format pattern used varies by locale. 657 * <p> 658 * The locale is determined from the formatter. The formatter returned directly by 659 * this method will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. 660 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)} 661 * on the result of this method. 662 * <p> 663 * Note that the localized pattern is looked up lazily. 664 * This {@code DateTimeFormatter} holds the style required and the locale, 665 * looking up the pattern required on demand. 666 * <p> 667 * The returned formatter has a chronology of ISO set to ensure dates in 668 * other calendar systems are correctly converted. 669 * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style. 670 * The {@code FULL} and {@code LONG} styles typically require a time-zone. 671 * When formatting using these styles, a {@code ZoneId} must be available, 672 * either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}. 673 * 674 * @param dateTimeStyle the formatter style to obtain, not null 675 * @return the date-time formatter, not null 676 */ ofLocalizedDateTime(FormatStyle dateTimeStyle)677 public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateTimeStyle) { 678 Objects.requireNonNull(dateTimeStyle, "dateTimeStyle"); 679 return new DateTimeFormatterBuilder().appendLocalized(dateTimeStyle, dateTimeStyle) 680 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE); 681 } 682 683 /** 684 * Returns a locale specific date and time format for the ISO chronology. 685 * <p> 686 * This returns a formatter that will format or parse a date-time. 687 * The exact format pattern used varies by locale. 688 * <p> 689 * The locale is determined from the formatter. The formatter returned directly by 690 * this method will use the {@link Locale#getDefault() default FORMAT locale}. 691 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)} 692 * on the result of this method. 693 * <p> 694 * Note that the localized pattern is looked up lazily. 695 * This {@code DateTimeFormatter} holds the style required and the locale, 696 * looking up the pattern required on demand. 697 * <p> 698 * The returned formatter has a chronology of ISO set to ensure dates in 699 * other calendar systems are correctly converted. 700 * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style. 701 * The {@code FULL} and {@code LONG} styles typically require a time-zone. 702 * When formatting using these styles, a {@code ZoneId} must be available, 703 * either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}. 704 * 705 * @param dateStyle the date formatter style to obtain, not null 706 * @param timeStyle the time formatter style to obtain, not null 707 * @return the date, time or date-time formatter, not null 708 */ ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle)709 public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle) { 710 Objects.requireNonNull(dateStyle, "dateStyle"); 711 Objects.requireNonNull(timeStyle, "timeStyle"); 712 return new DateTimeFormatterBuilder().appendLocalized(dateStyle, timeStyle) 713 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE); 714 } 715 716 //----------------------------------------------------------------------- 717 /** 718 * The ISO date formatter that formats or parses a date without an 719 * offset, such as '2011-12-03'. 720 * <p> 721 * This returns an immutable formatter capable of formatting and parsing 722 * the ISO-8601 extended local date format. 723 * The format consists of: 724 * <ul> 725 * <li>Four digits or more for the {@link ChronoField#YEAR year}. 726 * Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. 727 * Years outside that range will have a prefixed positive or negative symbol. 728 * <li>A dash 729 * <li>Two digits for the {@link ChronoField#MONTH_OF_YEAR month-of-year}. 730 * This is pre-padded by zero to ensure two digits. 731 * <li>A dash 732 * <li>Two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}. 733 * This is pre-padded by zero to ensure two digits. 734 * </ul> 735 * <p> 736 * The returned formatter has a chronology of ISO set to ensure dates in 737 * other calendar systems are correctly converted. 738 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 739 */ 740 public static final DateTimeFormatter ISO_LOCAL_DATE; 741 static { 742 ISO_LOCAL_DATE = new DateTimeFormatterBuilder() 743 .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) 744 .appendLiteral('-') 745 .appendValue(MONTH_OF_YEAR, 2) 746 .appendLiteral('-') 747 .appendValue(DAY_OF_MONTH, 2) 748 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 749 } 750 751 //----------------------------------------------------------------------- 752 /** 753 * The ISO date formatter that formats or parses a date with an 754 * offset, such as '2011-12-03+01:00'. 755 * <p> 756 * This returns an immutable formatter capable of formatting and parsing 757 * the ISO-8601 extended offset date format. 758 * The format consists of: 759 * <ul> 760 * <li>The {@link #ISO_LOCAL_DATE} 761 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 762 * they will be handled even though this is not part of the ISO-8601 standard. 763 * Parsing is case insensitive. 764 * </ul> 765 * <p> 766 * The returned formatter has a chronology of ISO set to ensure dates in 767 * other calendar systems are correctly converted. 768 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 769 */ 770 public static final DateTimeFormatter ISO_OFFSET_DATE; 771 static { 772 ISO_OFFSET_DATE = new DateTimeFormatterBuilder() 773 .parseCaseInsensitive() 774 .append(ISO_LOCAL_DATE) 775 .appendOffsetId() 776 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 777 } 778 779 //----------------------------------------------------------------------- 780 /** 781 * The ISO date formatter that formats or parses a date with the 782 * offset if available, such as '2011-12-03' or '2011-12-03+01:00'. 783 * <p> 784 * This returns an immutable formatter capable of formatting and parsing 785 * the ISO-8601 extended date format. 786 * The format consists of: 787 * <ul> 788 * <li>The {@link #ISO_LOCAL_DATE} 789 * <li>If the offset is not available then the format is complete. 790 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 791 * they will be handled even though this is not part of the ISO-8601 standard. 792 * Parsing is case insensitive. 793 * </ul> 794 * <p> 795 * As this formatter has an optional element, it may be necessary to parse using 796 * {@link DateTimeFormatter#parseBest}. 797 * <p> 798 * The returned formatter has a chronology of ISO set to ensure dates in 799 * other calendar systems are correctly converted. 800 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 801 */ 802 public static final DateTimeFormatter ISO_DATE; 803 static { 804 ISO_DATE = new DateTimeFormatterBuilder() 805 .parseCaseInsensitive() 806 .append(ISO_LOCAL_DATE) 807 .optionalStart() 808 .appendOffsetId() 809 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 810 } 811 812 //----------------------------------------------------------------------- 813 /** 814 * The ISO time formatter that formats or parses a time without an 815 * offset, such as '10:15' or '10:15:30'. 816 * <p> 817 * This returns an immutable formatter capable of formatting and parsing 818 * the ISO-8601 extended local time format. 819 * The format consists of: 820 * <ul> 821 * <li>Two digits for the {@link ChronoField#HOUR_OF_DAY hour-of-day}. 822 * This is pre-padded by zero to ensure two digits. 823 * <li>A colon 824 * <li>Two digits for the {@link ChronoField#MINUTE_OF_HOUR minute-of-hour}. 825 * This is pre-padded by zero to ensure two digits. 826 * <li>If the second-of-minute is not available then the format is complete. 827 * <li>A colon 828 * <li>Two digits for the {@link ChronoField#SECOND_OF_MINUTE second-of-minute}. 829 * This is pre-padded by zero to ensure two digits. 830 * <li>If the nano-of-second is zero or not available then the format is complete. 831 * <li>A decimal point 832 * <li>One to nine digits for the {@link ChronoField#NANO_OF_SECOND nano-of-second}. 833 * As many digits will be output as required. 834 * </ul> 835 * <p> 836 * The returned formatter has no override chronology or zone. 837 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. 838 */ 839 public static final DateTimeFormatter ISO_LOCAL_TIME; 840 static { 841 ISO_LOCAL_TIME = new DateTimeFormatterBuilder() 842 .appendValue(HOUR_OF_DAY, 2) 843 .appendLiteral(':') 844 .appendValue(MINUTE_OF_HOUR, 2) 845 .optionalStart() 846 .appendLiteral(':') 847 .appendValue(SECOND_OF_MINUTE, 2) 848 .optionalStart() 849 .appendFraction(NANO_OF_SECOND, 0, 9, true) 850 .toFormatter(ResolverStyle.STRICT, null); 851 } 852 853 //----------------------------------------------------------------------- 854 /** 855 * The ISO time formatter that formats or parses a time with an 856 * offset, such as '10:15+01:00' or '10:15:30+01:00'. 857 * <p> 858 * This returns an immutable formatter capable of formatting and parsing 859 * the ISO-8601 extended offset time format. 860 * The format consists of: 861 * <ul> 862 * <li>The {@link #ISO_LOCAL_TIME} 863 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 864 * they will be handled even though this is not part of the ISO-8601 standard. 865 * Parsing is case insensitive. 866 * </ul> 867 * <p> 868 * The returned formatter has no override chronology or zone. 869 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. 870 */ 871 public static final DateTimeFormatter ISO_OFFSET_TIME; 872 static { 873 ISO_OFFSET_TIME = new DateTimeFormatterBuilder() 874 .parseCaseInsensitive() 875 .append(ISO_LOCAL_TIME) 876 .appendOffsetId() 877 .toFormatter(ResolverStyle.STRICT, null); 878 } 879 880 //----------------------------------------------------------------------- 881 /** 882 * The ISO time formatter that formats or parses a time, with the 883 * offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'. 884 * <p> 885 * This returns an immutable formatter capable of formatting and parsing 886 * the ISO-8601 extended offset time format. 887 * The format consists of: 888 * <ul> 889 * <li>The {@link #ISO_LOCAL_TIME} 890 * <li>If the offset is not available then the format is complete. 891 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 892 * they will be handled even though this is not part of the ISO-8601 standard. 893 * Parsing is case insensitive. 894 * </ul> 895 * <p> 896 * As this formatter has an optional element, it may be necessary to parse using 897 * {@link DateTimeFormatter#parseBest}. 898 * <p> 899 * The returned formatter has no override chronology or zone. 900 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. 901 */ 902 public static final DateTimeFormatter ISO_TIME; 903 static { 904 ISO_TIME = new DateTimeFormatterBuilder() 905 .parseCaseInsensitive() 906 .append(ISO_LOCAL_TIME) 907 .optionalStart() 908 .appendOffsetId() 909 .toFormatter(ResolverStyle.STRICT, null); 910 } 911 912 //----------------------------------------------------------------------- 913 /** 914 * The ISO date-time formatter that formats or parses a date-time without 915 * an offset, such as '2011-12-03T10:15:30'. 916 * <p> 917 * This returns an immutable formatter capable of formatting and parsing 918 * the ISO-8601 extended offset date-time format. 919 * The format consists of: 920 * <ul> 921 * <li>The {@link #ISO_LOCAL_DATE} 922 * <li>The letter 'T'. Parsing is case insensitive. 923 * <li>The {@link #ISO_LOCAL_TIME} 924 * </ul> 925 * <p> 926 * The returned formatter has a chronology of ISO set to ensure dates in 927 * other calendar systems are correctly converted. 928 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 929 */ 930 public static final DateTimeFormatter ISO_LOCAL_DATE_TIME; 931 static { 932 ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder() 933 .parseCaseInsensitive() 934 .append(ISO_LOCAL_DATE) 935 .appendLiteral('T') 936 .append(ISO_LOCAL_TIME) 937 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 938 } 939 940 //----------------------------------------------------------------------- 941 /** 942 * The ISO date-time formatter that formats or parses a date-time with an 943 * offset, such as '2011-12-03T10:15:30+01:00'. 944 * <p> 945 * This returns an immutable formatter capable of formatting and parsing 946 * the ISO-8601 extended offset date-time format. 947 * The format consists of: 948 * <ul> 949 * <li>The {@link #ISO_LOCAL_DATE_TIME} 950 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 951 * they will be handled even though this is not part of the ISO-8601 standard. 952 * The offset parsing is lenient, which allows the minutes and seconds to be optional. 953 * Parsing is case insensitive. 954 * </ul> 955 * <p> 956 * The returned formatter has a chronology of ISO set to ensure dates in 957 * other calendar systems are correctly converted. 958 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 959 */ 960 public static final DateTimeFormatter ISO_OFFSET_DATE_TIME; 961 static { 962 ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder() 963 .parseCaseInsensitive() 964 .append(ISO_LOCAL_DATE_TIME) 965 .parseLenient() 966 .appendOffsetId() 967 .parseStrict() 968 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 969 } 970 971 //----------------------------------------------------------------------- 972 /** 973 * The ISO-like date-time formatter that formats or parses a date-time with 974 * offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'. 975 * <p> 976 * This returns an immutable formatter capable of formatting and parsing 977 * a format that extends the ISO-8601 extended offset date-time format 978 * to add the time-zone. 979 * The section in square brackets is not part of the ISO-8601 standard. 980 * The format consists of: 981 * <ul> 982 * <li>The {@link #ISO_OFFSET_DATE_TIME} 983 * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete. 984 * <li>An open square bracket '['. 985 * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard. 986 * Parsing is case sensitive. 987 * <li>A close square bracket ']'. 988 * </ul> 989 * <p> 990 * The returned formatter has a chronology of ISO set to ensure dates in 991 * other calendar systems are correctly converted. 992 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 993 */ 994 public static final DateTimeFormatter ISO_ZONED_DATE_TIME; 995 static { 996 ISO_ZONED_DATE_TIME = new DateTimeFormatterBuilder() 997 .append(ISO_OFFSET_DATE_TIME) 998 .optionalStart() 999 .appendLiteral('[') 1000 .parseCaseSensitive() 1001 .appendZoneRegionId() 1002 .appendLiteral(']') 1003 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 1004 } 1005 1006 //----------------------------------------------------------------------- 1007 /** 1008 * The ISO-like date-time formatter that formats or parses a date-time with 1009 * the offset and zone if available, such as '2011-12-03T10:15:30', 1010 * '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'. 1011 * <p> 1012 * This returns an immutable formatter capable of formatting and parsing 1013 * the ISO-8601 extended local or offset date-time format, as well as the 1014 * extended non-ISO form specifying the time-zone. 1015 * The format consists of: 1016 * <ul> 1017 * <li>The {@link #ISO_LOCAL_DATE_TIME} 1018 * <li>If the offset is not available to format or parse then the format is complete. 1019 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 1020 * they will be handled even though this is not part of the ISO-8601 standard. 1021 * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete. 1022 * <li>An open square bracket '['. 1023 * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard. 1024 * Parsing is case sensitive. 1025 * <li>A close square bracket ']'. 1026 * </ul> 1027 * <p> 1028 * As this formatter has an optional element, it may be necessary to parse using 1029 * {@link DateTimeFormatter#parseBest}. 1030 * <p> 1031 * The returned formatter has a chronology of ISO set to ensure dates in 1032 * other calendar systems are correctly converted. 1033 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 1034 */ 1035 public static final DateTimeFormatter ISO_DATE_TIME; 1036 static { 1037 ISO_DATE_TIME = new DateTimeFormatterBuilder() 1038 .append(ISO_LOCAL_DATE_TIME) 1039 .optionalStart() 1040 .appendOffsetId() 1041 .optionalStart() 1042 .appendLiteral('[') 1043 .parseCaseSensitive() 1044 .appendZoneRegionId() 1045 .appendLiteral(']') 1046 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 1047 } 1048 1049 //----------------------------------------------------------------------- 1050 /** 1051 * The ISO date formatter that formats or parses the ordinal date 1052 * without an offset, such as '2012-337'. 1053 * <p> 1054 * This returns an immutable formatter capable of formatting and parsing 1055 * the ISO-8601 extended ordinal date format. 1056 * The format consists of: 1057 * <ul> 1058 * <li>Four digits or more for the {@link ChronoField#YEAR year}. 1059 * Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. 1060 * Years outside that range will have a prefixed positive or negative symbol. 1061 * <li>A dash 1062 * <li>Three digits for the {@link ChronoField#DAY_OF_YEAR day-of-year}. 1063 * This is pre-padded by zero to ensure three digits. 1064 * <li>If the offset is not available to format or parse then the format is complete. 1065 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 1066 * they will be handled even though this is not part of the ISO-8601 standard. 1067 * Parsing is case insensitive. 1068 * </ul> 1069 * <p> 1070 * As this formatter has an optional element, it may be necessary to parse using 1071 * {@link DateTimeFormatter#parseBest}. 1072 * <p> 1073 * The returned formatter has a chronology of ISO set to ensure dates in 1074 * other calendar systems are correctly converted. 1075 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 1076 */ 1077 public static final DateTimeFormatter ISO_ORDINAL_DATE; 1078 static { 1079 ISO_ORDINAL_DATE = new DateTimeFormatterBuilder() 1080 .parseCaseInsensitive() 1081 .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) 1082 .appendLiteral('-') 1083 .appendValue(DAY_OF_YEAR, 3) 1084 .optionalStart() 1085 .appendOffsetId() 1086 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 1087 } 1088 1089 //----------------------------------------------------------------------- 1090 /** 1091 * The ISO date formatter that formats or parses the week-based date 1092 * without an offset, such as '2012-W48-6'. 1093 * <p> 1094 * This returns an immutable formatter capable of formatting and parsing 1095 * the ISO-8601 extended week-based date format. 1096 * The format consists of: 1097 * <ul> 1098 * <li>Four digits or more for the {@link IsoFields#WEEK_BASED_YEAR week-based-year}. 1099 * Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. 1100 * Years outside that range will have a prefixed positive or negative symbol. 1101 * <li>A dash 1102 * <li>The letter 'W'. Parsing is case insensitive. 1103 * <li>Two digits for the {@link IsoFields#WEEK_OF_WEEK_BASED_YEAR week-of-week-based-year}. 1104 * This is pre-padded by zero to ensure three digits. 1105 * <li>A dash 1106 * <li>One digit for the {@link ChronoField#DAY_OF_WEEK day-of-week}. 1107 * The value run from Monday (1) to Sunday (7). 1108 * <li>If the offset is not available to format or parse then the format is complete. 1109 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then 1110 * they will be handled even though this is not part of the ISO-8601 standard. 1111 * Parsing is case insensitive. 1112 * </ul> 1113 * <p> 1114 * As this formatter has an optional element, it may be necessary to parse using 1115 * {@link DateTimeFormatter#parseBest}. 1116 * <p> 1117 * The returned formatter has a chronology of ISO set to ensure dates in 1118 * other calendar systems are correctly converted. 1119 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 1120 */ 1121 public static final DateTimeFormatter ISO_WEEK_DATE; 1122 static { 1123 ISO_WEEK_DATE = new DateTimeFormatterBuilder() 1124 .parseCaseInsensitive() 1125 .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD) 1126 .appendLiteral("-W") 1127 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2) 1128 .appendLiteral('-') 1129 .appendValue(DAY_OF_WEEK, 1) 1130 .optionalStart() 1131 .appendOffsetId() 1132 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 1133 } 1134 1135 //----------------------------------------------------------------------- 1136 /** 1137 * The ISO instant formatter that formats or parses an instant in UTC, 1138 * such as '2011-12-03T10:15:30Z'. 1139 * <p> 1140 * This returns an immutable formatter capable of formatting and parsing 1141 * the ISO-8601 instant format. 1142 * When formatting, the second-of-minute is always output. 1143 * The nano-of-second outputs zero, three, six or nine digits as necessary. 1144 * When parsing, time to at least the seconds field is required. 1145 * Fractional seconds from zero to nine are parsed. 1146 * The localized decimal style is not used. 1147 * <p> 1148 * This is a special case formatter intended to allow a human readable form 1149 * of an {@link java.time.Instant}. The {@code Instant} class is designed to 1150 * only represent a point in time and internally stores a value in nanoseconds 1151 * from a fixed epoch of 1970-01-01Z. As such, an {@code Instant} cannot be 1152 * formatted as a date or time without providing some form of time-zone. 1153 * This formatter allows the {@code Instant} to be formatted, by providing 1154 * a suitable conversion using {@code ZoneOffset.UTC}. 1155 * <p> 1156 * The format consists of: 1157 * <ul> 1158 * <li>The {@link #ISO_OFFSET_DATE_TIME} where the instant is converted from 1159 * {@link ChronoField#INSTANT_SECONDS} and {@link ChronoField#NANO_OF_SECOND} 1160 * using the {@code UTC} offset. Parsing is case insensitive. 1161 * </ul> 1162 * <p> 1163 * The returned formatter has no override chronology or zone. 1164 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. 1165 */ 1166 public static final DateTimeFormatter ISO_INSTANT; 1167 static { 1168 ISO_INSTANT = new DateTimeFormatterBuilder() 1169 .parseCaseInsensitive() 1170 .appendInstant() 1171 .toFormatter(ResolverStyle.STRICT, null); 1172 } 1173 1174 //----------------------------------------------------------------------- 1175 /** 1176 * The ISO date formatter that formats or parses a date without an 1177 * offset, such as '20111203'. 1178 * <p> 1179 * This returns an immutable formatter capable of formatting and parsing 1180 * the ISO-8601 basic local date format. 1181 * The format consists of: 1182 * <ul> 1183 * <li>Four digits for the {@link ChronoField#YEAR year}. 1184 * Only years in the range 0000 to 9999 are supported. 1185 * <li>Two digits for the {@link ChronoField#MONTH_OF_YEAR month-of-year}. 1186 * This is pre-padded by zero to ensure two digits. 1187 * <li>Two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}. 1188 * This is pre-padded by zero to ensure two digits. 1189 * <li>If the offset is not available to format or parse then the format is complete. 1190 * <li>The {@link ZoneOffset#getId() offset ID} without colons. If the offset has 1191 * seconds then they will be handled even though this is not part of the ISO-8601 standard. 1192 * The offset parsing is lenient, which allows the minutes and seconds to be optional. 1193 * Parsing is case insensitive. 1194 * </ul> 1195 * <p> 1196 * As this formatter has an optional element, it may be necessary to parse using 1197 * {@link DateTimeFormatter#parseBest}. 1198 * <p> 1199 * The returned formatter has a chronology of ISO set to ensure dates in 1200 * other calendar systems are correctly converted. 1201 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. 1202 */ 1203 public static final DateTimeFormatter BASIC_ISO_DATE; 1204 static { 1205 BASIC_ISO_DATE = new DateTimeFormatterBuilder() 1206 .parseCaseInsensitive() 1207 .appendValue(YEAR, 4) 1208 .appendValue(MONTH_OF_YEAR, 2) 1209 .appendValue(DAY_OF_MONTH, 2) 1210 .optionalStart() 1211 .parseLenient() 1212 .appendOffset("+HHMMss", "Z") 1213 .parseStrict() 1214 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); 1215 } 1216 1217 //----------------------------------------------------------------------- 1218 /** 1219 * The RFC-1123 date-time formatter, such as 'Tue, 3 Jun 2008 11:05:30 GMT'. 1220 * <p> 1221 * This returns an immutable formatter capable of formatting and parsing 1222 * most of the RFC-1123 format. 1223 * RFC-1123 updates RFC-822 changing the year from two digits to four. 1224 * This implementation requires a four digit year. 1225 * This implementation also does not handle North American or military zone 1226 * names, only 'GMT' and offset amounts. 1227 * <p> 1228 * The format consists of: 1229 * <ul> 1230 * <li>If the day-of-week is not available to format or parse then jump to day-of-month. 1231 * <li>Three letter {@link ChronoField#DAY_OF_WEEK day-of-week} in English. 1232 * <li>A comma 1233 * <li>A space 1234 * <li>One or two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}. 1235 * <li>A space 1236 * <li>Three letter {@link ChronoField#MONTH_OF_YEAR month-of-year} in English. 1237 * <li>A space 1238 * <li>Four digits for the {@link ChronoField#YEAR year}. 1239 * Only years in the range 0000 to 9999 are supported. 1240 * <li>A space 1241 * <li>Two digits for the {@link ChronoField#HOUR_OF_DAY hour-of-day}. 1242 * This is pre-padded by zero to ensure two digits. 1243 * <li>A colon 1244 * <li>Two digits for the {@link ChronoField#MINUTE_OF_HOUR minute-of-hour}. 1245 * This is pre-padded by zero to ensure two digits. 1246 * <li>If the second-of-minute is not available then jump to the next space. 1247 * <li>A colon 1248 * <li>Two digits for the {@link ChronoField#SECOND_OF_MINUTE second-of-minute}. 1249 * This is pre-padded by zero to ensure two digits. 1250 * <li>A space 1251 * <li>The {@link ZoneOffset#getId() offset ID} without colons or seconds. 1252 * An offset of zero uses "GMT". North American zone names and military zone names are not handled. 1253 * </ul> 1254 * <p> 1255 * Parsing is case insensitive. 1256 * <p> 1257 * The returned formatter has a chronology of ISO set to ensure dates in 1258 * other calendar systems are correctly converted. 1259 * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style. 1260 */ 1261 public static final DateTimeFormatter RFC_1123_DATE_TIME; 1262 static { 1263 // manually code maps to ensure correct data always used 1264 // (locale data can be changed by application code) 1265 Map<Long, String> dow = new HashMap<>(); 1266 dow.put(1L, "Mon"); 1267 dow.put(2L, "Tue"); 1268 dow.put(3L, "Wed"); 1269 dow.put(4L, "Thu"); 1270 dow.put(5L, "Fri"); 1271 dow.put(6L, "Sat"); 1272 dow.put(7L, "Sun"); 1273 Map<Long, String> moy = new HashMap<>(); 1274 moy.put(1L, "Jan"); 1275 moy.put(2L, "Feb"); 1276 moy.put(3L, "Mar"); 1277 moy.put(4L, "Apr"); 1278 moy.put(5L, "May"); 1279 moy.put(6L, "Jun"); 1280 moy.put(7L, "Jul"); 1281 moy.put(8L, "Aug"); 1282 moy.put(9L, "Sep"); 1283 moy.put(10L, "Oct"); 1284 moy.put(11L, "Nov"); 1285 moy.put(12L, "Dec"); 1286 RFC_1123_DATE_TIME = new DateTimeFormatterBuilder() 1287 .parseCaseInsensitive() 1288 .parseLenient() 1289 .optionalStart() 1290 .appendText(DAY_OF_WEEK, dow) 1291 .appendLiteral(", ") 1292 .optionalEnd() 1293 .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE) 1294 .appendLiteral(' ') 1295 .appendText(MONTH_OF_YEAR, moy) 1296 .appendLiteral(' ') 1297 .appendValue(YEAR, 4) // 2 digit year not handled 1298 .appendLiteral(' ') 1299 .appendValue(HOUR_OF_DAY, 2) 1300 .appendLiteral(':') 1301 .appendValue(MINUTE_OF_HOUR, 2) 1302 .optionalStart() 1303 .appendLiteral(':') 1304 .appendValue(SECOND_OF_MINUTE, 2) 1305 .optionalEnd() 1306 .appendLiteral(' ') 1307 .appendOffset("+HHMM", "GMT") // should handle UT/Z/EST/EDT/CST/CDT/MST/MDT/PST/MDT 1308 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE); 1309 } 1310 1311 //----------------------------------------------------------------------- 1312 /** 1313 * A query that provides access to the excess days that were parsed. 1314 * <p> 1315 * This returns a singleton {@linkplain TemporalQuery query} that provides 1316 * access to additional information from the parse. The query always returns 1317 * a non-null period, with a zero period returned instead of null. 1318 * <p> 1319 * There are two situations where this query may return a non-zero period. 1320 * <ul> 1321 * <li>If the {@code ResolverStyle} is {@code LENIENT} and a time is parsed 1322 * without a date, then the complete result of the parse consists of a 1323 * {@code LocalTime} and an excess {@code Period} in days. 1324 * 1325 * <li>If the {@code ResolverStyle} is {@code SMART} and a time is parsed 1326 * without a date where the time is 24:00:00, then the complete result of 1327 * the parse consists of a {@code LocalTime} of 00:00:00 and an excess 1328 * {@code Period} of one day. 1329 * </ul> 1330 * <p> 1331 * In both cases, if a complete {@code ChronoLocalDateTime} or {@code Instant} 1332 * is parsed, then the excess days are added to the date part. 1333 * As a result, this query will return a zero period. 1334 * <p> 1335 * The {@code SMART} behaviour handles the common "end of day" 24:00 value. 1336 * Processing in {@code LENIENT} mode also produces the same result: 1337 * <pre> 1338 * Text to parse Parsed object Excess days 1339 * "2012-12-03T00:00" LocalDateTime.of(2012, 12, 3, 0, 0) ZERO 1340 * "2012-12-03T24:00" LocalDateTime.of(2012, 12, 4, 0, 0) ZERO 1341 * "00:00" LocalTime.of(0, 0) ZERO 1342 * "24:00" LocalTime.of(0, 0) Period.ofDays(1) 1343 * </pre> 1344 * The query can be used as follows: 1345 * <pre> 1346 * TemporalAccessor parsed = formatter.parse(str); 1347 * LocalTime time = parsed.query(LocalTime::from); 1348 * Period extraDays = parsed.query(DateTimeFormatter.parsedExcessDays()); 1349 * </pre> 1350 * @return a query that provides access to the excess days that were parsed 1351 */ parsedExcessDays()1352 public static final TemporalQuery<Period> parsedExcessDays() { 1353 return PARSED_EXCESS_DAYS; 1354 } 1355 private static final TemporalQuery<Period> PARSED_EXCESS_DAYS = t -> { 1356 if (t instanceof Parsed) { 1357 return ((Parsed) t).excessDays; 1358 } else { 1359 return Period.ZERO; 1360 } 1361 }; 1362 1363 /** 1364 * A query that provides access to whether a leap-second was parsed. 1365 * <p> 1366 * This returns a singleton {@linkplain TemporalQuery query} that provides 1367 * access to additional information from the parse. The query always returns 1368 * a non-null boolean, true if parsing saw a leap-second, false if not. 1369 * <p> 1370 * Instant parsing handles the special "leap second" time of '23:59:60'. 1371 * Leap seconds occur at '23:59:60' in the UTC time-zone, but at other 1372 * local times in different time-zones. To avoid this potential ambiguity, 1373 * the handling of leap-seconds is limited to 1374 * {@link DateTimeFormatterBuilder#appendInstant()}, as that method 1375 * always parses the instant with the UTC zone offset. 1376 * <p> 1377 * If the time '23:59:60' is received, then a simple conversion is applied, 1378 * replacing the second-of-minute of 60 with 59. This query can be used 1379 * on the parse result to determine if the leap-second adjustment was made. 1380 * The query will return {@code true} if it did adjust to remove the 1381 * leap-second, and {@code false} if not. Note that applying a leap-second 1382 * smoothing mechanism, such as UTC-SLS, is the responsibility of the 1383 * application, as follows: 1384 * <pre> 1385 * TemporalAccessor parsed = formatter.parse(str); 1386 * Instant instant = parsed.query(Instant::from); 1387 * if (parsed.query(DateTimeFormatter.parsedLeapSecond())) { 1388 * // validate leap-second is correct and apply correct smoothing 1389 * } 1390 * </pre> 1391 * @return a query that provides access to whether a leap-second was parsed 1392 */ parsedLeapSecond()1393 public static final TemporalQuery<Boolean> parsedLeapSecond() { 1394 return PARSED_LEAP_SECOND; 1395 } 1396 private static final TemporalQuery<Boolean> PARSED_LEAP_SECOND = t -> { 1397 if (t instanceof Parsed) { 1398 return ((Parsed) t).leapSecond; 1399 } else { 1400 return Boolean.FALSE; 1401 } 1402 }; 1403 1404 //----------------------------------------------------------------------- 1405 /** 1406 * Constructor. 1407 * 1408 * @param printerParser the printer/parser to use, not null 1409 * @param locale the locale to use, not null 1410 * @param decimalStyle the DecimalStyle to use, not null 1411 * @param resolverStyle the resolver style to use, not null 1412 * @param resolverFields the fields to use during resolving, null for all fields 1413 * @param chrono the chronology to use, null for no override 1414 * @param zone the zone to use, null for no override 1415 */ DateTimeFormatter(CompositePrinterParser printerParser, Locale locale, DecimalStyle decimalStyle, ResolverStyle resolverStyle, Set<TemporalField> resolverFields, Chronology chrono, ZoneId zone)1416 DateTimeFormatter(CompositePrinterParser printerParser, 1417 Locale locale, DecimalStyle decimalStyle, 1418 ResolverStyle resolverStyle, Set<TemporalField> resolverFields, 1419 Chronology chrono, ZoneId zone) { 1420 this.printerParser = Objects.requireNonNull(printerParser, "printerParser"); 1421 this.resolverFields = resolverFields; 1422 this.locale = Objects.requireNonNull(locale, "locale"); 1423 this.decimalStyle = Objects.requireNonNull(decimalStyle, "decimalStyle"); 1424 this.resolverStyle = Objects.requireNonNull(resolverStyle, "resolverStyle"); 1425 this.chrono = chrono; 1426 this.zone = zone; 1427 } 1428 1429 //----------------------------------------------------------------------- 1430 /** 1431 * Gets the locale to be used during formatting. 1432 * <p> 1433 * This is used to lookup any part of the formatter needing specific 1434 * localization, such as the text or localized pattern. 1435 * 1436 * @return the locale of this formatter, not null 1437 */ getLocale()1438 public Locale getLocale() { 1439 return locale; 1440 } 1441 1442 // Android-changed: Remove javadoc reference to #localizedBy(Locale) 1443 /** 1444 * Returns a copy of this formatter with a new locale. 1445 * <p> 1446 * This is used to lookup any part of the formatter needing specific 1447 * localization, such as the text or localized pattern. 1448 * <p> 1449 * The locale is stored as passed in, without further processing. 1450 * <p> 1451 * This instance is immutable and unaffected by this method call. 1452 * 1453 * @param locale the new locale, not null 1454 * @return a formatter based on this formatter with the requested locale, not null 1455 */ withLocale(Locale locale)1456 public DateTimeFormatter withLocale(Locale locale) { 1457 if (this.locale.equals(locale)) { 1458 return this; 1459 } 1460 return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); 1461 } 1462 1463 // Android-changed: Remove "rg" extension support in the javadoc. See http://b/228322300. 1464 /** 1465 * Returns a copy of this formatter with localized values of the locale, 1466 * calendar, decimal style and/or timezone, that superseded values in 1467 * this formatter. 1468 * <p> 1469 * This is used to lookup any part of the formatter needing specific 1470 * localization, such as the text or localized pattern. If the locale contains the 1471 * "ca" (calendar), "nu" (numbering system) and/or 1472 * "tz" (timezone) 1473 * <a href="../../util/Locale.html#def_locale_extension">Unicode extensions</a>, 1474 * the chronology, numbering system and/or the zone are overridden. 1475 * <p> 1476 * Unlike the {@link #withLocale withLocale} method, the call to this method may 1477 * produce a different formatter depending on the order of method chaining with 1478 * other withXXXX() methods. 1479 * <p> 1480 * This instance is immutable and unaffected by this method call. 1481 * 1482 * @param locale the locale, not null 1483 * @return a formatter based on this formatter with localized values of 1484 * the calendar, decimal style and/or timezone, that superseded values in this 1485 * formatter. 1486 * @see #withLocale(Locale) 1487 * @since 10 1488 */ localizedBy(Locale locale)1489 public DateTimeFormatter localizedBy(Locale locale) { 1490 if (this.locale.equals(locale)) { 1491 return this; 1492 } 1493 1494 // Check for decimalStyle/chronology/timezone in locale object 1495 Chronology c = locale.getUnicodeLocaleType("ca") != null ? 1496 Chronology.ofLocale(locale) : chrono; 1497 DecimalStyle ds = locale.getUnicodeLocaleType("nu") != null ? 1498 DecimalStyle.of(locale) : decimalStyle; 1499 String tzType = locale.getUnicodeLocaleType("tz"); 1500 ZoneId z = tzType != null ? 1501 // Android changed: Use ICU on Android. 1502 // TimeZoneNameUtility.convertLDMLShortID(tzType) 1503 Optional.ofNullable(ICU.convertToTzId(tzType)) 1504 .map(ZoneId::of) 1505 .orElse(zone) : 1506 zone; 1507 return new DateTimeFormatter(printerParser, locale, ds, resolverStyle, resolverFields, c, z); 1508 } 1509 1510 //----------------------------------------------------------------------- 1511 /** 1512 * Gets the DecimalStyle to be used during formatting. 1513 * 1514 * @return the locale of this formatter, not null 1515 */ getDecimalStyle()1516 public DecimalStyle getDecimalStyle() { 1517 return decimalStyle; 1518 } 1519 1520 /** 1521 * Returns a copy of this formatter with a new DecimalStyle. 1522 * <p> 1523 * This instance is immutable and unaffected by this method call. 1524 * 1525 * @param decimalStyle the new DecimalStyle, not null 1526 * @return a formatter based on this formatter with the requested DecimalStyle, not null 1527 */ withDecimalStyle(DecimalStyle decimalStyle)1528 public DateTimeFormatter withDecimalStyle(DecimalStyle decimalStyle) { 1529 if (this.decimalStyle.equals(decimalStyle)) { 1530 return this; 1531 } 1532 return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); 1533 } 1534 1535 //----------------------------------------------------------------------- 1536 /** 1537 * Gets the overriding chronology to be used during formatting. 1538 * <p> 1539 * This returns the override chronology, used to convert dates. 1540 * By default, a formatter has no override chronology, returning null. 1541 * See {@link #withChronology(Chronology)} for more details on overriding. 1542 * 1543 * @return the override chronology of this formatter, null if no override 1544 */ getChronology()1545 public Chronology getChronology() { 1546 return chrono; 1547 } 1548 1549 /** 1550 * Returns a copy of this formatter with a new override chronology. 1551 * <p> 1552 * This returns a formatter with similar state to this formatter but 1553 * with the override chronology set. 1554 * By default, a formatter has no override chronology, returning null. 1555 * <p> 1556 * If an override is added, then any date that is formatted or parsed will be affected. 1557 * <p> 1558 * When formatting, if the temporal object contains a date, then it will 1559 * be converted to a date in the override chronology. 1560 * Whether the temporal contains a date is determined by querying the 1561 * {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. 1562 * Any time or zone will be retained unaltered unless overridden. 1563 * <p> 1564 * If the temporal object does not contain a date, but does contain one 1565 * or more {@code ChronoField} date fields, then a {@code DateTimeException} 1566 * is thrown. In all other cases, the override chronology is added to the temporal, 1567 * replacing any previous chronology, but without changing the date/time. 1568 * <p> 1569 * When parsing, there are two distinct cases to consider. 1570 * If a chronology has been parsed directly from the text, perhaps because 1571 * {@link DateTimeFormatterBuilder#appendChronologyId()} was used, then 1572 * this override chronology has no effect. 1573 * If no zone has been parsed, then this override chronology will be used 1574 * to interpret the {@code ChronoField} values into a date according to the 1575 * date resolving rules of the chronology. 1576 * <p> 1577 * This instance is immutable and unaffected by this method call. 1578 * 1579 * @param chrono the new chronology, null if no override 1580 * @return a formatter based on this formatter with the requested override chronology, not null 1581 */ withChronology(Chronology chrono)1582 public DateTimeFormatter withChronology(Chronology chrono) { 1583 if (Objects.equals(this.chrono, chrono)) { 1584 return this; 1585 } 1586 return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); 1587 } 1588 1589 //----------------------------------------------------------------------- 1590 /** 1591 * Gets the overriding zone to be used during formatting. 1592 * <p> 1593 * This returns the override zone, used to convert instants. 1594 * By default, a formatter has no override zone, returning null. 1595 * See {@link #withZone(ZoneId)} for more details on overriding. 1596 * 1597 * @return the override zone of this formatter, null if no override 1598 */ getZone()1599 public ZoneId getZone() { 1600 return zone; 1601 } 1602 1603 /** 1604 * Returns a copy of this formatter with a new override zone. 1605 * <p> 1606 * This returns a formatter with similar state to this formatter but 1607 * with the override zone set. 1608 * By default, a formatter has no override zone, returning null. 1609 * <p> 1610 * If an override is added, then any instant that is formatted or parsed will be affected. 1611 * <p> 1612 * When formatting, if the temporal object contains an instant, then it will 1613 * be converted to a zoned date-time using the override zone. 1614 * Whether the temporal is an instant is determined by querying the 1615 * {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS} field. 1616 * If the input has a chronology then it will be retained unless overridden. 1617 * If the input does not have a chronology, such as {@code Instant}, then 1618 * the ISO chronology will be used. 1619 * <p> 1620 * If the temporal object does not contain an instant, but does contain 1621 * an offset then an additional check is made. If the normalized override 1622 * zone is an offset that differs from the offset of the temporal, then 1623 * a {@code DateTimeException} is thrown. In all other cases, the override 1624 * zone is added to the temporal, replacing any previous zone, but without 1625 * changing the date/time. 1626 * <p> 1627 * When parsing, there are two distinct cases to consider. 1628 * If a zone has been parsed directly from the text, perhaps because 1629 * {@link DateTimeFormatterBuilder#appendZoneId()} was used, then 1630 * this override zone has no effect. 1631 * If no zone has been parsed, then this override zone will be included in 1632 * the result of the parse where it can be used to build instants and date-times. 1633 * <p> 1634 * This instance is immutable and unaffected by this method call. 1635 * 1636 * @param zone the new override zone, null if no override 1637 * @return a formatter based on this formatter with the requested override zone, not null 1638 */ withZone(ZoneId zone)1639 public DateTimeFormatter withZone(ZoneId zone) { 1640 if (Objects.equals(this.zone, zone)) { 1641 return this; 1642 } 1643 return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); 1644 } 1645 1646 //----------------------------------------------------------------------- 1647 /** 1648 * Gets the resolver style to use during parsing. 1649 * <p> 1650 * This returns the resolver style, used during the second phase of parsing 1651 * when fields are resolved into dates and times. 1652 * By default, a formatter has the {@link ResolverStyle#SMART SMART} resolver style. 1653 * See {@link #withResolverStyle(ResolverStyle)} for more details. 1654 * 1655 * @return the resolver style of this formatter, not null 1656 */ getResolverStyle()1657 public ResolverStyle getResolverStyle() { 1658 return resolverStyle; 1659 } 1660 1661 /** 1662 * Returns a copy of this formatter with a new resolver style. 1663 * <p> 1664 * This returns a formatter with similar state to this formatter but 1665 * with the resolver style set. By default, a formatter has the 1666 * {@link ResolverStyle#SMART SMART} resolver style. 1667 * <p> 1668 * Changing the resolver style only has an effect during parsing. 1669 * Parsing a text string occurs in two phases. 1670 * Phase 1 is a basic text parse according to the fields added to the builder. 1671 * Phase 2 resolves the parsed field-value pairs into date and/or time objects. 1672 * The resolver style is used to control how phase 2, resolving, happens. 1673 * See {@code ResolverStyle} for more information on the options available. 1674 * <p> 1675 * This instance is immutable and unaffected by this method call. 1676 * 1677 * @param resolverStyle the new resolver style, not null 1678 * @return a formatter based on this formatter with the requested resolver style, not null 1679 */ withResolverStyle(ResolverStyle resolverStyle)1680 public DateTimeFormatter withResolverStyle(ResolverStyle resolverStyle) { 1681 Objects.requireNonNull(resolverStyle, "resolverStyle"); 1682 if (Objects.equals(this.resolverStyle, resolverStyle)) { 1683 return this; 1684 } 1685 return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); 1686 } 1687 1688 //----------------------------------------------------------------------- 1689 /** 1690 * Gets the resolver fields to use during parsing. 1691 * <p> 1692 * This returns the resolver fields, used during the second phase of parsing 1693 * when fields are resolved into dates and times. 1694 * By default, a formatter has no resolver fields, and thus returns null. 1695 * See {@link #withResolverFields(Set)} for more details. 1696 * 1697 * @return the immutable set of resolver fields of this formatter, null if no fields 1698 */ getResolverFields()1699 public Set<TemporalField> getResolverFields() { 1700 return resolverFields; 1701 } 1702 1703 /** 1704 * Returns a copy of this formatter with a new set of resolver fields. 1705 * <p> 1706 * This returns a formatter with similar state to this formatter but with 1707 * the resolver fields set. By default, a formatter has no resolver fields. 1708 * <p> 1709 * Changing the resolver fields only has an effect during parsing. 1710 * Parsing a text string occurs in two phases. 1711 * Phase 1 is a basic text parse according to the fields added to the builder. 1712 * Phase 2 resolves the parsed field-value pairs into date and/or time objects. 1713 * The resolver fields are used to filter the field-value pairs between phase 1 and 2. 1714 * <p> 1715 * This can be used to select between two or more ways that a date or time might 1716 * be resolved. For example, if the formatter consists of year, month, day-of-month 1717 * and day-of-year, then there are two ways to resolve a date. 1718 * Calling this method with the arguments {@link ChronoField#YEAR YEAR} and 1719 * {@link ChronoField#DAY_OF_YEAR DAY_OF_YEAR} will ensure that the date is 1720 * resolved using the year and day-of-year, effectively meaning that the month 1721 * and day-of-month are ignored during the resolving phase. 1722 * <p> 1723 * In a similar manner, this method can be used to ignore secondary fields that 1724 * would otherwise be cross-checked. For example, if the formatter consists of year, 1725 * month, day-of-month and day-of-week, then there is only one way to resolve a 1726 * date, but the parsed value for day-of-week will be cross-checked against the 1727 * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR}, 1728 * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and 1729 * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is 1730 * resolved correctly, but without any cross-check for the day-of-week. 1731 * <p> 1732 * In implementation terms, this method behaves as follows. The result of the 1733 * parsing phase can be considered to be a map of field to value. The behavior 1734 * of this method is to cause that map to be filtered between phase 1 and 2, 1735 * removing all fields other than those specified as arguments to this method. 1736 * <p> 1737 * This instance is immutable and unaffected by this method call. 1738 * 1739 * @param resolverFields the new set of resolver fields, null if no fields 1740 * @return a formatter based on this formatter with the requested resolver style, not null 1741 */ withResolverFields(TemporalField... resolverFields)1742 public DateTimeFormatter withResolverFields(TemporalField... resolverFields) { 1743 Set<TemporalField> fields = null; 1744 if (resolverFields != null) { 1745 // Set.of cannot be used because it is hostile to nulls and duplicate elements 1746 fields = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(resolverFields))); 1747 } 1748 if (Objects.equals(this.resolverFields, fields)) { 1749 return this; 1750 } 1751 return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, fields, chrono, zone); 1752 } 1753 1754 /** 1755 * Returns a copy of this formatter with a new set of resolver fields. 1756 * <p> 1757 * This returns a formatter with similar state to this formatter but with 1758 * the resolver fields set. By default, a formatter has no resolver fields. 1759 * <p> 1760 * Changing the resolver fields only has an effect during parsing. 1761 * Parsing a text string occurs in two phases. 1762 * Phase 1 is a basic text parse according to the fields added to the builder. 1763 * Phase 2 resolves the parsed field-value pairs into date and/or time objects. 1764 * The resolver fields are used to filter the field-value pairs between phase 1 and 2. 1765 * <p> 1766 * This can be used to select between two or more ways that a date or time might 1767 * be resolved. For example, if the formatter consists of year, month, day-of-month 1768 * and day-of-year, then there are two ways to resolve a date. 1769 * Calling this method with the arguments {@link ChronoField#YEAR YEAR} and 1770 * {@link ChronoField#DAY_OF_YEAR DAY_OF_YEAR} will ensure that the date is 1771 * resolved using the year and day-of-year, effectively meaning that the month 1772 * and day-of-month are ignored during the resolving phase. 1773 * <p> 1774 * In a similar manner, this method can be used to ignore secondary fields that 1775 * would otherwise be cross-checked. For example, if the formatter consists of year, 1776 * month, day-of-month and day-of-week, then there is only one way to resolve a 1777 * date, but the parsed value for day-of-week will be cross-checked against the 1778 * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR}, 1779 * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and 1780 * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is 1781 * resolved correctly, but without any cross-check for the day-of-week. 1782 * <p> 1783 * In implementation terms, this method behaves as follows. The result of the 1784 * parsing phase can be considered to be a map of field to value. The behavior 1785 * of this method is to cause that map to be filtered between phase 1 and 2, 1786 * removing all fields other than those specified as arguments to this method. 1787 * <p> 1788 * This instance is immutable and unaffected by this method call. 1789 * 1790 * @param resolverFields the new set of resolver fields, null if no fields 1791 * @return a formatter based on this formatter with the requested resolver style, not null 1792 */ withResolverFields(Set<TemporalField> resolverFields)1793 public DateTimeFormatter withResolverFields(Set<TemporalField> resolverFields) { 1794 if (Objects.equals(this.resolverFields, resolverFields)) { 1795 return this; 1796 } 1797 if (resolverFields != null) { 1798 resolverFields = Collections.unmodifiableSet(new HashSet<>(resolverFields)); 1799 } 1800 return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); 1801 } 1802 1803 //----------------------------------------------------------------------- 1804 /** 1805 * Formats a date-time object using this formatter. 1806 * <p> 1807 * This formats the date-time to a String using the rules of the formatter. 1808 * 1809 * @param temporal the temporal object to format, not null 1810 * @return the formatted string, not null 1811 * @throws DateTimeException if an error occurs during formatting 1812 */ format(TemporalAccessor temporal)1813 public String format(TemporalAccessor temporal) { 1814 StringBuilder buf = new StringBuilder(32); 1815 formatTo(temporal, buf); 1816 return buf.toString(); 1817 } 1818 1819 //----------------------------------------------------------------------- 1820 /** 1821 * Formats a date-time object to an {@code Appendable} using this formatter. 1822 * <p> 1823 * This outputs the formatted date-time to the specified destination. 1824 * {@link Appendable} is a general purpose interface that is implemented by all 1825 * key character output classes including {@code StringBuffer}, {@code StringBuilder}, 1826 * {@code PrintStream} and {@code Writer}. 1827 * <p> 1828 * Although {@code Appendable} methods throw an {@code IOException}, this method does not. 1829 * Instead, any {@code IOException} is wrapped in a runtime exception. 1830 * 1831 * @param temporal the temporal object to format, not null 1832 * @param appendable the appendable to format to, not null 1833 * @throws DateTimeException if an error occurs during formatting 1834 */ formatTo(TemporalAccessor temporal, Appendable appendable)1835 public void formatTo(TemporalAccessor temporal, Appendable appendable) { 1836 Objects.requireNonNull(temporal, "temporal"); 1837 Objects.requireNonNull(appendable, "appendable"); 1838 try { 1839 DateTimePrintContext context = new DateTimePrintContext(temporal, this); 1840 if (appendable instanceof StringBuilder) { 1841 printerParser.format(context, (StringBuilder) appendable); 1842 } else { 1843 // buffer output to avoid writing to appendable in case of error 1844 StringBuilder buf = new StringBuilder(32); 1845 printerParser.format(context, buf); 1846 appendable.append(buf); 1847 } 1848 } catch (IOException ex) { 1849 throw new DateTimeException(ex.getMessage(), ex); 1850 } 1851 } 1852 1853 //----------------------------------------------------------------------- 1854 /** 1855 * Fully parses the text producing a temporal object. 1856 * <p> 1857 * This parses the entire text producing a temporal object. 1858 * It is typically more useful to use {@link #parse(CharSequence, TemporalQuery)}. 1859 * The result of this method is {@code TemporalAccessor} which has been resolved, 1860 * applying basic validation checks to help ensure a valid date-time. 1861 * <p> 1862 * If the parse completes without reading the entire length of the text, 1863 * or a problem occurs during parsing or merging, then an exception is thrown. 1864 * 1865 * @param text the text to parse, not null 1866 * @return the parsed temporal object, not null 1867 * @throws DateTimeParseException if unable to parse the requested result 1868 */ parse(CharSequence text)1869 public TemporalAccessor parse(CharSequence text) { 1870 Objects.requireNonNull(text, "text"); 1871 try { 1872 return parseResolved0(text, null); 1873 } catch (DateTimeParseException ex) { 1874 throw ex; 1875 } catch (RuntimeException ex) { 1876 throw createError(text, ex); 1877 } 1878 } 1879 1880 /** 1881 * Parses the text using this formatter, providing control over the text position. 1882 * <p> 1883 * This parses the text without requiring the parse to start from the beginning 1884 * of the string or finish at the end. 1885 * The result of this method is {@code TemporalAccessor} which has been resolved, 1886 * applying basic validation checks to help ensure a valid date-time. 1887 * <p> 1888 * The text will be parsed from the specified start {@code ParsePosition}. 1889 * The entire length of the text does not have to be parsed, the {@code ParsePosition} 1890 * will be updated with the index at the end of parsing. 1891 * <p> 1892 * The operation of this method is slightly different to similar methods using 1893 * {@code ParsePosition} on {@code java.text.Format}. That class will return 1894 * errors using the error index on the {@code ParsePosition}. By contrast, this 1895 * method will throw a {@link DateTimeParseException} if an error occurs, with 1896 * the exception containing the error index. 1897 * This change in behavior is necessary due to the increased complexity of 1898 * parsing and resolving dates/times in this API. 1899 * <p> 1900 * If the formatter parses the same field more than once with different values, 1901 * the result will be an error. 1902 * 1903 * @param text the text to parse, not null 1904 * @param position the position to parse from, updated with length parsed 1905 * and the index of any error, not null 1906 * @return the parsed temporal object, not null 1907 * @throws DateTimeParseException if unable to parse the requested result 1908 * @throws IndexOutOfBoundsException if the position is invalid 1909 */ parse(CharSequence text, ParsePosition position)1910 public TemporalAccessor parse(CharSequence text, ParsePosition position) { 1911 Objects.requireNonNull(text, "text"); 1912 Objects.requireNonNull(position, "position"); 1913 try { 1914 return parseResolved0(text, position); 1915 } catch (DateTimeParseException | IndexOutOfBoundsException ex) { 1916 throw ex; 1917 } catch (RuntimeException ex) { 1918 throw createError(text, ex); 1919 } 1920 } 1921 1922 //----------------------------------------------------------------------- 1923 /** 1924 * Fully parses the text producing an object of the specified type. 1925 * <p> 1926 * Most applications should use this method for parsing. 1927 * It parses the entire text to produce the required date-time. 1928 * The query is typically a method reference to a {@code from(TemporalAccessor)} method. 1929 * For example: 1930 * <pre> 1931 * LocalDateTime dt = parser.parse(str, LocalDateTime::from); 1932 * </pre> 1933 * If the parse completes without reading the entire length of the text, 1934 * or a problem occurs during parsing or merging, then an exception is thrown. 1935 * 1936 * @param <T> the type of the parsed date-time 1937 * @param text the text to parse, not null 1938 * @param query the query defining the type to parse to, not null 1939 * @return the parsed date-time, not null 1940 * @throws DateTimeParseException if unable to parse the requested result 1941 */ parse(CharSequence text, TemporalQuery<T> query)1942 public <T> T parse(CharSequence text, TemporalQuery<T> query) { 1943 Objects.requireNonNull(text, "text"); 1944 Objects.requireNonNull(query, "query"); 1945 try { 1946 return parseResolved0(text, null).query(query); 1947 } catch (DateTimeParseException ex) { 1948 throw ex; 1949 } catch (RuntimeException ex) { 1950 throw createError(text, ex); 1951 } 1952 } 1953 1954 /** 1955 * Fully parses the text producing an object of one of the specified types. 1956 * <p> 1957 * This parse method is convenient for use when the parser can handle optional elements. 1958 * For example, a pattern of 'uuuu-MM-dd HH.mm[ VV]' can be fully parsed to a {@code ZonedDateTime}, 1959 * or partially parsed to a {@code LocalDateTime}. 1960 * The queries must be specified in order, starting from the best matching full-parse option 1961 * and ending with the worst matching minimal parse option. 1962 * The query is typically a method reference to a {@code from(TemporalAccessor)} method. 1963 * <p> 1964 * The result is associated with the first type that successfully parses. 1965 * Normally, applications will use {@code instanceof} to check the result. 1966 * For example: 1967 * <pre> 1968 * TemporalAccessor dt = parser.parseBest(str, ZonedDateTime::from, LocalDateTime::from); 1969 * if (dt instanceof ZonedDateTime) { 1970 * ... 1971 * } else { 1972 * ... 1973 * } 1974 * </pre> 1975 * If the parse completes without reading the entire length of the text, 1976 * or a problem occurs during parsing or merging, then an exception is thrown. 1977 * 1978 * @param text the text to parse, not null 1979 * @param queries the queries defining the types to attempt to parse to, 1980 * must implement {@code TemporalAccessor}, not null 1981 * @return the parsed date-time, not null 1982 * @throws IllegalArgumentException if less than 2 types are specified 1983 * @throws DateTimeParseException if unable to parse the requested result 1984 */ parseBest(CharSequence text, TemporalQuery<?>... queries)1985 public TemporalAccessor parseBest(CharSequence text, TemporalQuery<?>... queries) { 1986 Objects.requireNonNull(text, "text"); 1987 Objects.requireNonNull(queries, "queries"); 1988 if (queries.length < 2) { 1989 throw new IllegalArgumentException("At least two queries must be specified"); 1990 } 1991 try { 1992 TemporalAccessor resolved = parseResolved0(text, null); 1993 for (TemporalQuery<?> query : queries) { 1994 try { 1995 return (TemporalAccessor) resolved.query(query); 1996 } catch (RuntimeException ex) { 1997 // continue 1998 } 1999 } 2000 throw new DateTimeException("Unable to convert parsed text using any of the specified queries"); 2001 } catch (DateTimeParseException ex) { 2002 throw ex; 2003 } catch (RuntimeException ex) { 2004 throw createError(text, ex); 2005 } 2006 } 2007 createError(CharSequence text, RuntimeException ex)2008 private DateTimeParseException createError(CharSequence text, RuntimeException ex) { 2009 String abbr; 2010 if (text.length() > 64) { 2011 abbr = text.subSequence(0, 64).toString() + "..."; 2012 } else { 2013 abbr = text.toString(); 2014 } 2015 return new DateTimeParseException("Text '" + abbr + "' could not be parsed: " + ex.getMessage(), text, 0, ex); 2016 } 2017 2018 //----------------------------------------------------------------------- 2019 /** 2020 * Parses and resolves the specified text. 2021 * <p> 2022 * This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed. 2023 * 2024 * @param text the text to parse, not null 2025 * @param position the position to parse from, updated with length parsed 2026 * and the index of any error, null if parsing whole string 2027 * @return the resolved result of the parse, not null 2028 * @throws DateTimeParseException if the parse fails 2029 * @throws DateTimeException if an error occurs while resolving the date or time 2030 * @throws IndexOutOfBoundsException if the position is invalid 2031 */ parseResolved0(final CharSequence text, final ParsePosition position)2032 private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) { 2033 ParsePosition pos = (position != null ? position : new ParsePosition(0)); 2034 DateTimeParseContext context = parseUnresolved0(text, pos); 2035 if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) { 2036 String abbr; 2037 if (text.length() > 64) { 2038 abbr = text.subSequence(0, 64).toString() + "..."; 2039 } else { 2040 abbr = text.toString(); 2041 } 2042 if (pos.getErrorIndex() >= 0) { 2043 throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " + 2044 pos.getErrorIndex(), text, pos.getErrorIndex()); 2045 } else { 2046 throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " + 2047 pos.getIndex(), text, pos.getIndex()); 2048 } 2049 } 2050 return context.toResolved(resolverStyle, resolverFields); 2051 } 2052 2053 /** 2054 * Parses the text using this formatter, without resolving the result, intended 2055 * for advanced use cases. 2056 * <p> 2057 * Parsing is implemented as a two-phase operation. 2058 * First, the text is parsed using the layout defined by the formatter, producing 2059 * a {@code Map} of field to value, a {@code ZoneId} and a {@code Chronology}. 2060 * Second, the parsed data is <em>resolved</em>, by validating, combining and 2061 * simplifying the various fields into more useful ones. 2062 * This method performs the parsing stage but not the resolving stage. 2063 * <p> 2064 * The result of this method is {@code TemporalAccessor} which represents the 2065 * data as seen in the input. Values are not validated, thus parsing a date string 2066 * of '2012-00-65' would result in a temporal with three fields - year of '2012', 2067 * month of '0' and day-of-month of '65'. 2068 * <p> 2069 * The text will be parsed from the specified start {@code ParsePosition}. 2070 * The entire length of the text does not have to be parsed, the {@code ParsePosition} 2071 * will be updated with the index at the end of parsing. 2072 * <p> 2073 * Errors are returned using the error index field of the {@code ParsePosition} 2074 * instead of {@code DateTimeParseException}. 2075 * The returned error index will be set to an index indicative of the error. 2076 * Callers must check for errors before using the result. 2077 * <p> 2078 * If the formatter parses the same field more than once with different values, 2079 * the result will be an error. 2080 * <p> 2081 * This method is intended for advanced use cases that need access to the 2082 * internal state during parsing. Typical application code should use 2083 * {@link #parse(CharSequence, TemporalQuery)} or the parse method on the target type. 2084 * 2085 * @param text the text to parse, not null 2086 * @param position the position to parse from, updated with length parsed 2087 * and the index of any error, not null 2088 * @return the parsed text, null if the parse results in an error 2089 * @throws DateTimeException if some problem occurs during parsing 2090 * @throws IndexOutOfBoundsException if the position is invalid 2091 */ parseUnresolved(CharSequence text, ParsePosition position)2092 public TemporalAccessor parseUnresolved(CharSequence text, ParsePosition position) { 2093 DateTimeParseContext context = parseUnresolved0(text, position); 2094 if (context == null) { 2095 return null; 2096 } 2097 return context.toUnresolved(); 2098 } 2099 parseUnresolved0(CharSequence text, ParsePosition position)2100 private DateTimeParseContext parseUnresolved0(CharSequence text, ParsePosition position) { 2101 Objects.requireNonNull(text, "text"); 2102 Objects.requireNonNull(position, "position"); 2103 DateTimeParseContext context = new DateTimeParseContext(this); 2104 int pos = position.getIndex(); 2105 pos = printerParser.parse(context, text, pos); 2106 if (pos < 0) { 2107 position.setErrorIndex(~pos); // index not updated from input 2108 return null; 2109 } 2110 position.setIndex(pos); // errorIndex not updated from input 2111 return context; 2112 } 2113 2114 //----------------------------------------------------------------------- 2115 /** 2116 * Returns the formatter as a composite printer parser. 2117 * 2118 * @param optional whether the printer/parser should be optional 2119 * @return the printer/parser, not null 2120 */ toPrinterParser(boolean optional)2121 CompositePrinterParser toPrinterParser(boolean optional) { 2122 return printerParser.withOptional(optional); 2123 } 2124 2125 /** 2126 * Returns this formatter as a {@code java.text.Format} instance. 2127 * <p> 2128 * The returned {@link Format} instance will format any {@link TemporalAccessor} 2129 * and parses to a resolved {@link TemporalAccessor}. 2130 * <p> 2131 * Exceptions will follow the definitions of {@code Format}, see those methods 2132 * for details about {@code IllegalArgumentException} during formatting and 2133 * {@code ParseException} or null during parsing. 2134 * The format does not support attributing of the returned format string. 2135 * 2136 * @return this formatter as a classic format instance, not null 2137 */ toFormat()2138 public Format toFormat() { 2139 return new ClassicFormat(this, null); 2140 } 2141 2142 /** 2143 * Returns this formatter as a {@code java.text.Format} instance that will 2144 * parse using the specified query. 2145 * <p> 2146 * The returned {@link Format} instance will format any {@link TemporalAccessor} 2147 * and parses to the type specified. 2148 * The type must be one that is supported by {@link #parse}. 2149 * <p> 2150 * Exceptions will follow the definitions of {@code Format}, see those methods 2151 * for details about {@code IllegalArgumentException} during formatting and 2152 * {@code ParseException} or null during parsing. 2153 * The format does not support attributing of the returned format string. 2154 * 2155 * @param parseQuery the query defining the type to parse to, not null 2156 * @return this formatter as a classic format instance, not null 2157 */ toFormat(TemporalQuery<?> parseQuery)2158 public Format toFormat(TemporalQuery<?> parseQuery) { 2159 Objects.requireNonNull(parseQuery, "parseQuery"); 2160 return new ClassicFormat(this, parseQuery); 2161 } 2162 2163 //----------------------------------------------------------------------- 2164 /** 2165 * Returns a description of the underlying formatters. 2166 * 2167 * @return a description of this formatter, not null 2168 */ 2169 @Override toString()2170 public String toString() { 2171 String pattern = printerParser.toString(); 2172 pattern = pattern.startsWith("[") ? pattern : pattern.substring(1, pattern.length() - 1); 2173 return pattern; 2174 // TODO: Fix tests to not depend on toString() 2175 // return "DateTimeFormatter[" + locale + 2176 // (chrono != null ? "," + chrono : "") + 2177 // (zone != null ? "," + zone : "") + 2178 // pattern + "]"; 2179 } 2180 2181 //----------------------------------------------------------------------- 2182 /** 2183 * Implements the classic Java Format API. 2184 * @serial exclude 2185 */ 2186 @SuppressWarnings("serial") // not actually serializable 2187 static class ClassicFormat extends Format { 2188 /** The formatter. */ 2189 private final DateTimeFormatter formatter; 2190 /** The type to be parsed. */ 2191 private final TemporalQuery<?> parseType; 2192 /** Constructor. */ ClassicFormat(DateTimeFormatter formatter, TemporalQuery<?> parseType)2193 public ClassicFormat(DateTimeFormatter formatter, TemporalQuery<?> parseType) { 2194 this.formatter = formatter; 2195 this.parseType = parseType; 2196 } 2197 2198 @Override format(Object obj, StringBuffer toAppendTo, FieldPosition pos)2199 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 2200 Objects.requireNonNull(obj, "obj"); 2201 Objects.requireNonNull(toAppendTo, "toAppendTo"); 2202 Objects.requireNonNull(pos, "pos"); 2203 if (obj instanceof TemporalAccessor == false) { 2204 throw new IllegalArgumentException("Format target must implement TemporalAccessor"); 2205 } 2206 pos.setBeginIndex(0); 2207 pos.setEndIndex(0); 2208 try { 2209 formatter.formatTo((TemporalAccessor) obj, toAppendTo); 2210 } catch (RuntimeException ex) { 2211 throw new IllegalArgumentException(ex.getMessage(), ex); 2212 } 2213 return toAppendTo; 2214 } 2215 @Override parseObject(String text)2216 public Object parseObject(String text) throws ParseException { 2217 Objects.requireNonNull(text, "text"); 2218 try { 2219 if (parseType == null) { 2220 return formatter.parseResolved0(text, null); 2221 } 2222 return formatter.parse(text, parseType); 2223 } catch (DateTimeParseException ex) { 2224 throw new ParseException(ex.getMessage(), ex.getErrorIndex()); 2225 } catch (RuntimeException ex) { 2226 throw (ParseException) new ParseException(ex.getMessage(), 0).initCause(ex); 2227 } 2228 } 2229 @Override parseObject(String text, ParsePosition pos)2230 public Object parseObject(String text, ParsePosition pos) { 2231 Objects.requireNonNull(text, "text"); 2232 DateTimeParseContext context; 2233 try { 2234 context = formatter.parseUnresolved0(text, pos); 2235 } catch (IndexOutOfBoundsException ex) { 2236 if (pos.getErrorIndex() < 0) { 2237 pos.setErrorIndex(0); 2238 } 2239 return null; 2240 } 2241 if (context == null) { 2242 if (pos.getErrorIndex() < 0) { 2243 pos.setErrorIndex(0); 2244 } 2245 return null; 2246 } 2247 try { 2248 TemporalAccessor resolved = context.toResolved(formatter.resolverStyle, formatter.resolverFields); 2249 if (parseType == null) { 2250 return resolved; 2251 } 2252 return resolved.query(parseType); 2253 } catch (RuntimeException ex) { 2254 pos.setErrorIndex(0); 2255 return null; 2256 } 2257 } 2258 } 2259 2260 } 2261