Java Utility Library
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java.util.Calendar Class



Java Calendar Class

Java.util package provides a Calendar class which represents a specific instant in time, with millisecond precision.

Class declaration

The declaration of java.util.Calendar class is:

public abstract class Calendar
  extends Object
    implements Serializable, Cloneable, Comparable<Calendar>

Fields

S.NField and Description
1. static int ALL_STYLES
A style specifier for getDisplayNames indicating names in all styles, such as "January" and "Jan".
2. static int AM
Value of the AM_PM field indicating the period of the day from midnight to just before noon.
3. static int AM_PM
Field number for get and set indicating whether the HOUR is before or after noon.
4. static int APRIL
Value of the MONTH field indicating the fourth month of the year in the Gregorian and Julian calendars.
5. protected boolean areFieldsSet
True if fields[] are in sync with the currently set time.
6. static int AUGUST
Value of the MONTH field indicating the eighth month of the year in the Gregorian and Julian calendars.
7. static int DATE
Field number for get and set indicating the day of the month.
8. static int DAY_OF_MONTH
Field number for get and set indicating the day of the month.
9. static int DAY_OF_WEEK
Field number for get and set indicating the day of the week.
10. 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.
11. static int DAY_OF_YEAR
Field number for get and set indicating the day number within the current year.
12. static int DECEMBER
Value of the MONTH field indicating the twelfth month of the year in the Gregorian and Julian calendars.
13. static int DST_OFFSET
Field number for get and set indicating the daylight saving offset in milliseconds.
14. static int ERA
Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.
15. static int FEBRUARY
Value of the MONTH field indicating the second month of the year in the Gregorian and Julian calendars.
16. static int FIELD_COUNT
The number of distinct fields recognized by get and set.
17. protected int[] fields
The calendar field values for the currently set time for this calendar.
18. static int FRIDAY
Value of the DAY_OF_WEEK field indicating Friday.
19. static int HOUR
Field number for get and set indicating the hour of the morning or afternoon.
20. static int HOUR_OF_DAY
Field number for get and set indicating the hour of the day.
21. protected boolean[] isSet
The flags which tell if a specified calendar field for the calendar is set.
22. protected boolean isTimeSet
True if then the value of time is valid.
23. static int JANUARY
Value of the MONTH field indicating the first month of the year in the Gregorian and Julian calendars.
24. static int JULY
Value of the MONTH field indicating the seventh month of the year in the Gregorian and Julian calendars.
25. static int JUNE
Value of the MONTH field indicating the sixth month of the year in the Gregorian and Julian calendars.
26. static int LONG
A style specifier for getDisplayName and getDisplayNames equivalent to LONG_FORMAT.
27. static int LONG_FORMAT
A style specifier for getDisplayName and getDisplayNames indicating a long name used for format.
28. static int LONG_STANDALONE
A style specifier for getDisplayName and getDisplayNames indicating a long name used independently, such as a month name as calendar headers.
29. static int MARCH
Value of the MONTH field indicating the third month of the year in the Gregorian and Julian calendars.
30. static int MAY
Value of the MONTH field indicating the fifth month of the year in the Gregorian and Julian calendars.
31. static int MILLISECOND
Field number for get and set indicating the millisecond within the second.
32. static int MINUTE
Field number for get and set indicating the minute within the hour.
33. static int MONDAY
Value of the DAY_OF_WEEK field indicating Monday.
34. static int MONTH
Field number for get and set indicating the month.
35. static int NARROW_FORMAT
A style specifier for getDisplayName and getDisplayNames indicating a narrow name used for format.
36. static int NARROW_STANDALONE
A style specifier for getDisplayName and getDisplayNames indicating a narrow name independently.
37. static int NOVEMBER
Value of the MONTH field indicating the eleventh month of the year in the Gregorian and Julian calendars.
38. static int OCTOBER
Value of the MONTH field indicating the tenth month of the year in the Gregorian and Julian calendars.
39. static int PM
Value of the AM_PM field indicating the period of the day from noon to just before midnight.
40. static int SATURDAY
Value of the DAY_OF_WEEK field indicating Saturday.
41. static int SECOND
Field number for get and set indicating the second within the minute.
42. static int SEPTEMBER
Value of the MONTH field indicating the ninth month of the year in the Gregorian and Julian calendars.
43. static int SHORT
A style specifier for getDisplayName and getDisplayNames equivalent to SHORT_FORMAT.
44. static int SHORT_FORMAT
A style specifier for getDisplayName and getDisplayNames indicating a short name used for format.
45. static int SHORT_STANDALONE
A style specifier for getDisplayName and getDisplayNames indicating a short name used independently, such as a month abbreviation as calendar headers.
46. static int SUNDAY
Value of the DAY_OF_WEEK field indicating Sunday.
47. static int THURSDAY
Value of the DAY_OF_WEEK field indicating Thursday.
48. protected long time
The currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.
49. static int TUESDAY
Value of the DAY_OF_WEEK field indicating Tuesday.
50. static int UNDECIMBER
Value of the MONTH field indicating the thirteenth month of the year.
51. static int WEDNESDAY
Value of the DAY_OF_WEEK field indicating Wednesday.
52. static int WEEK_OF_MONTH
Field number for get and set indicating the week number within the current month.
53. static int WEEK_OF_YEAR
Field number for get and set indicating the week number within the current year.
54. static int YEAR
Field number for get and set indicating the year.
55. static int ZONE_OFFSET
Field number for get and set indicating the raw offset from GMT in milliseconds.

