Package java.util

Class Calendar

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int AM
      Value of the AM_PM field indicating the period of the day from midnight to just before noon.
      static int AM_PM
      Field number for get and set indicating whether the HOUR is before or after noon.
      static int APRIL
      Value of the MONTH field indicating the fourth month of the year in the Gregorian and Julian calendars.
      static int AUGUST
      Value of the MONTH field indicating the eighth month of the year in the Gregorian and Julian calendars.
      static int DATE
      Field number for get and set indicating the day of the month.
      static int DAY_OF_MONTH
      Field number for get and set indicating the day of the month.
      static int DAY_OF_WEEK
      Field number for get and set indicating the day of the week.
      static int DAY_OF_WEEK_IN_MONTH
      Field number for get and set indicating the ordinal number of the day of the week within the current month.
      static int DAY_OF_YEAR
      Field number for get and set indicating the day number within the current year.
      static int DECEMBER
      Value of the MONTH field indicating the twelfth month of the year in the Gregorian and Julian calendars.
      static int ERA
      Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.
      static int FEBRUARY
      Value of the MONTH field indicating the second month of the year in the Gregorian and Julian calendars.
      static int FRIDAY
      Value of the DAY_OF_WEEK field indicating Friday.
      static int HOUR
      Field number for get and set indicating the hour of the morning or afternoon.
      static int HOUR_OF_DAY
      Field number for get and set indicating the hour of the day.
      static int JANUARY
      Value of the MONTH field indicating the first month of the year in the Gregorian and Julian calendars.
      static int JULY
      Value of the MONTH field indicating the seventh month of the year in the Gregorian and Julian calendars.
      static int JUNE
      Value of the MONTH field indicating the sixth month of the year in the Gregorian and Julian calendars.
      static int MARCH
      Value of the MONTH field indicating the third month of the year in the Gregorian and Julian calendars.
      static int MAY
      Value of the MONTH field indicating the fifth month of the year in the Gregorian and Julian calendars.
      static int MILLISECOND
      Field number for get and set indicating the millisecond within the second.
      static int MINUTE
      Field number for get and set indicating the minute within the hour.
      static int MONDAY
      Value of the DAY_OF_WEEK field indicating Monday.
      static int MONTH
      Field number for get and set indicating the month.
      static int NOVEMBER
      Value of the MONTH field indicating the eleventh month of the year in the Gregorian and Julian calendars.
      static int OCTOBER
      Value of the MONTH field indicating the tenth month of the year in the Gregorian and Julian calendars.
      static int PM
      Value of the AM_PM field indicating the period of the day from noon to just before midnight.
      static int SATURDAY
      Value of the DAY_OF_WEEK field indicating Saturday.
      static int SECOND
      Field number for get and set indicating the second within the minute.
      static int SEPTEMBER
      Value of the MONTH field indicating the ninth month of the year in the Gregorian and Julian calendars.
      static int SUNDAY
      Value of the DAY_OF_WEEK field indicating Sunday.
      static int THURSDAY
      Value of the DAY_OF_WEEK field indicating Thursday.
      static int TUESDAY
      Value of the DAY_OF_WEEK field indicating Tuesday.
      static int UNDECIMBER
      Value of the MONTH field indicating the thirteenth month of the year.
      static int WEDNESDAY
      Value of the DAY_OF_WEEK field indicating Wednesday.
      static int WEEK_OF_MONTH
      Field number for get and set indicating the week number within the current month.
      static int WEEK_OF_YEAR
      Field number for get and set indicating the week number within the current year.
      static int YEAR
      Field number for get and set indicating the year.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Calendar()  
    • Field Detail

      • YEAR

        public static final int YEAR
        Field number for get and set indicating the year. This is a calendar-specific value; see subclass documentation.
        See Also:
        Constant Field Values
      • WEEK_OF_YEAR

        public static final int WEEK_OF_YEAR
        Field number for get and set indicating the week number within the current year. The first week of the year, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1. Subclasses define the value of WEEK_OF_YEAR for days before the first week of the year.
        See Also:
        getFirstDayOfWeek(), getMinimalDaysInFirstWeek(), Constant Field Values
      • WEEK_OF_MONTH

        public static final int WEEK_OF_MONTH
        Field number for get and set indicating the week number within the current month. The first week of the month, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1. Subclasses define the value of WEEK_OF_MONTH for days before the first week of the month.
        See Also:
        getFirstDayOfWeek(), getMinimalDaysInFirstWeek(), Constant Field Values
      • DATE

        public static final int DATE
        Field number for get and set indicating the day of the month. This is a synonym for DAY_OF_MONTH. The first day of the month has value 1.
        See Also:
        DAY_OF_MONTH, Constant Field Values
      • DAY_OF_MONTH

        public static final int DAY_OF_MONTH
        Field number for get and set indicating the day of the month. This is a synonym for DATE. The first day of the month has value 1.
        See Also:
        DATE, Constant Field Values
      • DAY_OF_YEAR

        public static final int DAY_OF_YEAR
        Field number for get and set indicating the day number within the current year. The first day of the year has value 1.
        See Also:
        Constant Field Values
      • DAY_OF_WEEK_IN_MONTH

        public static final int DAY_OF_WEEK_IN_MONTH
        Field number for get and set indicating the ordinal number of the day of the week within the current month. Together with the DAY_OF_WEEK field, this uniquely specifies a day within a month. Unlike WEEK_OF_MONTH and WEEK_OF_YEAR, this field's value does not depend on getFirstDayOfWeek() or getMinimalDaysInFirstWeek(). DAY_OF_MONTH 1 through 7 always correspond to DAY_OF_WEEK_IN_MONTH 1; 8 through 14 correspond to DAY_OF_WEEK_IN_MONTH 2, and so on. DAY_OF_WEEK_IN_MONTH 0 indicates the week before DAY_OF_WEEK_IN_MONTH 1. Negative values count back from the end of the month, so the last Sunday of a month is specified as DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1. Because negative values count backward they will usually be aligned differently within the month than positive values. For example, if a month has 31 days, DAY_OF_WEEK_IN_MONTH -1 will overlap DAY_OF_WEEK_IN_MONTH 5 and the end of 4.
        See Also:
        DAY_OF_WEEK, WEEK_OF_MONTH, Constant Field Values
      • AM_PM

        public static final int AM_PM
        Field number for get and set indicating whether the HOUR is before or after noon. E.g., at 10:04:15.250 PM the AM_PM is PM.
        See Also:
        AM, PM, HOUR, Constant Field Values
      • HOUR

        public static final int HOUR
        Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.
        See Also:
        AM_PM, HOUR_OF_DAY, Constant Field Values
      • HOUR_OF_DAY

        public static final int HOUR_OF_DAY
        Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.
        See Also:
        HOUR, Constant Field Values
      • MINUTE

        public static final int MINUTE
        Field number for get and set indicating the minute within the hour. E.g., at 10:04:15.250 PM the MINUTE is 4.
        See Also:
        Constant Field Values
      • SECOND

        public static final int SECOND
        Field number for get and set indicating the second within the minute. E.g., at 10:04:15.250 PM the SECOND is 15.
        See Also:
        Constant Field Values
      • MILLISECOND

        public static final int MILLISECOND
        Field number for get and set indicating the millisecond within the second. E.g., at 10:04:15.250 PM the MILLISECOND is 250.
        See Also:
        Constant Field Values
      • JANUARY

        public static final int JANUARY
        Value of the MONTH field indicating the first month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • FEBRUARY

        public static final int FEBRUARY
        Value of the MONTH field indicating the second month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • MARCH

        public static final int MARCH
        Value of the MONTH field indicating the third month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • APRIL

        public static final int APRIL
        Value of the MONTH field indicating the fourth month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • MAY

        public static final int MAY
        Value of the MONTH field indicating the fifth month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • JUNE

        public static final int JUNE
        Value of the MONTH field indicating the sixth month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • JULY

        public static final int JULY
        Value of the MONTH field indicating the seventh month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • AUGUST

        public static final int AUGUST
        Value of the MONTH field indicating the eighth month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • SEPTEMBER

        public static final int SEPTEMBER
        Value of the MONTH field indicating the ninth month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • OCTOBER

        public static final int OCTOBER
        Value of the MONTH field indicating the tenth month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • NOVEMBER

        public static final int NOVEMBER
        Value of the MONTH field indicating the eleventh month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • DECEMBER

        public static final int DECEMBER
        Value of the MONTH field indicating the twelfth month of the year in the Gregorian and Julian calendars.
        See Also:
        Constant Field Values
      • UNDECIMBER

        public static final int UNDECIMBER
        Value of the MONTH field indicating the thirteenth month of the year. Although GregorianCalendar does not use this value, lunar calendars do.
        See Also:
        Constant Field Values
      • AM

        public static final int AM
        Value of the AM_PM field indicating the period of the day from midnight to just before noon.
        See Also:
        Constant Field Values
      • PM

        public static final int PM
        Value of the AM_PM field indicating the period of the day from noon to just before midnight.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Calendar

        protected Calendar()
    • Method Detail

      • getTime

        public abstract Date getTime()
        Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").
        Returns:
        a Date representing the time value.
        See Also:
        setTime(Date), getTimeInMillis()
      • setTime

        public abstract void setTime​(Date date)
        Sets this Calendar's time with the given Date.

        Note: Calling setTime() with Date(Long.MAX_VALUE) or Date(Long.MIN_VALUE) may yield incorrect field values from get().

        Parameters:
        date - the given Date.
        See Also:
        getTime(), setTimeInMillis(long)
      • getTimeInMillis

        public abstract long getTimeInMillis()
        Returns this Calendar's time value in milliseconds.
        Returns:
        the current time as UTC milliseconds from the epoch.
        See Also:
        getTime(), setTimeInMillis(long)
      • setTimeInMillis

        public abstract void setTimeInMillis​(long millis)
        Sets this Calendar's current time from the given long value.
        Parameters:
        millis - the new time in UTC milliseconds from the epoch.
        See Also:
        setTime(Date), getTimeInMillis()
      • get

        public abstract int get​(int field)
      • set

        public abstract void set​(int field,
                                 int value)
        Sets the given calendar field to the given value. The value is not interpreted by this method regardless of the leniency mode.
        Parameters:
        field - the given calendar field.
        value - the value to be set for the given calendar field.
        Throws:
        ArrayIndexOutOfBoundsException - if the specified field is out of range (field < 0 || field >= FIELD_COUNT). in non-lenient mode.
        See Also:
        #set(int,int,int), #set(int,int,int,int,int), #set(int,int,int,int,int,int), get(int)
      • add

        public abstract void add​(int field,
                                 int value)
      • clear

        public abstract void clear()
      • getFirstDayOfWeek

        public int getFirstDayOfWeek()
      • setFirstDayOfWeek

        public void setFirstDayOfWeek​(int newFirstDayOfWeek)
      • getMinimalDaysInFirstWeek

        public int getMinimalDaysInFirstWeek()
      • setMinimalDaysInFirstWeek

        public void setMinimalDaysInFirstWeek​(int newMinimalDaysInFirstWeek)
      • getInstance

        public static Calendar getInstance()
      • before

        public boolean before​(Object when)
        Returns whether this Calendar represents a time before the time represented by the specified Object. This method is equivalent to:
         
        compareTo(when) < 0
        if and only if when is a Calendar instance. Otherwise, the method returns false.
        Parameters:
        when - the Object to be compared
        Returns:
        true if the time of this Calendar is before the time represented by when; false otherwise.
        See Also:
        compareTo(Calendar)
      • after

        public boolean after​(Object when)
        Returns whether this Calendar represents a time after the time represented by the specified Object. This method is equivalent to:
         
        compareTo(when) > 0
        if and only if when is a Calendar instance. Otherwise, the method returns false.
        Parameters:
        when - the Object to be compared
        Returns:
        true if the time of this Calendar is after the time represented by when; false otherwise.
        See Also:
        compareTo(Calendar)
      • equals

        public boolean equals​(Object obj)
        Compares this Calendar to the specified Object. The result is true if and only if the argument is a Calendar object of the same calendar system that represents the same time value (millisecond offset from the Epoch) under the same Calendar parameters as this object.

        The Calendar parameters are the values represented by the isLenient, getFirstDayOfWeek, getMinimalDaysInFirstWeek and getTimeZone methods. If there is any difference in those parameters between the two Calendars, this method returns false.

        Use the compareTo method to compare only the time values.

        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare with.
        Returns:
        true if this object is equal to obj; false otherwise.
      • compareTo

        public int compareTo​(Calendar anotherCalendar)
        Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
        Parameters:
        anotherCalendar - the Calendar to be compared.
        Returns:
        the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than 0 if the time of this Calendar is after the time represented by the argument.
        Throws:
        NullPointerException - if the specified Calendar is null.
        IllegalArgumentException - if the time value of the specified Calendar object can't be obtained due to any invalid calendar values.
        Since:
        1.5
      • setLenient

        public void setLenient​(boolean lenient)
        Specifies 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 (non-lenient) interpretation, such dates will cause an exception to be thrown. The default is lenient.
        Parameters:
        lenient - true if the lenient mode is to be turned on; false if it is to be turned off.
        See Also:
        isLenient(), DateFormat.setLenient(boolean)
      • isLenient

        public boolean isLenient()
        Tells whether date/time interpretation is to be lenient.
        Returns:
        true if the interpretation mode of this calendar is lenient; false otherwise.
        See Also:
        setLenient(boolean)
      • getActualMaximum

        public int getActualMaximum​(int field)
      • getActualMinimum

        public int getActualMinimum​(int field)