40
41 import java.io.IOException;
42 import java.io.ObjectInputStream;
43 import java.io.ObjectOutputStream;
44 import java.io.OptionalDataException;
45 import java.io.Serializable;
46 import java.security.AccessControlContext;
47 import java.security.AccessController;
48 import java.security.PermissionCollection;
49 import java.security.PrivilegedActionException;
50 import java.security.PrivilegedExceptionAction;
51 import java.security.ProtectionDomain;
52 import java.text.DateFormat;
53 import java.text.DateFormatSymbols;
54 import java.time.Instant;
55 import java.util.concurrent.ConcurrentHashMap;
56 import java.util.concurrent.ConcurrentMap;
57 import sun.util.BuddhistCalendar;
58 import sun.util.calendar.ZoneInfo;
59 import sun.util.locale.provider.CalendarDataUtility;
60
61 /**
62 * The <code>Calendar</code> class is an abstract class that provides methods
63 * for converting between a specific instant in time and a set of {@link
64 * #fields calendar fields} such as <code>YEAR</code>, <code>MONTH</code>,
65 * <code>DAY_OF_MONTH</code>, <code>HOUR</code>, and so on, and for
66 * manipulating the calendar fields, such as getting the date of the next
67 * week. An instant in time can be represented by a millisecond value that is
68 * an offset from the <a name="Epoch"><em>Epoch</em></a>, January 1, 1970
69 * 00:00:00.000 GMT (Gregorian).
70 *
71 * <p>The class also provides additional fields and methods for
72 * implementing a concrete calendar system outside the package. Those
73 * fields and methods are defined as <code>protected</code>.
74 *
75 * <p>
76 * Like other locale-sensitive classes, <code>Calendar</code> provides a
77 * class method, <code>getInstance</code>, for getting a generally useful
78 * object of this type. <code>Calendar</code>'s <code>getInstance</code> method
79 * returns a <code>Calendar</code> object whose
1591 protected Calendar(TimeZone zone, Locale aLocale)
1592 {
1593 fields = new int[FIELD_COUNT];
1594 isSet = new boolean[FIELD_COUNT];
1595 stamp = new int[FIELD_COUNT];
1596
1597 this.zone = zone;
1598 setWeekCountData(aLocale);
1599 }
1600
1601 /**
1602 * Gets a calendar using the default time zone and locale. The
1603 * <code>Calendar</code> returned is based on the current time
1604 * in the default time zone with the default
1605 * {@link Locale.Category#FORMAT FORMAT} locale.
1606 *
1607 * @return a Calendar.
1608 */
1609 public static Calendar getInstance()
1610 {
1611 Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
1612 cal.sharedZone = true;
1613 return cal;
1614 }
1615
1616 /**
1617 * Gets a calendar using the specified time zone and default locale.
1618 * The <code>Calendar</code> returned is based on the current time
1619 * in the given time zone with the default
1620 * {@link Locale.Category#FORMAT FORMAT} locale.
1621 *
1622 * @param zone the time zone to use
1623 * @return a Calendar.
1624 */
1625 public static Calendar getInstance(TimeZone zone)
1626 {
1627 return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));
1628 }
1629
1630 /**
1631 * Gets a calendar using the default time zone and specified locale.
1632 * The <code>Calendar</code> returned is based on the current time
1633 * in the default time zone with the given locale.
1634 *
1635 * @param aLocale the locale for the week data
1636 * @return a Calendar.
1637 */
1638 public static Calendar getInstance(Locale aLocale)
1639 {
1640 Calendar cal = createCalendar(TimeZone.getDefaultRef(), aLocale);
1641 cal.sharedZone = true;
1642 return cal;
1643 }
1644
1645 /**
1646 * Gets a calendar with the specified time zone and locale.
1647 * The <code>Calendar</code> returned is based on the current time
1648 * in the given time zone with the given locale.
1649 *
1650 * @param zone the time zone to use
1651 * @param aLocale the locale for the week data
1652 * @return a Calendar.
1653 */
1654 public static Calendar getInstance(TimeZone zone,
1655 Locale aLocale)
1656 {
1657 return createCalendar(zone, aLocale);
1658 }
1659
1660 private static Calendar createCalendar(TimeZone zone,
1661 Locale aLocale)
1662 {
1663 Calendar cal = null;
1664
1665 if (aLocale.hasExtensions()) {
1666 String caltype = aLocale.getUnicodeLocaleType("ca");
1667 if (caltype != null) {
1668 switch (caltype) {
1669 case "buddhist":
1670 cal = new BuddhistCalendar(zone, aLocale);
1671 break;
1672 case "japanese":
1673 cal = new JapaneseImperialCalendar(zone, aLocale);
1674 break;
1675 case "gregory":
1676 cal = new GregorianCalendar(zone, aLocale);
1677 break;
1678 }
1679 }
1680 }
1681 if (cal == null) {
1682 // If no known calendar type is explicitly specified,
|
40
41 import java.io.IOException;
42 import java.io.ObjectInputStream;
43 import java.io.ObjectOutputStream;
44 import java.io.OptionalDataException;
45 import java.io.Serializable;
46 import java.security.AccessControlContext;
47 import java.security.AccessController;
48 import java.security.PermissionCollection;
49 import java.security.PrivilegedActionException;
50 import java.security.PrivilegedExceptionAction;
51 import java.security.ProtectionDomain;
52 import java.text.DateFormat;
53 import java.text.DateFormatSymbols;
54 import java.time.Instant;
55 import java.util.concurrent.ConcurrentHashMap;
56 import java.util.concurrent.ConcurrentMap;
57 import sun.util.BuddhistCalendar;
58 import sun.util.calendar.ZoneInfo;
59 import sun.util.locale.provider.CalendarDataUtility;
60 import sun.util.locale.provider.LocaleProviderAdapter;
61 import sun.util.spi.CalendarProvider;
62
63 /**
64 * The <code>Calendar</code> class is an abstract class that provides methods
65 * for converting between a specific instant in time and a set of {@link
66 * #fields calendar fields} such as <code>YEAR</code>, <code>MONTH</code>,
67 * <code>DAY_OF_MONTH</code>, <code>HOUR</code>, and so on, and for
68 * manipulating the calendar fields, such as getting the date of the next
69 * week. An instant in time can be represented by a millisecond value that is
70 * an offset from the <a name="Epoch"><em>Epoch</em></a>, January 1, 1970
71 * 00:00:00.000 GMT (Gregorian).
72 *
73 * <p>The class also provides additional fields and methods for
74 * implementing a concrete calendar system outside the package. Those
75 * fields and methods are defined as <code>protected</code>.
76 *
77 * <p>
78 * Like other locale-sensitive classes, <code>Calendar</code> provides a
79 * class method, <code>getInstance</code>, for getting a generally useful
80 * object of this type. <code>Calendar</code>'s <code>getInstance</code> method
81 * returns a <code>Calendar</code> object whose
1593 protected Calendar(TimeZone zone, Locale aLocale)
1594 {
1595 fields = new int[FIELD_COUNT];
1596 isSet = new boolean[FIELD_COUNT];
1597 stamp = new int[FIELD_COUNT];
1598
1599 this.zone = zone;
1600 setWeekCountData(aLocale);
1601 }
1602
1603 /**
1604 * Gets a calendar using the default time zone and locale. The
1605 * <code>Calendar</code> returned is based on the current time
1606 * in the default time zone with the default
1607 * {@link Locale.Category#FORMAT FORMAT} locale.
1608 *
1609 * @return a Calendar.
1610 */
1611 public static Calendar getInstance()
1612 {
1613 return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
1614 }
1615
1616 /**
1617 * Gets a calendar using the specified time zone and default locale.
1618 * The <code>Calendar</code> returned is based on the current time
1619 * in the given time zone with the default
1620 * {@link Locale.Category#FORMAT FORMAT} locale.
1621 *
1622 * @param zone the time zone to use
1623 * @return a Calendar.
1624 */
1625 public static Calendar getInstance(TimeZone zone)
1626 {
1627 return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));
1628 }
1629
1630 /**
1631 * Gets a calendar using the default time zone and specified locale.
1632 * The <code>Calendar</code> returned is based on the current time
1633 * in the default time zone with the given locale.
1634 *
1635 * @param aLocale the locale for the week data
1636 * @return a Calendar.
1637 */
1638 public static Calendar getInstance(Locale aLocale)
1639 {
1640 return createCalendar(TimeZone.getDefault(), aLocale);
1641 }
1642
1643 /**
1644 * Gets a calendar with the specified time zone and locale.
1645 * The <code>Calendar</code> returned is based on the current time
1646 * in the given time zone with the given locale.
1647 *
1648 * @param zone the time zone to use
1649 * @param aLocale the locale for the week data
1650 * @return a Calendar.
1651 */
1652 public static Calendar getInstance(TimeZone zone,
1653 Locale aLocale)
1654 {
1655 return createCalendar(zone, aLocale);
1656 }
1657
1658 private static Calendar createCalendar(TimeZone zone,
1659 Locale aLocale)
1660 {
1661 CalendarProvider provider =
1662 LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
1663 .getCalendarProvider();
1664 if (provider != null) {
1665 try {
1666 return provider.getInstance(zone, aLocale);
1667 } catch (IllegalArgumentException iae) {
1668 // fall back to the default instantiation
1669 }
1670 }
1671
1672 Calendar cal = null;
1673
1674 if (aLocale.hasExtensions()) {
1675 String caltype = aLocale.getUnicodeLocaleType("ca");
1676 if (caltype != null) {
1677 switch (caltype) {
1678 case "buddhist":
1679 cal = new BuddhistCalendar(zone, aLocale);
1680 break;
1681 case "japanese":
1682 cal = new JapaneseImperialCalendar(zone, aLocale);
1683 break;
1684 case "gregory":
1685 cal = new GregorianCalendar(zone, aLocale);
1686 break;
1687 }
1688 }
1689 }
1690 if (cal == null) {
1691 // If no known calendar type is explicitly specified,
|