jdk/src/share/classes/java/util/Calendar.java
changeset 14765 0987999ed367
parent 14502 d63fed06fed4
child 14928 afdc82e5403a
equal deleted inserted replaced
14764:6dd2370b4cd3 14765:0987999ed367
    51 import java.security.ProtectionDomain;
    51 import java.security.ProtectionDomain;
    52 import java.text.DateFormat;
    52 import java.text.DateFormat;
    53 import java.text.DateFormatSymbols;
    53 import java.text.DateFormatSymbols;
    54 import java.util.concurrent.ConcurrentHashMap;
    54 import java.util.concurrent.ConcurrentHashMap;
    55 import java.util.concurrent.ConcurrentMap;
    55 import java.util.concurrent.ConcurrentMap;
    56 import java.util.spi.CalendarDataProvider;
       
    57 import sun.util.BuddhistCalendar;
    56 import sun.util.BuddhistCalendar;
    58 import sun.util.locale.provider.LocaleProviderAdapter;
       
    59 import sun.util.calendar.ZoneInfo;
    57 import sun.util.calendar.ZoneInfo;
    60 import sun.util.locale.provider.CalendarDataUtility;
    58 import sun.util.locale.provider.CalendarDataUtility;
    61 
    59 
    62 /**
    60 /**
    63  * The <code>Calendar</code> class is an abstract class that provides methods
    61  * The <code>Calendar</code> class is an abstract class that provides methods
   740      * @see #LONG_STANDALONE
   738      * @see #LONG_STANDALONE
   741      * @see #SHORT
   739      * @see #SHORT
   742      * @since 1.6
   740      * @since 1.6
   743      */
   741      */
   744     public static final int LONG = 2;
   742     public static final int LONG = 2;
       
   743 
       
   744     /**
       
   745      * A style specifier for {@link #getDisplayName(int, int, Locale)
       
   746      * getDisplayName} and {@link #getDisplayNames(int, int, Locale)
       
   747      * getDisplayNames} indicating a narrow name used for format. Narrow names
       
   748      * are typically single character strings, such as "M" for Monday.
       
   749      *
       
   750      * @see #NARROW_STANDALONE
       
   751      * @see #SHORT_FORMAT
       
   752      * @see #LONG_FOTMAT
       
   753      * @since 1.8
       
   754      */
       
   755     public static final int NARROW_FORMAT = 4;
       
   756 
       
   757     /**
       
   758      * A style specifier for {@link #getDisplayName(int, int, Locale)
       
   759      * getDisplayName} and {@link #getDisplayNames(int, int, Locale)
       
   760      * getDisplayNames} indicating a narrow name independently. Narrow names
       
   761      * are typically single character strings, such as "M" for Monday.
       
   762      *
       
   763      * @see #NARROW_FORMAT
       
   764      * @see #SHORT_STANDALONE
       
   765      * @see #LONG_STANDALONE
       
   766      * @since 1.8
       
   767      */
       
   768     public static final int NARROW_STANDALONE = NARROW_FORMAT | STANDALONE_MASK;
   745 
   769 
   746     /**
   770     /**
   747      * A style specifier for {@link #getDisplayName(int, int, Locale)
   771      * A style specifier for {@link #getDisplayName(int, int, Locale)
   748      * getDisplayName} and {@link #getDisplayNames(int, int, Locale)
   772      * getDisplayName} and {@link #getDisplayNames(int, int, Locale)
   749      * getDisplayNames} indicating a short name used for format.
   773      * getDisplayNames} indicating a short name used for format.
  1470      *        the calendar field for which the string representation
  1494      *        the calendar field for which the string representation
  1471      *        is returned
  1495      *        is returned
  1472      * @param style
  1496      * @param style
  1473      *        the style applied to the string representation; one of {@link
  1497      *        the style applied to the string representation; one of {@link
  1474      *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
  1498      *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
  1475      *        {@link #LONG_FORMAT} ({@link #LONG}) or {@link #LONG_STANDALONE}.
  1499      *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
       
  1500      *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}.
  1476      * @param locale
  1501      * @param locale
  1477      *        the locale for the string representation
  1502      *        the locale for the string representation
  1478      *        (any calendar types specified by {@code locale} are ignored)
  1503      *        (any calendar types specified by {@code locale} are ignored)
  1479      * @return the string representation of the given
  1504      * @return the string representation of the given
  1480      *        <code>field</code> in the given <code>style</code>, or
  1505      *        {@code field} in the given {@code style}, or
  1481      *        <code>null</code> if no string representation is
  1506      *        {@code null} if no string representation is
  1482      *        applicable.
  1507      *        applicable.
  1483      * @exception IllegalArgumentException
  1508      * @exception IllegalArgumentException
  1484      *        if <code>field</code> or <code>style</code> is invalid,
  1509      *        if {@code field} or {@code style} is invalid,
  1485      *        or if this <code>Calendar</code> is non-lenient and any
  1510      *        or if this {@code Calendar} is non-lenient and any
  1486      *        of the calendar fields have invalid values
  1511      *        of the calendar fields have invalid values
  1487      * @exception NullPointerException
  1512      * @exception NullPointerException
  1488      *        if <code>locale</code> is null
  1513      *        if {@code locale} is null
  1489      * @since 1.6
  1514      * @since 1.6
  1490      */
  1515      */
  1491     public String getDisplayName(int field, int style, Locale locale) {
  1516     public String getDisplayName(int field, int style, Locale locale) {
  1492         if (!checkDisplayNameParams(field, style, SHORT, LONG, locale,
  1517         if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
  1493                             ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
  1518                             ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
  1494             return null;
  1519             return null;
  1495         }
  1520         }
  1496 
  1521 
  1497         // the standalone styles are supported only through CalendarDataProviders.
  1522         // the standalone and narrow styles are supported only through CalendarDataProviders.
  1498         if (isStandaloneStyle(style)) {
  1523         if (isStandaloneStyle(style) || isNarrowStyle(style)) {
  1499             return CalendarDataUtility.retrieveFieldValueName(getCalendarType(),
  1524             return CalendarDataUtility.retrieveFieldValueName(getCalendarType(),
  1500                                                               field, get(field),
  1525                                                               field, get(field),
  1501                                                               style, locale);
  1526                                                               style, locale);
  1502         }
  1527         }
  1503 
  1528 
  1511         }
  1536         }
  1512         return null;
  1537         return null;
  1513     }
  1538     }
  1514 
  1539 
  1515     /**
  1540     /**
  1516      * Returns a <code>Map</code> containing all names of the calendar
  1541      * Returns a {@code Map} containing all names of the calendar
  1517      * <code>field</code> in the given <code>style</code> and
  1542      * {@code field} in the given {@code style} and
  1518      * <code>locale</code> and their corresponding field values. For
  1543      * {@code locale} and their corresponding field values. For
  1519      * example, if this <code>Calendar</code> is a {@link
  1544      * example, if this {@code Calendar} is a {@link
  1520      * GregorianCalendar}, the returned map would contain "Jan" to
  1545      * GregorianCalendar}, the returned map would contain "Jan" to
  1521      * {@link #JANUARY}, "Feb" to {@link #FEBRUARY}, and so on, in the
  1546      * {@link #JANUARY}, "Feb" to {@link #FEBRUARY}, and so on, in the
  1522      * {@linkplain #SHORT short} style in an English locale.
  1547      * {@linkplain #SHORT short} style in an English locale.
  1523      *
  1548      *
       
  1549      * <p>Narrow names may not be unique due to use of single characters,
       
  1550      * such as "S" for Sunday and Saturday. In that case narrow names are not
       
  1551      * included in the returned {@code Map}.
       
  1552      *
  1524      * <p>The values of other calendar fields may be taken into
  1553      * <p>The values of other calendar fields may be taken into
  1525      * account to determine a set of display names. For example, if
  1554      * account to determine a set of display names. For example, if
  1526      * this <code>Calendar</code> is a lunisolar calendar system and
  1555      * this {@code Calendar} is a lunisolar calendar system and
  1527      * the year value given by the {@link #YEAR} field has a leap
  1556      * the year value given by the {@link #YEAR} field has a leap
  1528      * month, this method would return month names containing the leap
  1557      * month, this method would return month names containing the leap
  1529      * month name, and month names are mapped to their values specific
  1558      * month name, and month names are mapped to their values specific
  1530      * for the year.
  1559      * for the year.
  1531      *
  1560      *
  1532      * <p>The default implementation supports display names contained in
  1561      * <p>The default implementation supports display names contained in
  1533      * a {@link DateFormatSymbols}. For example, if <code>field</code>
  1562      * a {@link DateFormatSymbols}. For example, if {@code field}
  1534      * is {@link #MONTH} and <code>style</code> is {@link
  1563      * is {@link #MONTH} and {@code style} is {@link
  1535      * #ALL_STYLES}, this method returns a <code>Map</code> containing
  1564      * #ALL_STYLES}, this method returns a {@code Map} containing
  1536      * all strings returned by {@link DateFormatSymbols#getShortMonths()}
  1565      * all strings returned by {@link DateFormatSymbols#getShortMonths()}
  1537      * and {@link DateFormatSymbols#getMonths()}.
  1566      * and {@link DateFormatSymbols#getMonths()}.
  1538      *
  1567      *
  1539      * @param field
  1568      * @param field
  1540      *        the calendar field for which the display names are returned
  1569      *        the calendar field for which the display names are returned
  1541      * @param style
  1570      * @param style
  1542      *        the style applied to the string representation; one of {@link
  1571      *        the style applied to the string representation; one of {@link
  1543      *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
  1572      *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
  1544      *        {@link #LONG_FORMAT} ({@link #LONG}) or {@link #LONG_STANDALONE}.
  1573      *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
       
  1574      *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}
  1545      * @param locale
  1575      * @param locale
  1546      *        the locale for the display names
  1576      *        the locale for the display names
  1547      * @return a <code>Map</code> containing all display names in
  1577      * @return a {@code Map} containing all display names in
  1548      *        <code>style</code> and <code>locale</code> and their
  1578      *        {@code style} and {@code locale} and their
  1549      *        field values, or <code>null</code> if no display names
  1579      *        field values, or {@code null} if no display names
  1550      *        are defined for <code>field</code>
  1580      *        are defined for {@code field}
  1551      * @exception IllegalArgumentException
  1581      * @exception IllegalArgumentException
  1552      *        if <code>field</code> or <code>style</code> is invalid,
  1582      *        if {@code field} or {@code style} is invalid,
  1553      *        or if this <code>Calendar</code> is non-lenient and any
  1583      *        or if this {@code Calendar} is non-lenient and any
  1554      *        of the calendar fields have invalid values
  1584      *        of the calendar fields have invalid values
  1555      * @exception NullPointerException
  1585      * @exception NullPointerException
  1556      *        if <code>locale</code> is null
  1586      *        if {@code locale} is null
  1557      * @since 1.6
  1587      * @since 1.6
  1558      */
  1588      */
  1559     public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) {
  1589     public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) {
  1560         if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale,
  1590         if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale,
  1561                                     ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
  1591                                     ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
  1562             return null;
  1592             return null;
  1563         }
  1593         }
  1564         if (style == ALL_STYLES || isStandaloneStyle(style)) {
  1594         if (style == ALL_STYLES || isStandaloneStyle(style)) {
  1565             return CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale);
  1595             return CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale);
  1566         }
  1596         }
  1567         // SHORT or LONG
  1597         // SHORT, LONG, or NARROW
  1568         return getDisplayNamesImpl(field, style, locale);
  1598         return getDisplayNamesImpl(field, style, locale);
  1569     }
  1599     }
  1570 
  1600 
  1571     private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
  1601     private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
  1572         DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
  1602         DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
  1597         return isFieldSet(fieldMask, field);
  1627         return isFieldSet(fieldMask, field);
  1598     }
  1628     }
  1599 
  1629 
  1600     private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
  1630     private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
  1601         int baseStyle = getBaseStyle(style); // ignore the standalone mask
  1631         int baseStyle = getBaseStyle(style); // ignore the standalone mask
       
  1632 
       
  1633         // DateFormatSymbols doesn't support any narrow names.
       
  1634         if (baseStyle == NARROW_FORMAT) {
       
  1635             return null;
       
  1636         }
       
  1637 
  1602         String[] strings = null;
  1638         String[] strings = null;
  1603         switch (field) {
  1639         switch (field) {
  1604         case ERA:
  1640         case ERA:
  1605             strings = symbols.getEras();
  1641             strings = symbols.getEras();
  1606             break;
  1642             break;
  1946 
  1982 
  1947     boolean isStandaloneStyle(int style) {
  1983     boolean isStandaloneStyle(int style) {
  1948         return (style & STANDALONE_MASK) != 0;
  1984         return (style & STANDALONE_MASK) != 0;
  1949     }
  1985     }
  1950 
  1986 
       
  1987     boolean isNarrowStyle(int style) {
       
  1988         return style == NARROW_FORMAT || style == NARROW_STANDALONE;
       
  1989     }
       
  1990 
  1951     /**
  1991     /**
  1952      * Returns the pseudo-time-stamp for two fields, given their
  1992      * Returns the pseudo-time-stamp for two fields, given their
  1953      * individual pseudo-time-stamps.  If either of the fields
  1993      * individual pseudo-time-stamps.  If either of the fields
  1954      * is unset, then the aggregate is unset.  Otherwise, the
  1994      * is unset, then the aggregate is unset.  Otherwise, the
  1955      * aggregate is the later of the two stamps.
  1995      * aggregate is the later of the two stamps.