calendar.java

来自「java源代码 请看看啊 提点宝贵的意见」· Java 代码 · 共 1,730 行 · 第 1/5 页

JAVA
1,730 行
字号
    }    /**     * Sets the values for the fields year, month, date, hour, minute, and second.     * Previous values of other fields are retained.  If this is not desired,     * call <code>clear</code> first.     * @param year the value used to set the YEAR time field.     * @param month the value used to set the MONTH time field.     * Month value is 0-based. e.g., 0 for January.     * @param date the value used to set the DATE time field.     * @param hour the value used to set the HOUR_OF_DAY time field.     * @param minute the value used to set the MINUTE time field.     * @param second the value used to set the SECOND time field.     */    public final void set(int year, int month, int date, int hour, int minute,                          int second)    {        set(YEAR, year);        set(MONTH, month);        set(DATE, date);        set(HOUR_OF_DAY, hour);        set(MINUTE, minute);        set(SECOND, second);    }    /**     * Clears the values of all the time fields.     */    public final void clear()    {        fields = new int[FIELD_COUNT];        stamp = new int[FIELD_COUNT];        areFieldsSet = false;        areAllFieldsSet = false;        isSet = new boolean[FIELD_COUNT]; // Remove later        isTimeSet = false;    }    /**     * Clears the value in the given time field.     * @param field the time field to be cleared.     */    public final void clear(int field)    {        fields[field] = 0;        stamp[field] = UNSET;        areFieldsSet = false;        areAllFieldsSet = false;        isSet[field] = false; // Remove later        isTimeSet = false;    }    /**     * Determines if the given time field has a value set.     * @return true if the given time field has a value set; false otherwise.     */    public final boolean isSet(int field)    {        return stamp[field] != UNSET;        // return isSet[field];    }    /**     * Fills in any unset fields in the time field list.     */    protected void complete()    {        if (!isTimeSet) updateTime();        if (!areFieldsSet) {            computeFields(); // fills in unset fields            areFieldsSet = true;            areAllFieldsSet = true;        }    }    /**     * Compares this calendar to the specified object.     * The result is <code>true</code> if and only if the argument is     * not <code>null</code> and is a <code>Calendar</code> object that     * represents the same calendar as this object.     * @param obj the object to compare with.     * @return <code>true</code> if the objects are the same;     * <code>false</code> otherwise.     */    public boolean equals(Object obj) {        if (this == obj)            return true;        if (!(obj instanceof Calendar))            return false;        Calendar that = (Calendar)obj;        return getTimeInMillis() == that.getTimeInMillis() &&            lenient == that.lenient &&            firstDayOfWeek == that.firstDayOfWeek &&            minimalDaysInFirstWeek == that.minimalDaysInFirstWeek &&            zone.equals(that.zone);    }    /**     * Returns a hash code for this calendar.     * @return a hash code value for this object.      * @since 1.2     */    public int hashCode() {        /* Don't include the time because (a) we don't want the hash value to         * move around just because a calendar is set to different times, and         * (b) we don't want to trigger a time computation just to get a hash.         * Note that it is not necessary for unequal objects to always have         * unequal hashes, but equal objects must have equal hashes.  */        return (lenient ? 1 : 0)            | (firstDayOfWeek << 1)            | (minimalDaysInFirstWeek << 4)            | (zone.hashCode() << 7);    }    /**     * Compares the time field records.     * Equivalent to comparing result of conversion to UTC.     * @param when the Calendar to be compared with this Calendar.     * @return true if the current time of this Calendar is before     * the time of Calendar when; false otherwise.     */    public boolean before(Object when) {        return when instanceof Calendar &&            getTimeInMillis() < ((Calendar) when).getTimeInMillis();    }    /**     * Compares the time field records.     * Equivalent to comparing result of conversion to UTC.     * @param when the Calendar to be compared with this Calendar.     * @return true if the current time of this Calendar is after     * the time of Calendar when; false otherwise.     */    public boolean after(Object when) {        return when instanceof Calendar &&            getTimeInMillis() > ((Calendar) when).getTimeInMillis();    }    /**     * Date Arithmetic function.     * Adds the specified (signed) amount of time to the given time field,     * based on the calendar's rules. For example, to subtract 5 days from     * the current time of the calendar, you can achieve it by calling:     * <p>add(Calendar.DATE, -5).     * @param field the time field.     * @param amount the amount of date or time to be added to the field.     */    abstract public void add(int field, int amount);    /**     * Time Field Rolling function.     * Adds or subtracts (up/down) a single unit of time on the given time      * field without changing larger fields. For example, to roll the current      * date up by one day, you can achieve it by calling:     * <p>roll(Calendar.DATE, true).     * When rolling on the year or Calendar.YEAR field, it will roll the year     * value in the range between 1 and the value returned by calling     * getMaximum(Calendar.YEAR).     * When rolling on the month or Calendar.MONTH field, other fields like     * date might conflict and, need to be changed. For instance,     * rolling the month on the date 01/31/96 will result in 02/29/96.     * When rolling on the hour-in-day or Calendar.HOUR_OF_DAY field, it will     * roll the hour value in the range between 0 and 23, which is zero-based.     * @param field the time field.     * @param up indicates if the value of the specified time field is to be     * rolled up or rolled down. Use true if rolling up, false otherwise.     * @see Calendar#add     * @see Calendar#set     */    abstract public void roll(int field, boolean up);    /**     * Time Field Rolling function.     * Add to field a signed amount without changing larger fields.     * A negative roll amount means to roll down.     * [NOTE:  This default implementation on Calendar just repeatedly calls the     * version of roll() that takes a boolean and rolls by one unit.  This may not     * always do the right thing.  For example, if the DAY_OF_MONTH field is 31,     * rolling through February will leave it set to 28.  The GregorianCalendar     * version of this function takes care of this problem.  Other subclasses     * should also provide overrides of this function that do the right thing.     * @param field the time field.     * @param amount the signed amount to add to <code>field</code>.     * @since 1.2     * @see Calendar#add     * @see Calendar#set     */    public void roll(int field, int amount)    {        while (amount > 0) {            roll(field, true);            amount--;        }        while (amount < 0) {            roll(field, false);            amount++;        }    }    /**     * Sets the time zone with the given time zone value.     * @param value the given time zone.     */    public void setTimeZone(TimeZone value)    {        zone = value;        /* Recompute the fields from the time using the new zone.  This also         * works if isTimeSet is false (after a call to set()).  In that case         * the time will be computed from the fields using the new zone, then         * the fields will get recomputed from that.  Consider the sequence of         * calls: cal.setTimeZone(EST); cal.set(HOUR, 1); cal.setTimeZone(PST).         * Is cal set to 1 o'clock EST or 1 o'clock PST?  Answer: PST.  More         * generally, a call to setTimeZone() affects calls to set() BEFORE AND         * AFTER it up to the next call to complete().         */        areFieldsSet = false;    }    /**     * Gets the time zone.     * @return the time zone object associated with this calendar.     */    public TimeZone getTimeZone()    {        return zone;    }    /**     * Specify whether or not date/time interpretation is to be lenient.  With     * lenient interpretation, a date such as "February 942, 1996" will be     * treated as being equivalent to the 941st day after February 1, 1996.     * With strict interpretation, such dates will cause an exception to be     * thrown.     *     * @see java.text.DateFormat#setLenient     */    public void setLenient(boolean lenient)    {        this.lenient = lenient;    }    /**     * Tell whether date/time interpretation is to be lenient.     */    public boolean isLenient()    {        return lenient;    }    /**     * Sets what the first day of the week is; e.g., Sunday in US,     * Monday in France.     * @param value the given first day of the week.     */    public void setFirstDayOfWeek(int value)    {	if (firstDayOfWeek == value) {	    return;	}        firstDayOfWeek = value;	invalidateWeekFields();    }    /**     * Gets what the first day of the week is; e.g., Sunday in US,     * Monday in France.     * @return the first day of the week.     */    public int getFirstDayOfWeek()    {        return firstDayOfWeek;    }    /**     * Sets what the minimal days required in the first week of the year are;     * For example, if the first week is defined as one that contains the first     * day of the first month of a year, call the method with value 1. If it     * must be a full week, use value 7.     * @param value the given minimal days required in the first week     * of the year.     */    public void setMinimalDaysInFirstWeek(int value)    {        if (minimalDaysInFirstWeek == value) {	    return;	}        minimalDaysInFirstWeek = value;	invalidateWeekFields();    }    /**     * Gets what the minimal days required in the first week of the year are;     * e.g., if the first week is defined as one that contains the first day     * of the first month of a year, getMinimalDaysInFirstWeek returns 1. If     * the minimal days required must be a full week, getMinimalDaysInFirstWeek     * returns 7.     * @return the minimal days required in the first week of the year.     */    public int getMinimalDaysInFirstWeek()    {        return minimalDaysInFirstWeek;    }    /**     * Gets the minimum value for the given time field.     * e.g., for Gregorian DAY_OF_MONTH, 1.     * @param field the given time field.     * @return the minimum value for the given time field.     */    abstract public int getMinimum(int field);    /**     * Gets the maximum value for the given time field.     * e.g. for Gregorian DAY_OF_MONTH, 31.     * @param field the given time field.     * @return the maximum value for the given time field.     */    abstract public int getMaximum(int field);    /**     * Gets the highest minimum value for the given field if varies.     * Otherwise same as getMinimum(). For Gregorian, no difference.     * @param field the given time field.     * @return the highest minimum value for the given time field.     */    abstract public int getGreatestMinimum(int field);    /**     * Gets the lowest maximum value for the given field if varies.     * Otherwise same as getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.     * @param field the given time field.     * @return the lowest maximum value for the given time field.     */    abstract public int getLeastMaximum(int field);    /**     * Return the minimum value that this field could have, given the current date.     * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().     *     * The version of this function on Calendar uses an iterative algorithm to determine the     * actual minimum value for the field.  There is almost always a more efficient way to     * accomplish this (in most cases, you can simply return getMinimum()).  GregorianCalendar     * overrides this function with a more efficient implementation.     *

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?