Back to: Java Tutorials For Beginners and Professionals
Java Calendar Class with Examples
In this article, I am going to discuss Java Calendar Class with Examples. Please read our previous article where we discussed Date and Time API in Java.
Java Calendar Class:
java.util.Calendar is an abstract class. It provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. We cannot use a constructor to create an instance because it is an Abstract class. Therefore, we have to use the static method Calendar.getInstance() to instantiate and implement a sub-class.
Calendar.getInstance(): It returns a Calendar instance based on the current time in the default time zone with the default locale.
Calendar.getInstance(TimeZone zone)
Calendar.getInstance(Locale aLocale)
Calendar.getInstance(TimeZone zone, Locale aLocale)
The important points about Calendar −
It also provides additional fields and methods for implementing a concrete calendar system outside the package. It defines the range of values returned by certain calendar fields.
Declaration: public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar>
Methods
- public void add(int field, int amount): It is used to add the specified amount of time to the given calendar field.
- public boolean after(Object when): It returns true if the time represented by this Calendar is after the time represented by when Object.
- public boolean before(Object when): It basically returns true if the time represented by this Calendar is before the time represented by when Object.
- public final void clear(int field): It is used to set the given calendar field value and the time value of this Calendar undefined.
- public Object clone(): It provides the copy of the current object.
- public int compareTo(Calendar anotherCalendar): It is used to compare the time value between two calendar objects.
- protected void complete(): It is used to fill any unset fields in the calendar fields.
- protected abstract void computeFields(): It is used to convert the current millisecond time value time to calendar field values in fields[].
- protected abstract void computeTime(): It is used to convert calendar field values in the field[] to the millisecond time value time.
- public boolean equals(Object object): It is used to compare two objects for equality and returns true if they are equal.
- public int get(int field): It is passed as the parameter and returns the value of fields passed as the parameter.
- public int getActualMaximum(int field): It returns the maximum possible value of the calendar field passed as the parameter to this method.
- public int getActualMinimum(int field): It returns the minimum possible value of the calendar field passed as the parameter to this method.
- public static Set<String> getAvailableCalendarTypes(): It returns a set which contains string set of all available calendar type supported by JRE.
- public static Locale[] getAvailableLocales(): It returns an array of all locales available in JRE.
- public String getCalendarType(): It returns all available calendar types supported by JRE.
- public String getDisplayName(int field, int style, Locale locale): It returns the String representation of the calendar field value passed as the parameter in a given style and local.
- public Map<String, Integer> getDisplayNames(int field, int style, Locale locale): It returns Map representation of the calendar field value passed as the parameter.
- public int getFirstDayOfWeek(): It returns the first day of the week in integer form.
- public abstract int getGreatestMinimum(int field): It returns the highest minimum value of the Calendar field passed as the parameter.
- public static Calendar getInstance(): It is used with calendar objects to get the instance of the calendar according to the current time zone set by JRE.
- public abstract int getLeastMaximum(int field): It returns the smallest value from all maximum value for the field specified as the parameter to the method.
- public abstract int getMaximum(int field): It is used with calendar object to get the maximum value of the specified calendar field as the parameter.
- public int getMinimalDaysInFirstWeek(): It returns required minimum days in integer form.
- public abstract int getMinimum(int field): It is used with calendar objects to get the minimum value of the specified calendar field as the parameter.
- public final Date getTime(): It gets the time value of the calendar object and returns the date.
- public long getTimeInMillis(): It returns the current time in millisecond. Its returns type is long.
- public TimeZone getTimeZone(): It gets the Time zone of calendar object and returns a Time zone object.
- public int getWeeksInWeekYear(): It returns total weeks in week year. It is returned in integer form.
- public int getWeekYear(): It gets the week year represented by current Calendar.
- public int hashCode(): It returns the hash code for calendar object.
- Protected final int internalGet(int field): It returns the value of the calendar field passed as the parameter.
- public boolean isLenient(): It returns the Boolean value True if the interpretation mode of this calendar is lenient; false otherwise.
- public final boolean isSet(int field): It will check if the specified field as the parameter has been set or not. Returns false if it is not set; otherwise true.
- public boolean isWeekDateSupported(): It will check if this calendar supports the week date. Its default value is false.
- public abstract void roll(int field, Boolean up): It increases or decreases the specified calendar field by one unit without affecting the other field.
- public void set(int field, int value): It sets the specified calendar field by the specified value.
- public void setFirstDayOfWeek(int value): It sets the first day of the week. The value to be set is passed as a parameter.
- public void setMinimalDaysInFirstWeek(int value): It sets the minimal days required in the first week. The value to be set is passed as a parameter.
- public final void setTime(Date date): It sets the time of the current calendar object.
- public void setTimeInMillis(long millis): It sets the current time in milliseconds.
- public void setTimeZone(TimeZone value): It sets the timeZone with passed TimeZone value as the parameter.
- public void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek): It sets the current date with specified integer value as the parameter.
- public final Instant toInstant(): It converts the current object to an instant.
- public String toString(): It returns the string representation of the current object.
Sample Program for Java Calendar Class
import java.util.Calendar; public class CalendarDemo1 { public static void main (String[]args) { Calendar calendar = Calendar.getInstance (); System.out.println ("The current date is : " + calendar.getTime ()); calendar.add (Calendar.DATE, -15); System.out.println ("15 days ago: " + calendar.getTime ()); calendar.add (Calendar.MONTH, 4); System.out.println ("4 months later: " + calendar.getTime ()); calendar.add (Calendar.YEAR, 2); System.out.println ("2 years later: " + calendar.getTime ()); } }
Output
Sample Program
import java.util.*; public class CalendarDemo2 { public static void main (String[]args) { Calendar calendar = Calendar.getInstance (); System.out.println ("At present Calendar's Year: " + calendar.get (Calendar.YEAR)); System.out.println ("At present Calendar's Day: " + calendar.get (Calendar.DATE)); System.out.print ("At present Date And Time Is: " + calendar.getTime ()); int maximum = calendar.getMaximum (Calendar.DAY_OF_WEEK); System.out.println ("Maximum number of days in week: " + maximum); maximum = calendar.getMaximum (Calendar.WEEK_OF_YEAR); System.out.println ("Maximum number of weeks in year: " + maximum); int minimum = calendar.getMinimum (Calendar.DAY_OF_WEEK); System.out.println ("Minimum number of days in week: " + minimum); minimum = calendar.getMinimum (Calendar.WEEK_OF_YEAR); System.out.println ("Minimum number of weeks in year: " + minimum); } }
Output
In the next article, I am going to discuss System.Exit Method in Java with Examples. Here, in this article, I try to explain Java Calendar Class with Examples. I hope you enjoy this Java Calendar Class with Examples article.