Package java.util

Class Calendar

java.lang.Object
java.util.Calendar
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
GregorianCalendar

public abstract class Calendar extends Object implements Cloneable
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Value of the AM_PM field indicating the period of the day from midnight to just before noon.
    static final int
    Field number for get and set indicating whether the HOUR is before or after noon.
    static final int
    Value of the MONTH field indicating the fourth month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the MONTH field indicating the eighth month of the year in the Gregorian and Julian calendars.
    static final int
    Field number for get and set indicating the day of the month.
    static final int
    Field number for get and set indicating the day of the month.
    static final int
    Field number for get and set indicating the day of the week.
    static final int
    Field number for get and set indicating the ordinal number of the day of the week within the current month.
    static final int
    Field number for get and set indicating the day number within the current year.
    static final int
    Value of the MONTH field indicating the twelfth month of the year in the Gregorian and Julian calendars.
    static final int
    Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.
    static final int
    Value of the MONTH field indicating the second month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the DAY_OF_WEEK field indicating Friday.
    static final int
    Field number for get and set indicating the hour of the morning or afternoon.
    static final int
    Field number for get and set indicating the hour of the day.
    static final int
    Value of the MONTH field indicating the first month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the MONTH field indicating the seventh month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the MONTH field indicating the sixth month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the MONTH field indicating the third month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the MONTH field indicating the fifth month of the year in the Gregorian and Julian calendars.
    static final int
    Field number for get and set indicating the millisecond within the second.
    static final int
    Field number for get and set indicating the minute within the hour.
    static final int
    Value of the DAY_OF_WEEK field indicating Monday.
    static final int
    Field number for get and set indicating the month.
    static final int
    Value of the MONTH field indicating the eleventh month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the MONTH field indicating the tenth month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the AM_PM field indicating the period of the day from noon to just before midnight.
    static final int
    Value of the DAY_OF_WEEK field indicating Saturday.
    static final int
    Field number for get and set indicating the second within the minute.
    static final int
    Value of the MONTH field indicating the ninth month of the year in the Gregorian and Julian calendars.
    static final int
    Value of the DAY_OF_WEEK field indicating Sunday.
    static final int
    Value of the DAY_OF_WEEK field indicating Thursday.
    static final int
    Value of the DAY_OF_WEEK field indicating Tuesday.
    static final int
    Value of the MONTH field indicating the thirteenth month of the year.
    static final int
    Value of the DAY_OF_WEEK field indicating Wednesday.
    static final int
    Field number for get and set indicating the week number within the current month.
    static final int
    Field number for get and set indicating the week number within the current year.
    static final int
    Field number for get and set indicating the year.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    add(int field, int value)
     
    boolean
    after(Object when)
    Returns whether this Calendar represents a time after the time represented by the specified Object.
    boolean
    before(Object when)
    Returns whether this Calendar represents a time before the time represented by the specified Object.
    abstract void
     
    abstract Object
     
    int
    compareTo(Calendar anotherCalendar)
    Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
    boolean
    Compares this Calendar to the specified Object.
    abstract int
    get(int field)
     
    int
    getActualMaximum(int field)
     
    int
    getActualMinimum(int field)
     
    int
     
    static Calendar
     
    int
     
    abstract Date
    Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").
    abstract long
    Returns this Calendar's time value in milliseconds.
    boolean
    Tells whether date/time interpretation is to be lenient.
    abstract void
    set(int field, int value)
    Sets the given calendar field to the given value.
    void
    setFirstDayOfWeek(int newFirstDayOfWeek)
     
    void
    setLenient(boolean lenient)
    Specifies whether or not date/time interpretation is to be lenient.
    void
    setMinimalDaysInFirstWeek(int newMinimalDaysInFirstWeek)
     
    abstract void
    setTime(Date date)
    Sets this Calendar's time with the given Date.
    abstract void
    setTimeInMillis(long millis)
    Sets this Calendar's current time from the given long value.

    Methods inherited from class java.lang.Object

    finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ERA

      public static final int ERA
      Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar. This is a calendar-specific value; see subclass documentation.
      See Also:
    • 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:
    • MONTH

      public static final int MONTH
      Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
      See Also:
    • 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:
    • 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:
    • 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

      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:
    • 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:
    • DAY_OF_WEEK

      public static final int DAY_OF_WEEK
      Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
      See Also:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • SUNDAY

      public static final int SUNDAY
      Value of the DAY_OF_WEEK field indicating Sunday.
      See Also:
    • MONDAY

      public static final int MONDAY
      Value of the DAY_OF_WEEK field indicating Monday.
      See Also:
    • TUESDAY

      public static final int TUESDAY
      Value of the DAY_OF_WEEK field indicating Tuesday.
      See Also:
    • WEDNESDAY

      public static final int WEDNESDAY
      Value of the DAY_OF_WEEK field indicating Wednesday.
      See Also:
    • THURSDAY

      public static final int THURSDAY
      Value of the DAY_OF_WEEK field indicating Thursday.
      See Also:
    • FRIDAY

      public static final int FRIDAY
      Value of the DAY_OF_WEEK field indicating Friday.
      See Also:
    • SATURDAY

      public static final int SATURDAY
      Value of the DAY_OF_WEEK field indicating Saturday.
      See Also:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
  • Constructor Details

    • Calendar

      protected Calendar()
  • Method Details

    • 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

      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:
    • 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:
    • 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:
    • 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:
    • 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)
    • clone

      public abstract Object clone()
      Overrides:
      clone in class Object
    • 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:
    • 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:
    • 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

      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:
    • getActualMaximum

      public int getActualMaximum(int field)
    • getActualMinimum

      public int getActualMinimum(int field)