Calendar Functions in Java


The java.util.calendar class provides the calendar functions in Java. Is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

Following are the calendar functions in Java −

Sr.NoMethod & Description
1abstract void add(int field, int amount)
This method adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
2boolean after(Object when)
This method returns whether this Calendar represents a time after the time represented by the specified Object.
3boolean before(Object when)
This method returns whether this Calendar represents a time before the time represented by the specified Object.
4void clear()
This method sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.
5void clear(int field)
This method sets the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined.
6Object clone()
This method creates and returns a copy of this object.
7int compareTo(Calendar anotherCalendar)
This method compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
8protected void complete()
This method fills in any unset fields in the calendar fields.
9protected abstract void computeFields()
This method converts the current millisecond time value time to calendar field values in fields[].
10protected abstract void computeTime()
This method converts the current calendar field values in fields[] to the millisecond time value time.
11boolean equals(Object obj)
This method compares this Calendar to the specified Object.
12int get(int field)
This method returns the value of the given calendar field.
13int getActualMaximum(int field)
This method returns the maximum value that the specified calendar field could have, given the time value of this Calendar.
14int getActualMinimum(int field)
This method returns the minimum value that the specified calendar field could have, given the time value of this Calendar.
15static Locale[] getAvailableLocales()
This method returns an array of all locales for which the getInstance methods of this class can return localized instances.
16String getDisplayName(int field, int style, Locale locale)
This method returns the string representation of the calendar field value in the given style and locale.
17Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
This method returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.
18int getFirstDayOfWeek()
This method gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
19abstract int getGreatestMinimum(int field)
This method returns the highest minimum value for the given calendar field of this Calendar instance.
20static Calendar getInstance()
This method gets a calendar using the default time zone and locale.
21static Calendar getInstance(Locale aLocale)
This method gets a calendar using the default time zone and specified locale.
22static Calendar getInstance(TimeZone zone)
This method gets a calendar using the specified time zone and default locale.
23static Calendar getInstance(TimeZone zone, Locale aLocale)
This method gets a calendar with the specified time zone and locale.
24abstract int getLeastMaximum(int field)
This method returns the lowest maximum value for the given calendar field of this Calendar instance.
25abstract int getMaximum(int field)
This method returns the maximum value for the given calendar field of this Calendar instance.
26int getMinimalDaysInFirstWeek()
This method 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.
27abstract int getMinimum(int field)
This method returns the minimum value for the given calendar field of this Calendar instance.
28Date getTime()
This method returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").
29long getTimeInMillis()
This method returns this Calendar's time value in milliseconds.
30TimeZone getTimeZone()
This method gets the time zone.
31int hashCode()
This method Returns a hash code for this calendar.
32protected int internalGet(int field)
This method returns the value of the given calendar field.
33boolean isLenient()
This method tells whether date/time interpretation is to be lenient.
34boolean isSet(int field)
This method 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.
35abstract void roll(int field, boolean up)
This method adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.
36void roll(int field, int amount)
This method adds the specified (signed) amount to the specified calendar field without changing larger fields.
37void set(int field, int value)
This method sets the given calendar field to the given value.
38void set(int year, int month, int date)
This method sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
39void set(int year, int month, int date, int hourOfDay, int minute)
This method sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.
40void set(int year, int month, int date, int hourOfDay, int minute, int second)
This method sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND.
41void setFirstDayOfWeek(int value)
This method sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
42void setLenient(boolean lenient)
This method specifies whether or not date/time interpretation is to be lenient.
43void setMinimalDaysInFirstWeek(int value)
This method 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.
44void setTime(Date date)
This method sets this Calendar's time with the given Date.
45void setTimeInMillis(long millis)
This method sets this Calendar's current time from the given long value.
46void setTimeZone(TimeZone value)
This method sets the time zone with the given time zone value.
47String toString()
This method return a string representation of this calendar.

Let us now see an example of the equals() method.

The java.util.Calendar.equals() method compares this Calendar to the specified Object −

Example

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      // create two calendars
      Calendar cal = Calendar.getInstance();
      // specify a date for one of them
      Calendar cal2 = new GregorianCalendar(2011, 04, 29);
      // compare the two calendars.
      boolean b = cal.equals(cal2);
      // print result
      System.out.println("Calendars are equal :" + b);
   }
}

Output

Calendars are equal :false

Let us see another example wherein we are getting the first day of week. The java.util.Calendar.getFirstDayOfWeek() method returns the first day of the week.

Example

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      // create a new calendar
      Calendar cal = Calendar.getInstance();
      // print the first day of the week
      System.out.println("First day is :" + cal.getFirstDayOfWeek());
      int day = cal.getFirstDayOfWeek();
      switch (day) {
         case (1):
            System.out.println("Sunday");
            break;
         case (2):
            System.out.println("Monday");
            break;
         case 3:
            System.out.println("Tuesday");
            break;
         case 4:
            System.out.println("Wednesday");
            break;
         case 5:
            System.out.println("Thrusday");
            break;
         case 6:
            System.out.println("Friday");
            break;
         case 7:
            System.out.println("Saturday");
            break;
      }
   }
}

Output

First day is :1
Sunday

Updated on: 24-Sep-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements