src/share/classes/java/util/Calendar.java

Print this page
rev 6663 : imported patch 8008576


  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


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.JRELocaleProviderAdapter;
  61 import sun.util.locale.provider.LocaleProviderAdapter;
  62 import sun.util.spi.CalendarProvider;
  63 
  64 /**
  65  * The <code>Calendar</code> class is an abstract class that provides methods
  66  * for converting between a specific instant in time and a set of {@link
  67  * #fields calendar fields} such as <code>YEAR</code>, <code>MONTH</code>,
  68  * <code>DAY_OF_MONTH</code>, <code>HOUR</code>, and so on, and for
  69  * manipulating the calendar fields, such as getting the date of the next
  70  * week. An instant in time can be represented by a millisecond value that is
  71  * an offset from the <a name="Epoch"><em>Epoch</em></a>, January 1, 1970
  72  * 00:00:00.000 GMT (Gregorian).
  73  *
  74  * <p>The class also provides additional fields and methods for
  75  * implementing a concrete calendar system outside the package. Those
  76  * fields and methods are defined as <code>protected</code>.
  77  *
  78  * <p>
  79  * Like other locale-sensitive classes, <code>Calendar</code> provides a
  80  * class method, <code>getInstance</code>, for getting a generally useful
  81  * object of this type. <code>Calendar</code>'s <code>getInstance</code> method
  82  * returns a <code>Calendar</code> object whose


1646     }
1647 
1648     /**
1649      * Gets a calendar with the specified time zone and locale.
1650      * The <code>Calendar</code> returned is based on the current time
1651      * in the given time zone with the given locale.
1652      *
1653      * @param zone the time zone to use
1654      * @param aLocale the locale for the week data
1655      * @return a Calendar.
1656      */
1657     public static Calendar getInstance(TimeZone zone,
1658                                        Locale aLocale)
1659     {
1660         return createCalendar(zone, aLocale);
1661     }
1662 
1663     private static Calendar createCalendar(TimeZone zone,
1664                                            Locale aLocale)
1665     {
1666         LocaleProviderAdapter adapter =
1667                 LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale);
1668         // Kludge Alert: JRE's CalendarProvider should instantiate Calendar instances
1669         // in it, but it's not possible since JapaneseImperialCalendar is package
1670         // private.
1671         if (!(adapter instanceof JRELocaleProviderAdapter)) {
1672             CalendarProvider provider = adapter.getCalendarProvider();
1673             if (provider != null) {
1674                 return provider.getInstance(zone, aLocale);
1675             }
1676         }
1677 
1678         Calendar cal = null;
1679 
1680         if (aLocale.hasExtensions()) {
1681             String caltype = aLocale.getUnicodeLocaleType("ca");
1682             if (caltype != null) {
1683                 switch (caltype) {
1684                 case "buddhist":
1685                 cal = new BuddhistCalendar(zone, aLocale);
1686                     break;
1687                 case "japanese":
1688                     cal = new JapaneseImperialCalendar(zone, aLocale);
1689                     break;
1690                 case "gregory":
1691                     cal = new GregorianCalendar(zone, aLocale);
1692                     break;
1693                 }
1694             }
1695         }
1696         if (cal == null) {
1697             // If no known calendar type is explicitly specified,