Calendar.java
Upload User: njxy551199
Upload Date: 2020-09-09
Package Size: 1186k
Code Size: 19k
Category:

Jsp/Servlet

Development Platform:

Java

  1. /*
  2.  *   
  3.  *
  4.  * Portions Copyright  2000-2008 Sun Microsystems, Inc. All Rights
  5.  * Reserved.  Use is subject to license terms.
  6.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  7.  * 
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License version
  10.  * 2 only, as published by the Free Software Foundation.
  11.  * 
  12.  * This program is distributed in the hope that it will be useful, but
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  * General Public License version 2 for more details (a copy is
  16.  * included at /legal/license.txt).
  17.  * 
  18.  * You should have received a copy of the GNU General Public License
  19.  * version 2 along with this work; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21.  * 02110-1301 USA
  22.  * 
  23.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
  24.  * Clara, CA 95054 or visit www.sun.com if you need additional
  25.  * information or have any questions.
  26.  */
  27. /*
  28.  * (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved
  29.  * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
  30.  *
  31.  *   The original version of this source code and documentation is copyrighted
  32.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  33.  * materials are provided under terms of a License Agreement between Taligent
  34.  * and Sun. This technology is protected by multiple US and International
  35.  * patents. This notice and attribution to Taligent may not be removed.
  36.  *   Taligent is a registered trademark of Taligent, Inc.
  37.  *
  38.  */
  39. package java.util;
  40. /**
  41.  * <code>Calendar</code> is an abstract base class for converting between
  42.  * a <code>Date</code> object and a set of integer fields such as
  43.  * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</code>, <code>HOUR</code>,
  44.  * and so on. (A <code>Date</code> object represents a specific instant in
  45.  * time with millisecond precision. See
  46.  * {@link Date}
  47.  * for information about the <code>Date</code> class.)
  48.  *
  49.  * <p>
  50.  * Subclasses of <code>Calendar</code> interpret a <code>Date</code>
  51.  * according to the rules of a specific calendar system.
  52.  *
  53.  * <p>
  54.  * Like other locale-sensitive classes, <code>Calendar</code> provides a
  55.  * class method, <code>getInstance</code>, for getting a generally useful
  56.  * object of this type.
  57.  * <blockquote>
  58.  * <pre>
  59.  * Calendar rightNow = Calendar.getInstance();
  60.  * </pre>
  61.  * </blockquote>
  62.  *
  63.  * <p>
  64.  * A <code>Calendar</code> object can produce all the time field values
  65.  * needed to implement the date-time formatting for a particular language
  66.  * and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
  67.  *
  68.  * <p>
  69.  * When computing a <code>Date</code> from time fields,
  70.  * there may be insufficient information to compute the
  71.  * <code>Date</code> (such as only year and month but no day in the month).
  72.  *
  73.  * <p>
  74.  * <strong>Insufficient information.</strong> The calendar will use default
  75.  * information to specify the missing fields. This may vary by calendar; for
  76.  * the Gregorian calendar, the default for a field is the same as that of the
  77.  * start of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc.
  78.  *
  79.  * <strong>Note:</strong> The ambiguity in interpretation of what day midnight
  80.  * belongs to, is resolved as so: midnight "belongs" to the following day.<br>
  81.  * 23:59 on Dec 31, 1969 &lt; 00:00 on Jan 1, 1970.<br>
  82.  * 12:00 PM is midday, and 12:00 AM is midnight.<br>
  83.  * 11:59 PM on Jan 1 &lt; 12:00 AM on Jan 2 &lt; 12:01 AM on Jan 2.<br>
  84.  * 11:59 AM on Mar 10 &lt; 12:00 PM on Mar 10 &lt; 12:01 PM on Mar 10.<br>
  85.  * 24:00 or greater are invalid.
  86.  * Hours greater than 12 are invalid in AM/PM mode.
  87.  * Setting the time will never change the date.
  88.  * <p>
  89.  * If equivalent times are entered in AM/PM or 24 hour mode, equality will be
  90.  * determined by the actual time rather than the entered time.
  91.  * <p>
  92.  *
  93.  * This class has been subset for J2ME based on the JDK 1.3 Calendar class.
  94.  * Many methods and variables have been pruned, and other methods
  95.  * simplified, in an effort to reduce the size of this class.
  96.  *
  97.  * @see     java.util.Date
  98.  * @see     java.util.TimeZone
  99.  * @version CLDC 1.1 02/01/2002 (based on JDK 1.3)
  100.  */
  101. public abstract class Calendar {
  102.     /**
  103.      * Field number for <code>get</code> and <code>set</code> indicating the
  104.      * year. This is a calendar-specific value.
  105.      */
  106.     public final static int YEAR = 1;
  107.     /**
  108.      * Field number for <code>get</code> and <code>set</code> indicating the
  109.      * month. This is a calendar-specific value.
  110.      */
  111.     public final static int MONTH = 2;
  112.     /**
  113.      * Field number for <code>get</code> and <code>set</code> indicating the
  114.      * day of the month. This is a synonym for <code>DAY_OF_MONTH</code>.
  115.      * @see #DAY_OF_MONTH
  116.      */
  117.     public final static int DATE = 5;
  118.     /**
  119.      * Field number for <code>get</code> and <code>set</code> indicating the
  120.      * day of the month. This is a synonym for <code>DATE</code>.
  121.      * @see #DATE
  122.      */
  123.     public final static int DAY_OF_MONTH = 5;
  124.     /**
  125.      * Field number for <code>get</code> and <code>set</code> indicating the
  126.      * day of the week.
  127.      */
  128.     public final static int DAY_OF_WEEK = 7;
  129.     /**
  130.      * Field number for <code>get</code> and <code>set</code> indicating
  131.      * whether the <code>HOUR</code> is before or after noon.
  132.      * E.g., at 10:04:15.250 PM the <code>AM_PM</code> is <code>PM</code>.
  133.      * @see #AM
  134.      * @see #PM
  135.      * @see #HOUR
  136.      */
  137.     public final static int AM_PM = 9;
  138.     /**
  139.      * Field number for <code>get</code> and <code>set</code> indicating the
  140.      * hour of the morning or afternoon. <code>HOUR</code> is used for the
  141.      * 12-hour clock.
  142.      * E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10.
  143.      * @see #AM_PM
  144.      * @see #HOUR_OF_DAY
  145.      */
  146.     public final static int HOUR = 10;
  147.     /**
  148.      * Field number for <code>get</code> and <code>set</code> indicating the
  149.      * hour of the day. <code>HOUR_OF_DAY</code> is used for the 24-hour clock.
  150.      * E.g., at 10:04:15.250 PM the <code>HOUR_OF_DAY</code> is 22.
  151.      */
  152.     public final static int HOUR_OF_DAY = 11;
  153.     /**
  154.      * Field number for <code>get</code> and <code>set</code> indicating the
  155.      * minute within the hour.
  156.      * E.g., at 10:04:15.250 PM the <code>MINUTE</code> is 4.
  157.      */
  158.     public final static int MINUTE = 12;
  159.     /**
  160.      * Field number for <code>get</code> and <code>set</code> indicating the
  161.      * second within the minute.
  162.      * E.g., at 10:04:15.250 PM the <code>SECOND</code> is 15.
  163.      */
  164.     public final static int SECOND = 13;
  165.     /**
  166.      * Field number for <code>get</code> and <code>set</code> indicating the
  167.      * millisecond within the second.
  168.      * E.g., at 10:04:15.250 PM the <code>MILLISECOND</code> is 250.
  169.      */
  170.     public final static int MILLISECOND = 14;
  171.     /**
  172.      * Value of the <code>DAY_OF_WEEK</code> field indicating
  173.      * Sunday.
  174.      */
  175.     public final static int SUNDAY = 1;
  176.     /**
  177.      * Value of the <code>DAY_OF_WEEK</code> field indicating
  178.      * Monday.
  179.      */
  180.     public final static int MONDAY = 2;
  181.     /**
  182.      * Value of the <code>DAY_OF_WEEK</code> field indicating
  183.      * Tuesday.
  184.      */
  185.     public final static int TUESDAY = 3;
  186.     /**
  187.      * Value of the <code>DAY_OF_WEEK</code> field indicating
  188.      * Wednesday.
  189.      */
  190.     public final static int WEDNESDAY = 4;
  191.     /**
  192.      * Value of the <code>DAY_OF_WEEK</code> field indicating
  193.      * Thursday.
  194.      */
  195.     public final static int THURSDAY = 5;
  196.     /**
  197.      * Value of the <code>DAY_OF_WEEK</code> field indicating
  198.      * Friday.
  199.      */
  200.     public final static int FRIDAY = 6;
  201.     /**
  202.      * Value of the <code>DAY_OF_WEEK</code> field indicating
  203.      * Saturday.
  204.      */
  205.     public final static int SATURDAY = 7;
  206.     /**
  207.      * Value of the <code>MONTH</code> field indicating the
  208.      * first month of the year.
  209.      */
  210.     public final static int JANUARY = 0;
  211.     /**
  212.      * Value of the <code>MONTH</code> field indicating the
  213.      * second month of the year.
  214.      */
  215.     public final static int FEBRUARY = 1;
  216.     /**
  217.      * Value of the <code>MONTH</code> field indicating the
  218.      * third month of the year.
  219.      */
  220.     public final static int MARCH = 2;
  221.     /**
  222.      * Value of the <code>MONTH</code> field indicating the
  223.      * fourth month of the year.
  224.      */
  225.     public final static int APRIL = 3;
  226.     /**
  227.      * Value of the <code>MONTH</code> field indicating the
  228.      * fifth month of the year.
  229.      */
  230.     public final static int MAY = 4;
  231.     /**
  232.      * Value of the <code>MONTH</code> field indicating the
  233.      * sixth month of the year.
  234.      */
  235.     public final static int JUNE = 5;
  236.     /**
  237.      * Value of the <code>MONTH</code> field indicating the
  238.      * seventh month of the year.
  239.      */
  240.     public final static int JULY = 6;
  241.     /**
  242.      * Value of the <code>MONTH</code> field indicating the
  243.      * eighth month of the year.
  244.      */
  245.     public final static int AUGUST = 7;
  246.     /**
  247.      * Value of the <code>MONTH</code> field indicating the
  248.      * ninth month of the year.
  249.      */
  250.     public final static int SEPTEMBER = 8;
  251.     /**
  252.      * Value of the <code>MONTH</code> field indicating the
  253.      * tenth month of the year.
  254.      */
  255.     public final static int OCTOBER = 9;
  256.     /**
  257.      * Value of the <code>MONTH</code> field indicating the
  258.      * eleventh month of the year.
  259.      */
  260.     public final static int NOVEMBER = 10;
  261.     /**
  262.      * Value of the <code>MONTH</code> field indicating the
  263.      * twelfth month of the year.
  264.      */
  265.     public final static int DECEMBER = 11;
  266.     /**
  267.      * Value of the <code>AM_PM</code> field indicating the
  268.      * period of the day from midnight to just before noon.
  269.      */
  270.     public final static int AM = 0;
  271.     /**
  272.      * Value of the <code>AM_PM</code> field indicating the
  273.      * period of the day from noon to just before midnight.
  274.      */
  275.     public final static int PM = 1;
  276.     // Internal notes:
  277.     // Calendar contains two kinds of time representations: current "time" in
  278.     // milliseconds, and a set of time "fields" representing the current time.
  279.     // The two representations are usually in sync, but can get out of sync
  280.     // as follows.
  281.     // 1. Initially, no fields are set, and the time is invalid.
  282.     // 2. If the time is set, all fields are computed and in sync.
  283.     // 3. If a single field is set, the time is invalid.
  284.     // Recomputation of the time and fields happens when the object needs
  285.     // to return a result to the user, or use a result for a computation.
  286.     /*
  287.      * The number of fields for the array below.
  288.      */
  289.     private final static int FIELDS = 15;
  290.     /**
  291.      * The field values for the currently set time for this calendar.
  292.      */
  293.     protected int fields[];
  294.     /**
  295.      * The flags which tell if a specified time field for the calendar is set.
  296.      * This is an array of <code>FIELD_COUNT</code> booleans,
  297.      */
  298.     protected boolean isSet[];
  299.     /**
  300.      * The currently set time for this calendar, expressed in milliseconds after
  301.      * January 1, 1970, 0:00:00 GMT.
  302.      */
  303.     protected long time;
  304.     /**
  305.      * True if then the value of <code>time</code> is valid.
  306.      * The time is made invalid by a change to an item of <code>field[]</code>.
  307.      * @see #time
  308.      */
  309.     private boolean isTimeSet; // NOTE: Make transient when possible
  310.     /**
  311.      * The <code>TimeZone</code> used by this calendar. </code>Calendar</code>
  312.      * uses the time zone data to translate between the current/default
  313.      * system time and GMT time.
  314.      */
  315.     private TimeZone zone;
  316.     private Date dateObj = null;
  317.     /**
  318.      * Constructs a Calendar with the default time zone.
  319.      *
  320.      * @see     TimeZone#getDefault
  321.      */
  322.     protected Calendar() {
  323.         fields = new int[FIELDS];
  324.         isSet = new boolean[FIELDS];
  325.         zone = TimeZone.getDefault();
  326.         if (zone == null) {
  327.             throw new RuntimeException(
  328. /* #ifdef VERBOSE_EXCEPTIONS */
  329. /// skipped                       "Could not find default timezone"
  330. /* #endif */
  331.             );
  332.         }
  333.         setTimeInMillis(System.currentTimeMillis());
  334.     }
  335.     /**
  336.      * Gets this Calendar's current time.
  337.      *
  338.      * @return the current time.
  339.      *
  340.      * @see #setTime
  341.      */
  342.     public final Date getTime() {
  343.         if (dateObj == null) {
  344.             return dateObj = new Date( getTimeInMillis() );
  345.         } else {
  346.             synchronized (dateObj) {
  347.                 dateObj.setTime( getTimeInMillis() );
  348.                 return dateObj;
  349.             }
  350.         }
  351.     }
  352.     /**
  353.      * Sets this Calendar's current time with the given Date.
  354.      * <p>
  355.      * Note: Calling <code>setTime()</code> with
  356.      * <code>Date(Long.MAX_VALUE)</code> or <code>Date(Long.MIN_VALUE)</code>
  357.      * may yield incorrect field values from <code>get()</code>.
  358.      *
  359.      * @param date the given Date.
  360.      *
  361.      * @see #getTime
  362.      */
  363.     public final void setTime(Date date) {
  364.         setTimeInMillis( date.getTime() );
  365.     }
  366.     /**
  367.      * Gets a calendar using the default time zone.
  368.      *
  369.      * @return a Calendar.
  370.      */
  371.     /* <p>
  372.      * The following is information for implementers. Applications
  373.      * should not need to be aware of this or rely on it, because
  374.      * each implementation may do it differently:
  375.      * <p>
  376.      * The Calendar class will look up a calendar implementation
  377.      * class at runtime. The class name will take the form:
  378.      * <p>
  379.      * <code>{classRoot}.util.{platform}.CalendarImpl</code>
  380.      * <p>
  381.      * To simplify things, we use a hard-coded path name here.
  382.      * Actual location of the implementation class may vary
  383.      * from one implementation to another.
  384.      */
  385.     public static synchronized Calendar getInstance() {
  386.         try {
  387.             // Obtain the calendar implementation class
  388.             Class clazz = Class.forName("com.sun.cldc.util.j2me.CalendarImpl");
  389.             // Construct a new instance
  390.             return (Calendar)clazz.newInstance();
  391.         }
  392.         catch (Exception x) {}
  393.         return null;
  394.     }
  395.     /**
  396.      * Gets a calendar using the specified time zone.
  397.      * @param zone  the time zone to use
  398.      * @return a Calendar.
  399.      */
  400.     public static synchronized Calendar getInstance(TimeZone zone) {
  401.         Calendar cal = getInstance();
  402.         cal.setTimeZone(zone);
  403.         return cal;
  404.     }
  405.     /**
  406.      * Gets this Calendar's current time as a long expressed in milliseconds
  407.      * after January 1, 1970, 0:00:00 GMT (the epoch).
  408.      *
  409.      * @return the current time as UTC milliseconds from the epoch.
  410.      *
  411.      * @see #setTimeInMillis
  412.      */
  413.     protected long getTimeInMillis() {
  414.         if (!isTimeSet) {
  415.             computeTime();
  416.             isTimeSet = true;
  417.         }
  418.         return this.time;
  419.     }
  420.     /**
  421.      * Sets this Calendar's current time from the given long value.
  422.      * @param millis the new time in UTC milliseconds from the epoch.
  423.      *
  424.      * @see #getTimeInMillis
  425.      */
  426.     protected void setTimeInMillis( long millis ) {
  427.         isTimeSet = true;
  428.         this.fields[DAY_OF_WEEK] = 0;
  429.         this.time = millis;
  430.         computeFields();
  431.     }
  432.     /**
  433.      * Gets the value for a given time field.
  434.      * @param field the given time field (either YEAR, MONTH, DATE, DAY_OF_WEEK,
  435.      *                                    HOUR_OF_DAY, HOUR, AM_PM, MINUTE,
  436.      *                                    SECOND, or MILLISECOND
  437.      * @return the value for the given time field.
  438.      * @exception ArrayIndexOutOfBoundsException if the parameter is not
  439.      * one of the above.
  440.      */
  441.     public final int get(int field) {
  442.         if ( field == DAY_OF_WEEK ||
  443.              field == HOUR_OF_DAY ||
  444.              field == AM_PM ||
  445.              field == HOUR ) {
  446.             getTimeInMillis();
  447.             computeFields();
  448.         }
  449.         return this.fields[field];
  450.     }
  451.     /**
  452.      * Sets the time field with the given value. 
  453.      *
  454.      * @param field the given time field.
  455.      * @param value the value to be set for the given time field.
  456.      *
  457.      * @exception ArrayIndexOutOfBoundsException if an illegal field
  458.      * parameter is received.
  459.      */
  460.     public final void set(int field, int value) {
  461.         isTimeSet = false;
  462.         this.isSet[field] = true;
  463.         this.fields[field] = value;
  464.     }
  465.     /**
  466.      * Compares this calendar to the specified object.
  467.      * The result is <code>true</code> if and only if the argument is
  468.      * not <code>null</code> and is a <code>Calendar</code> object that
  469.      * represents the same calendar as this object.
  470.      * @param obj the object to compare with.
  471.      * @return <code>true</code> if the objects are the same;
  472.      * <code>false</code> otherwise.
  473.      */
  474.     public boolean equals(Object obj) {
  475.         if (this == obj) {
  476.             return true;
  477.         }
  478.         if (!(obj instanceof Calendar)) {
  479.             return false;
  480.         }
  481.         Calendar that = (Calendar)obj;
  482.         return getTimeInMillis() == that.getTimeInMillis() && zone.equals(that.zone);
  483.     }
  484.     /**
  485.      * Compares the time field records.
  486.      * Equivalent to comparing result of conversion to UTC.
  487.      * @param when the Calendar to be compared with this Calendar.
  488.      * @return true if the current time of this Calendar is before
  489.      * the time of Calendar when; false otherwise.
  490.      */
  491.     public boolean before(Object when) {
  492.         return (when instanceof Calendar
  493.                 && getTimeInMillis() < ((Calendar)when).getTimeInMillis());
  494.     }
  495.     /**
  496.      * Compares the time field records.
  497.      * Equivalent to comparing result of conversion to UTC.
  498.      * @param when the Calendar to be compared with this Calendar.
  499.      * @return true if the current time of this Calendar is after
  500.      * the time of Calendar when; false otherwise.
  501.      */
  502.     public boolean after(Object when) {
  503.         return (when instanceof Calendar
  504.                 && getTimeInMillis() > ((Calendar)when).getTimeInMillis());
  505.     }
  506.     /**
  507.      * Sets the time zone with the given time zone value.
  508.      * @param value the given time zone.
  509.      *
  510.      * @see #getTimeZone
  511.      */
  512.     public void setTimeZone(TimeZone value) {
  513.         zone = value;
  514.         getTimeInMillis();
  515.         computeFields();
  516.     }
  517.     /**
  518.      * Gets the time zone.
  519.      * @return the time zone object associated with this calendar.
  520.      *
  521.      * @see #setTimeZone
  522.      */
  523.     public TimeZone getTimeZone() {
  524.         return zone;
  525.     }
  526.     /**
  527.      * Converts
  528.      * the current millisecond time value
  529.      * <code>time</code>
  530.      * to field values in <code>fields[]</code>.
  531.      * This allows you to sync up the time field values with
  532.      * a new time that is set for the calendar.
  533.      */
  534.     protected abstract void computeFields();
  535.     /**
  536.      * Converts the current field values in <code>fields[]</code>
  537.      * to the millisecond time value
  538.      * <code>time</code>.
  539.      */
  540.     protected abstract void computeTime();
  541. }