< prev index next >

src/java.base/share/classes/java/time/format/DateTimeFormatter.java

Print this page
rev 47925 : 8191349: Add a new method in j.t.f.DateTimeFormatter to reflect Unicode extensions
Reviewed-by:

*** 128,143 **** * Chronology, ZoneId, and DecimalStyle. * <p> * The {@link #withLocale withLocale} method returns a new formatter that * overrides the locale. The locale affects some aspects of formatting and * parsing. For example, the {@link #ofLocalizedDate ofLocalizedDate} provides a ! * formatter that uses the locale specific date format. If the locale contains ! * "ca" (calendar), "rg" (region override) and/or "tz" (timezone) ! * <a href="../../util/Locale.html#def_locale_extension">Unicode extensions</a>, ! * the chronology and/or the zone are also overridden. If both "ca" and "rg" are ! * specified, the chronology from the "ca" extension supersedes the implicit one ! * from the "rg" extension. * <p> * The {@link #withChronology withChronology} method returns a new formatter * that overrides the chronology. If overridden, the date-time value is * converted to the chronology before formatting. During parsing the date-time * value is converted to the chronology before it is returned. --- 128,138 ---- * Chronology, ZoneId, and DecimalStyle. * <p> * The {@link #withLocale withLocale} method returns a new formatter that * overrides the locale. The locale affects some aspects of formatting and * parsing. For example, the {@link #ofLocalizedDate ofLocalizedDate} provides a ! * formatter that uses the locale specific date format. * <p> * The {@link #withChronology withChronology} method returns a new formatter * that overrides the chronology. If overridden, the date-time value is * converted to the chronology before formatting. During parsing the date-time * value is converted to the chronology before it is returned.
*** 552,568 **** * <a href="#patterns">pattern of letters and symbols</a> * as described in the class documentation. * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. * <p> * The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. ! * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter. * Alternatively use the {@link #ofPattern(String, Locale)} variant of this method. - * If the default locale contains "ca" (calendar), "rg" (region override) and/or "tz" (timezone) - * <a href="../../util/Locale.html#def_locale_extension">Unicode extensions</a>, - * the chronology and/or the zone are overridden. If both "ca" and "rg" are - * specified, the chronology from the "ca" extension supersedes the implicit one - * from the "rg" extension. * <p> * The returned formatter has no override chronology or zone. * It uses {@link ResolverStyle#SMART SMART} resolver style. * * @param pattern the pattern to use, not null --- 547,558 ---- * <a href="#patterns">pattern of letters and symbols</a> * as described in the class documentation. * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. * <p> * The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. ! * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter * Alternatively use the {@link #ofPattern(String, Locale)} variant of this method. * <p> * The returned formatter has no override chronology or zone. * It uses {@link ResolverStyle#SMART SMART} resolver style. * * @param pattern the pattern to use, not null
*** 581,596 **** * <a href="#patterns">pattern of letters and symbols</a> * as described in the class documentation. * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. * <p> * The formatter will use the specified locale. ! * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter. ! * If the specified locale contains "ca" (calendar), "rg" (region override) and/or "tz" (timezone) ! * <a href="../../util/Locale.html#def_locale_extension">Unicode extensions</a>, ! * the chronology and/or the zone are overridden. If both "ca" and "rg" are ! * specified, the chronology from the "ca" extension supersedes the implicit one ! * from the "rg" extension. * <p> * The returned formatter has no override chronology or zone. * It uses {@link ResolverStyle#SMART SMART} resolver style. * * @param pattern the pattern to use, not null --- 571,581 ---- * <a href="#patterns">pattern of letters and symbols</a> * as described in the class documentation. * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. * <p> * The formatter will use the specified locale. ! * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter * <p> * The returned formatter has no override chronology or zone. * It uses {@link ResolverStyle#SMART SMART} resolver style. * * @param pattern the pattern to use, not null
*** 1455,1492 **** /** * Returns a copy of this formatter with a new locale. * <p> * This is used to lookup any part of the formatter needing specific ! * localization, such as the text or localized pattern. If the new ! * locale contains "ca" (calendar), "rg" (region override) and/or "tz" (timezone) ! * <a href="../../util/Locale.html#def_locale_extension">Unicode extensions</a>, ! * the chronology and/or the zone are also overridden. If both "ca" and "rg" are ! * specified, the chronology from the "ca" extension supersedes the implicit one ! * from the "rg" extension. * <p> * This instance is immutable and unaffected by this method call. * * @param locale the new locale, not null * @return a formatter based on this formatter with the requested locale, not null */ public DateTimeFormatter withLocale(Locale locale) { if (this.locale.equals(locale)) { return this; } ! // Check for chronology/timezone in locale object Chronology c = locale.getUnicodeLocaleType("ca") != null ? Chronology.ofLocale(locale) : chrono; String tzType = locale.getUnicodeLocaleType("tz"); ZoneId z = tzType != null ? TimeZoneNameUtility.convertLDMLShortID(tzType) .map(ZoneId::of) .orElse(zone) : zone; ! return new DateTimeFormatter(printerParser, locale, decimalStyle, ! resolverStyle, resolverFields, c, z); } //----------------------------------------------------------------------- /** * Gets the DecimalStyle to be used during formatting. --- 1440,1516 ---- /** * Returns a copy of this formatter with a new locale. * <p> * This is used to lookup any part of the formatter needing specific ! * localization, such as the text or localized pattern. ! * <p> ! * The locale is stored as passed in, without further processing. ! * If the locale has unicode extensions, they may be used later in text ! * processing. To set the chronology, time-zone and decimal style from ! * unicode extensions, see {@link #localizedBy localizedBy()}. * <p> * This instance is immutable and unaffected by this method call. * * @param locale the new locale, not null * @return a formatter based on this formatter with the requested locale, not null + * @see #localizedBy(Locale) */ public DateTimeFormatter withLocale(Locale locale) { if (this.locale.equals(locale)) { return this; } + return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); + } + + /** + * Returns a copy of this formatter with localized values of the locale, + * calendar, region, decimal style and/or timezone, that supercede values in + * this formatter. + * <p> + * This is used to lookup any part of the formatter needing specific + * localization, such as the text or localized pattern. If the locale contains the + * "ca" (calendar), "nu" (numbering system), "rg" (region override), and/or + * "tz" (timezone) + * <a href="../../util/Locale.html#def_locale_extension">Unicode extensions</a>, + * the chronology, numbering system and/or the zone are overridden. If both "ca" + * and "rg" are specified, the chronology from the "ca" extension supersedes the + * implicit one from the "rg" extension. Same is true for the "nu" extension. + * <p> + * Unlike the {@link #withLocale withLocale} method, the call to this method may + * produce a different formatter depending on the order of method chaining with + * other withXXXX() methods. + * <p> + * This instance is immutable and unaffected by this method call. + * + * @param locale the locale, not null + * @return a formatter based on this formatter with localized values of + * the calendar, decimal style and/or timezone, that supercede values in this + * formatter. + * @see #withLocale(Locale) + * @since 10 + */ + public DateTimeFormatter localizedBy(Locale locale) { + if (this.locale.equals(locale)) { + return this; + } ! // Check for decimalStyle/chronology/timezone in locale object Chronology c = locale.getUnicodeLocaleType("ca") != null ? Chronology.ofLocale(locale) : chrono; + DecimalStyle ds = locale.getUnicodeLocaleType("nu") != null ? + DecimalStyle.of(locale) : decimalStyle; String tzType = locale.getUnicodeLocaleType("tz"); ZoneId z = tzType != null ? TimeZoneNameUtility.convertLDMLShortID(tzType) .map(ZoneId::of) .orElse(zone) : zone; ! return withLocale(locale) ! .withChronology(c) ! .withDecimalStyle(ds) ! .withZone(z); } //----------------------------------------------------------------------- /** * Gets the DecimalStyle to be used during formatting.
< prev index next >