src/share/classes/sun/util/calendar/CalendarSystem.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 







  28 import java.util.TimeZone;
  29 import java.util.concurrent.ConcurrentHashMap;
  30 import java.util.concurrent.ConcurrentMap;
  31 
  32 /**
  33  * <code>CalendarSystem</code> is an abstract class that defines the
  34  * programming interface to deal with calendar date and time.
  35  *
  36  * <p><code>CalendarSystem</code> instances are singletons. For
  37  * example, there exists only one Gregorian calendar instance in the
  38  * Java runtime environment. A singleton instance can be obtained
  39  * calling one of the static factory methods.
  40  *
  41  * <h4>CalendarDate</h4>
  42  *
  43  * <p>For the methods in a <code>CalendarSystem</code> that manipulate
  44  * a <code>CalendarDate</code>, <code>CalendarDate</code>s that have
  45  * been created by the <code>CalendarSystem</code> must be
  46  * specified. Otherwise, the methods throw an exception. This is
  47  * because, for example, a Chinese calendar date can't be understood


 153         }
 154 
 155         if (className.endsWith("LocalGregorianCalendar")) {
 156             // Create the specific kind of local Gregorian calendar system
 157             cal = LocalGregorianCalendar.getLocalGregorianCalendar(calendarName);
 158         } else {
 159             try {
 160                 Class<?> cl = Class.forName(className);
 161                 cal = (CalendarSystem) cl.newInstance();
 162             } catch (Exception e) {
 163                 throw new InternalError(e);
 164             }
 165         }
 166         if (cal == null) {
 167             return null;
 168         }
 169         CalendarSystem cs =  calendars.putIfAbsent(calendarName, cal);
 170         return (cs == null) ? cal : cs;
 171     }
 172 






































 173     //////////////////////////////// Calendar API //////////////////////////////////
 174 
 175     /**
 176      * Returns the name of this calendar system.
 177      */
 178     public abstract String getName();
 179 
 180     public abstract CalendarDate getCalendarDate();
 181 
 182     /**
 183      * Calculates calendar fields from the specified number of
 184      * milliseconds since the Epoch, January 1, 1970 00:00:00 UTC
 185      * (Gregorian). This method doesn't check overflow or underflow
 186      * when adjusting the millisecond value (representing UTC) with
 187      * the time zone offsets (i.e., the GMT offset and amount of
 188      * daylight saving).
 189      *
 190      * @param millis the offset value in milliseconds from January 1,
 191      * 1970 00:00:00 UTC (Gregorian).
 192      * @return a <code>CalendarDate</code> instance that contains the


   1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 
  28 import java.io.File;
  29 import java.io.FileInputStream;
  30 import java.io.IOException;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedActionException;
  33 import java.security.PrivilegedExceptionAction;
  34 import java.util.Properties;
  35 import java.util.TimeZone;
  36 import java.util.concurrent.ConcurrentHashMap;
  37 import java.util.concurrent.ConcurrentMap;
  38 
  39 /**
  40  * <code>CalendarSystem</code> is an abstract class that defines the
  41  * programming interface to deal with calendar date and time.
  42  *
  43  * <p><code>CalendarSystem</code> instances are singletons. For
  44  * example, there exists only one Gregorian calendar instance in the
  45  * Java runtime environment. A singleton instance can be obtained
  46  * calling one of the static factory methods.
  47  *
  48  * <h4>CalendarDate</h4>
  49  *
  50  * <p>For the methods in a <code>CalendarSystem</code> that manipulate
  51  * a <code>CalendarDate</code>, <code>CalendarDate</code>s that have
  52  * been created by the <code>CalendarSystem</code> must be
  53  * specified. Otherwise, the methods throw an exception. This is
  54  * because, for example, a Chinese calendar date can't be understood


 160         }
 161 
 162         if (className.endsWith("LocalGregorianCalendar")) {
 163             // Create the specific kind of local Gregorian calendar system
 164             cal = LocalGregorianCalendar.getLocalGregorianCalendar(calendarName);
 165         } else {
 166             try {
 167                 Class<?> cl = Class.forName(className);
 168                 cal = (CalendarSystem) cl.newInstance();
 169             } catch (Exception e) {
 170                 throw new InternalError(e);
 171             }
 172         }
 173         if (cal == null) {
 174             return null;
 175         }
 176         CalendarSystem cs =  calendars.putIfAbsent(calendarName, cal);
 177         return (cs == null) ? cal : cs;
 178     }
 179 
 180     /**
 181      * Returns a {@link Properties} loaded from lib/calendars.properties.
 182      *
 183      * @return a {@link Properties} loaded from lib/calendars.properties
 184      * @throws IOException if an error occurred when reading from the input stream
 185      * @throws IllegalArgumentException if the input stream contains any malformed
 186      *                                  Unicode escape sequences
 187      */
 188     public static Properties getCalendarProperties() throws IOException {
 189         Properties calendarProps = null;
 190         try {
 191             String homeDir = AccessController.doPrivileged(
 192                 new sun.security.action.GetPropertyAction("java.home"));
 193             final String fname = homeDir + File.separator + "lib" + File.separator
 194                                  + "calendars.properties";
 195             calendarProps = AccessController.doPrivileged(new PrivilegedExceptionAction<Properties>() {
 196                 @Override
 197                 public Properties run() throws IOException {
 198                     Properties props = new Properties();
 199                     try (FileInputStream fis = new FileInputStream(fname)) {
 200                         props.load(fis);
 201                     }
 202                     return props;
 203                 }
 204             });
 205         } catch (PrivilegedActionException e) {
 206             Throwable cause = e.getCause();
 207             if (cause instanceof IOException) {
 208                 throw (IOException) cause;
 209             } else if (cause instanceof IllegalArgumentException) {
 210                 throw (IllegalArgumentException) cause;
 211             }
 212             // Should not happen
 213             throw new InternalError(cause);
 214         }
 215         return calendarProps;
 216     }
 217 
 218     //////////////////////////////// Calendar API //////////////////////////////////
 219 
 220     /**
 221      * Returns the name of this calendar system.
 222      */
 223     public abstract String getName();
 224 
 225     public abstract CalendarDate getCalendarDate();
 226 
 227     /**
 228      * Calculates calendar fields from the specified number of
 229      * milliseconds since the Epoch, January 1, 1970 00:00:00 UTC
 230      * (Gregorian). This method doesn't check overflow or underflow
 231      * when adjusting the millisecond value (representing UTC) with
 232      * the time zone offsets (i.e., the GMT offset and amount of
 233      * daylight saving).
 234      *
 235      * @param millis the offset value in milliseconds from January 1,
 236      * 1970 00:00:00 UTC (Gregorian).
 237      * @return a <code>CalendarDate</code> instance that contains the