Class Constructors

S.NConstructors & Description
1. protected Calendar()
Constructs a Calendar with the default time zone and the default FORMAT locale.
2. protected Calendar(TimeZone zone, Locale aLocale)
Constructs a calendar with the specified time zone and locale.

java.util.Calendar Methods

The java.util.Calendar class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. abstract void add(int field, int amount)
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
2. boolean after(Object when)
Returns whether this Calendar represents a time after the time represented by the specified Object.
3. boolean before(Object when)
Returns whether this Calendar represents a time before the time represented by the specified Object.
4. void clear()
Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.
5. void clear(int field)
Sets the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined.
6. Object clone()
Creates and returns a copy of this object.
7. int compareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
8. protected void complete()
Fills in any unset fields in the calendar fields.
9. protected abstract void computeFields()
Converts the current millisecond time value time to calendar field values in fields[].
10. protected abstract void computeTime()
Converts the current calendar field values in fields[] to the millisecond time value time.
11. boolean equals(Object obj)
Compares this Calendar to the specified Object.
12. int get(int field)
Returns the value of the given calendar field.
13. int getActualMaximum(int field)
Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.
14. int getActualMinimum(int field)
Returns the minimum value that the specified calendar field could have, given the time value of this Calendar.
15. static Set<String> getAvailableCalendarTypes()
Returns an unmodifiable Set containing all calendar types supported by Calendar in the runtime environment.
16. static Locale[] getAvailableLocales()
Returns an array of all locales for which the getInstance methods of this class can return localized instances.
17. String getCalendarType()
Returns the calendar type of this Calendar.
18. String getDisplayName(int field, int style, Locale locale)
Returns the string representation of the calendar field value in the given style and locale.
19. Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
Returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.
20. int getFirstDayOfWeek()
Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
21. abstract int getGreatestMinimum(int field)
Returns the highest minimum value for the given calendar field of this Calendar instance.
22. static Calendar getInstance()
Gets a calendar using the default time zone and locale.
23. static Calendar getInstance(Locale aLocale)
Gets a calendar using the default time zone and specified locale.
24. static Calendar getInstance(TimeZone zone)
Gets a calendar using the specified time zone and default locale.
25. static Calendar getInstance(TimeZone zone, Locale aLocale)
Gets a calendar with the specified time zone and locale.
26. abstract int getLeastMaximum(int field)
Returns the lowest maximum value for the given calendar field of this Calendar instance.
27. abstract int getMaximum(int field)
Returns the maximum value for the given calendar field of this Calendar instance.
28. int getMinimalDaysInFirstWeek()
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, this method returns 1.
29. abstract int getMinimum(int field)
Returns the minimum value for the given calendar field of this Calendar instance.
30. Date getTime()
Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").
31. long getTimeInMillis()
Returns this Calendar's time value in milliseconds.
32. TimeZone getTimeZone()
Gets the time zone.
33. int getWeeksInWeekYear()
Returns the number of weeks in the week year represented by this Calendar.
34. int getWeekYear()
Returns the week year represented by this Calendar.
35. int hashCode()
Returns a hash code for this calendar.
36. protected int internalGet(int field)
Returns the value of the given calendar field.
37. boolean isLenient()
Tells whether date/time interpretation is to be lenient.
38. boolean isSet(int field)
Determines if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a get method call.
39. boolean isWeekDateSupported()
Returns whether this Calendar supports week dates.
40. abstract void roll(int field, boolean up)
Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.
41. void roll(int field, int amount)
Adds the specified (signed) amount to the specified calendar field without changing larger fields.
42. void set(int field, int value)
Sets the given calendar field to the given value.
43. void set(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
44. void set(int year, int month, int date, int hourOfDay, int minute)
Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.
45. void set(int year, int month, int date, int hourOfDay, int minute, int second)
Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, and SECOND.
46. void setFirstDayOfWeek(int value)
Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
47. void setLenient(boolean lenient)
Specifies whether or not date/time interpretation is to be lenient.
48. void setMinimalDaysInFirstWeek(int value)
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 this method with value 1.
49. void setTime(Date date)
Sets this Calendar's time with the given Date.
50. void setTimeInMillis(long millis)
Sets this Calendar's current time from the given long value.
51. void setTimeZone(TimeZone value)
Sets the time zone with the given time zone value.
52. void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
Sets the date of this Calendar with the given date specifiers - week year, week of year, and day of week.
53. Instant toInstant()
Converts this object to an Instant.
54. String toString()
Return a string representation of this calendar.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object

5