< prev index next >

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

Print this page
rev 47480 : [mq]: 8176841

@@ -95,10 +95,11 @@
 import java.util.HashSet;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import sun.util.locale.provider.TimeZoneNameUtility;
 
 /**
  * Formatter for printing and parsing date-time objects.
  * <p>
  * This class provides the main application entry point for printing and parsing

@@ -127,11 +128,16 @@
  * 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.
+ * 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 overriden. If both "ca" and "rg" are
+ * specified, the chronology from "ca" extension supersedes the implicit one
+ * from "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.

@@ -546,12 +552,17 @@
      * <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
+     * 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 overriden. If both "ca" and "rg" are
+     * specified, the chronology from "ca" extension supersedes the implicit one
+     * from "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

@@ -570,11 +581,16 @@
      * <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
+     * 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 overriden. If both "ca" and "rg" are
+     * specified, the chronology from "ca" extension supersedes the implicit one
+     * from "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

@@ -1439,22 +1455,37 @@
 
     /**
      * 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.
+     * 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 overriden. If both "ca" and "rg" are
+     * specified, the chronology from "ca" extension supersedes the implicit one
+     * from "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;
         }
-        return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone);
+
+        // 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.
< prev index next >