0N/A/*
2362N/A * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.util.calendar;
0N/A
0N/Aimport java.lang.reflect.Field;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Map;
0N/Aimport java.util.MissingResourceException;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.Set;
0N/Aimport java.util.TimeZone;
0N/Aimport java.util.concurrent.ConcurrentHashMap;
0N/Aimport java.util.concurrent.ConcurrentMap;
0N/A
0N/A/**
0N/A * <code>CalendarSystem</code> is an abstract class that defines the
0N/A * programming interface to deal with calendar date and time.
0N/A *
0N/A * <p><code>CalendarSystem</code> instances are singletons. For
0N/A * example, there exists only one Gregorian calendar instance in the
0N/A * Java runtime environment. A singleton instance can be obtained
0N/A * calling one of the static factory methods.
0N/A *
0N/A * <h4>CalendarDate</h4>
0N/A *
0N/A * <p>For the methods in a <code>CalendarSystem</code> that manipulate
0N/A * a <code>CalendarDate</code>, <code>CalendarDate</code>s that have
0N/A * been created by the <code>CalendarSystem</code> must be
0N/A * specified. Otherwise, the methods throw an exception. This is
0N/A * because, for example, a Chinese calendar date can't be understood
0N/A * by the Hebrew calendar system.
0N/A *
0N/A * <h4>Calendar names</h4>
0N/A *
0N/A * Each calendar system has a unique name to be identified. The Java
0N/A * runtime in this release supports the following calendar systems.
0N/A *
0N/A * <pre>
0N/A * Name Calendar System
0N/A * ---------------------------------------
0N/A * gregorian Gregorian Calendar
0N/A * julian Julian Calendar
0N/A * japanese Japanese Imperial Calendar
0N/A * </pre>
0N/A *
0N/A * @see CalendarDate
0N/A * @author Masayoshi Okutsu
0N/A * @since 1.5
0N/A */
0N/A
0N/Apublic abstract class CalendarSystem {
0N/A
0N/A /////////////////////// Calendar Factory Methods /////////////////////////
0N/A
0N/A private volatile static boolean initialized = false;
0N/A
0N/A // Map of calendar names and calendar class names
0N/A private static ConcurrentMap<String, String> names;
0N/A
0N/A // Map of calendar names and CalendarSystem instances
0N/A private static ConcurrentMap<String,CalendarSystem> calendars;
0N/A
0N/A private static final String PACKAGE_NAME = "sun.util.calendar.";
0N/A
0N/A private static final String[] namePairs = {
0N/A "gregorian", "Gregorian",
0N/A "japanese", "LocalGregorianCalendar",
0N/A "julian", "JulianCalendar",
0N/A /*
0N/A "hebrew", "HebrewCalendar",
0N/A "iso8601", "ISOCalendar",
0N/A "taiwanese", "LocalGregorianCalendar",
0N/A "thaibuddhist", "LocalGregorianCalendar",
0N/A */
0N/A };
0N/A
0N/A private static void initNames() {
0N/A ConcurrentMap<String,String> nameMap = new ConcurrentHashMap<String,String>();
0N/A
0N/A // Associate a calendar name with its class name and the
0N/A // calendar class name with its date class name.
0N/A StringBuilder clName = new StringBuilder();
0N/A for (int i = 0; i < namePairs.length; i += 2) {
0N/A clName.setLength(0);
0N/A String cl = clName.append(PACKAGE_NAME).append(namePairs[i+1]).toString();
0N/A nameMap.put(namePairs[i], cl);
0N/A }
0N/A synchronized (CalendarSystem.class) {
0N/A if (!initialized) {
0N/A names = nameMap;
0N/A calendars = new ConcurrentHashMap<String,CalendarSystem>();
0N/A initialized = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private final static Gregorian GREGORIAN_INSTANCE = new Gregorian();
0N/A
0N/A /**
0N/A * Returns the singleton instance of the <code>Gregorian</code>
0N/A * calendar system.
0N/A *
0N/A * @return the <code>Gregorian</code> instance
0N/A */
0N/A public static Gregorian getGregorianCalendar() {
0N/A return GREGORIAN_INSTANCE;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>CalendarSystem</code> specified by the calendar
0N/A * name. The calendar name has to be one of the supported calendar
0N/A * names.
0N/A *
0N/A * @param calendarName the calendar name
0N/A * @return the <code>CalendarSystem</code> specified by
0N/A * <code>calendarName</code>, or null if there is no
0N/A * <code>CalendarSystem</code> associated with the given calendar name.
0N/A */
0N/A public static CalendarSystem forName(String calendarName) {
0N/A if ("gregorian".equals(calendarName)) {
0N/A return GREGORIAN_INSTANCE;
0N/A }
0N/A
0N/A if (!initialized) {
0N/A initNames();
0N/A }
0N/A
0N/A CalendarSystem cal = calendars.get(calendarName);
0N/A if (cal != null) {
0N/A return cal;
0N/A }
0N/A
0N/A String className = names.get(calendarName);
0N/A if (className == null) {
0N/A return null; // Unknown calendar name
0N/A }
0N/A
0N/A if (className.endsWith("LocalGregorianCalendar")) {
0N/A // Create the specific kind of local Gregorian calendar system
0N/A cal = LocalGregorianCalendar.getLocalGregorianCalendar(calendarName);
0N/A } else {
0N/A try {
0N/A Class cl = Class.forName(className);
0N/A cal = (CalendarSystem) cl.newInstance();
0N/A } catch (Exception e) {
0N/A throw new RuntimeException("internal error", e);
0N/A }
0N/A }
0N/A if (cal == null) {
0N/A return null;
0N/A }
0N/A CalendarSystem cs = calendars.putIfAbsent(calendarName, cal);
0N/A return (cs == null) ? cal : cs;
0N/A }
0N/A
0N/A //////////////////////////////// Calendar API //////////////////////////////////
0N/A
0N/A /**
0N/A * Returns the name of this calendar system.
0N/A */
0N/A public abstract String getName();
0N/A
0N/A public abstract CalendarDate getCalendarDate();
0N/A
0N/A /**
0N/A * Calculates calendar fields from the specified number of
0N/A * milliseconds since the Epoch, January 1, 1970 00:00:00 UTC
0N/A * (Gregorian). This method doesn't check overflow or underflow
0N/A * when adjusting the millisecond value (representing UTC) with
0N/A * the time zone offsets (i.e., the GMT offset and amount of
0N/A * daylight saving).
0N/A *
0N/A * @param millis the offset value in milliseconds from January 1,
0N/A * 1970 00:00:00 UTC (Gregorian).
0N/A * @return a <code>CalendarDate</code> instance that contains the
0N/A * calculated calendar field values.
0N/A */
0N/A public abstract CalendarDate getCalendarDate(long millis);
0N/A
0N/A public abstract CalendarDate getCalendarDate(long millis, CalendarDate date);
0N/A
0N/A public abstract CalendarDate getCalendarDate(long millis, TimeZone zone);
0N/A
0N/A /**
0N/A * Constructs a <code>CalendarDate</code> that is specific to this
0N/A * calendar system. All calendar fields have their initial
0N/A * values. The {@link TimeZone#getDefault() default time zone} is
0N/A * set to the instance.
0N/A *
0N/A * @return a <code>CalendarDate</code> instance that contains the initial
0N/A * calendar field values.
0N/A */
0N/A public abstract CalendarDate newCalendarDate();
0N/A
0N/A public abstract CalendarDate newCalendarDate(TimeZone zone);
0N/A
0N/A /**
0N/A * Returns the number of milliseconds since the Epoch, January 1,
0N/A * 1970 00:00:00 UTC (Gregorian), represented by the specified
0N/A * <code>CalendarDate</code>.
0N/A *
0N/A * @param date the <code>CalendarDate</code> from which the time
0N/A * value is calculated
0N/A * @return the number of milliseconds since the Epoch.
0N/A */
0N/A public abstract long getTime(CalendarDate date);
0N/A
0N/A /**
0N/A * Returns the length in days of the specified year by
0N/A * <code>date</code>. This method does not perform the
0N/A * normalization with the specified <code>CalendarDate</code>. The
0N/A * <code>CalendarDate</code> must be normalized to get a correct
0N/A * value.
0N/A */
0N/A public abstract int getYearLength(CalendarDate date);
0N/A
0N/A /**
0N/A * Returns the number of months of the specified year. This method
0N/A * does not perform the normalization with the specified
0N/A * <code>CalendarDate</code>. The <code>CalendarDate</code> must
0N/A * be normalized to get a correct value.
0N/A */
0N/A public abstract int getYearLengthInMonths(CalendarDate date);
0N/A
0N/A /**
0N/A * Returns the length in days of the month specified by the calendar
0N/A * date. This method does not perform the normalization with the
0N/A * specified calendar date. The <code>CalendarDate</code> must
0N/A * be normalized to get a correct value.
0N/A *
0N/A * @param date the date from which the month value is obtained
0N/A * @return the number of days in the month
0N/A * @exception IllegalArgumentException if the specified calendar date
0N/A * doesn't have a valid month value in this calendar system.
0N/A */
0N/A public abstract int getMonthLength(CalendarDate date); // no setter
0N/A
0N/A /**
0N/A * Returns the length in days of a week in this calendar
0N/A * system. If this calendar system has multiple radix weeks, this
0N/A * method returns only one of them.
0N/A */
0N/A public abstract int getWeekLength();
0N/A
0N/A /**
0N/A * Returns the <code>Era</code> designated by the era name that
0N/A * has to be known to this calendar system. If no Era is
0N/A * applicable to this calendar system, null is returned.
0N/A *
0N/A * @param eraName the name of the era
0N/A * @return the <code>Era</code> designated by
0N/A * <code>eraName</code>, or <code>null</code> if no Era is
0N/A * applicable to this calendar system or the specified era name is
0N/A * not known to this calendar system.
0N/A */
0N/A public abstract Era getEra(String eraName);
0N/A
0N/A /**
0N/A * Returns valid <code>Era</code>s of this calendar system. The
0N/A * return value is sorted in the descendant order. (i.e., the first
0N/A * element of the returned array is the oldest era.) If no era is
0N/A * applicable to this calendar system, <code>null</code> is returned.
0N/A *
0N/A * @return an array of valid <code>Era</code>s, or
0N/A * <code>null</code> if no era is applicable to this calendar
0N/A * system.
0N/A */
0N/A public abstract Era[] getEras();
0N/A
0N/A /**
0N/A * @throws IllegalArgumentException if the specified era name is
0N/A * unknown to this calendar system.
0N/A * @see Era
0N/A */
0N/A public abstract void setEra(CalendarDate date, String eraName);
0N/A
0N/A /**
0N/A * Returns a <code>CalendarDate</code> of the n-th day of week
0N/A * which is on, after or before the specified date. For example, the
0N/A * first Sunday in April 2002 (Gregorian) can be obtained as
0N/A * below:
0N/A *
0N/A * <pre><code>
0N/A * Gregorian cal = CalendarSystem.getGregorianCalendar();
0N/A * CalendarDate date = cal.newCalendarDate();
0N/A * date.setDate(2004, cal.APRIL, 1);
0N/A * CalendarDate firstSun = cal.getNthDayOfWeek(1, cal.SUNDAY, date);
0N/A * // firstSun represents April 4, 2004.
0N/A * </code></pre>
0N/A *
0N/A * This method returns a new <code>CalendarDate</code> instance
0N/A * and doesn't modify the original date.
0N/A *
0N/A * @param nth specifies the n-th one. A positive number specifies
0N/A * <em>on or after</em> the <code>date</code>. A non-positive number
0N/A * specifies <em>on or before</em> the <code>date</code>.
0N/A * @param dayOfWeek the day of week
0N/A * @param date the date
0N/A * @return the date of the nth <code>dayOfWeek</code> after
0N/A * or before the specified <code>CalendarDate</code>
0N/A */
0N/A public abstract CalendarDate getNthDayOfWeek(int nth, int dayOfWeek,
0N/A CalendarDate date);
0N/A
0N/A public abstract CalendarDate setTimeOfDay(CalendarDate date, int timeOfDay);
0N/A
0N/A /**
0N/A * Checks whether the calendar fields specified by <code>date</code>
0N/A * represents a valid date and time in this calendar system. If the
0N/A * given date is valid, <code>date</code> is marked as <em>normalized</em>.
0N/A *
0N/A * @param date the <code>CalendarDate</code> to be validated
0N/A * @return <code>true</code> if all the calendar fields are consistent,
0N/A * otherwise, <code>false</code> is returned.
0N/A * @exception NullPointerException if the specified
0N/A * <code>date</code> is <code>null</code>
0N/A */
0N/A public abstract boolean validate(CalendarDate date);
0N/A
0N/A /**
0N/A * Normalizes calendar fields in the specified
0N/A * <code>date</code>. Also all {@link CalendarDate#FIELD_UNDEFINED
0N/A * undefined} fields are set to correct values. The actual
0N/A * normalization process is calendar system dependent.
0N/A *
0N/A * @param date the calendar date to be validated
0N/A * @return <code>true</code> if all fields have been normalized;
0N/A * <code>false</code> otherwise.
0N/A * @exception NullPointerException if the specified
0N/A * <code>date</code> is <code>null</code>
0N/A */
0N/A public abstract boolean normalize(CalendarDate date);
0N/A}