Java Calendar get(int field) Method



Description

The Java Calendar get() method returns the value of the given calendar field.

Declaration

Following is the declaration for java.util.Calendar.get() method

public int get(int field)

Parameters

  • field − the given calendar field.

Return Value

The value of the given field.

Exception

ArrayIndexOutOfBoundsException − if the specified field is out of range

Getting Year, Month and Day from a Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar get() method. We're creating a Calendar instance of current date. We're getting values of Year, Month and Day and printing them.

package com.tutorialspoint;

import java.util.Calendar;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
      Calendar cal = Calendar.getInstance();

      // get the value of all the calendar date fields.
      System.out.println("Calendar's Year: " + cal.get(Calendar.YEAR));
      System.out.println("Calendar's Month: " + cal.get(Calendar.MONTH));
      System.out.println("Calendar's Day: " + cal.get(Calendar.DATE));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Calendar's Year: 2022
Calendar's Month: 8
Calendar's Day: 23

Getting Year, Month and Day from a Current Dated GregorianCalendar Instance Example

The following example shows the usage of Java Calendar get() method. We're creating a Calendar instance using GregorianCalendar object. We're getting values of Year, Month and Day and printing them.

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
      Calendar cal = new GregorianCalendar();

      // get the value of all the calendar date fields.
      System.out.println("Calendar's Year: " + cal.get(Calendar.YEAR));
      System.out.println("Calendar's Month: " + cal.get(Calendar.MONTH));
      System.out.println("Calendar's Day: " + cal.get(Calendar.DATE));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Calendar's Year: 2022
Calendar's Month: 8
Calendar's Day: 23

Getting Year, Month and Day from a Given Dated GregorianCalendar Instance Example

The following example shows the usage of Java Calendar get() method. We're creating a Calendar instance using GregorianCalendar object. We're getting different types of days and printing them.

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
      Calendar cal = new GregorianCalendar(2015, 8, 15);

      // get the value of all the calendar date fields.
      System.out.println("Calendar's Year: " + cal.get(Calendar.YEAR));
      System.out.println("Calendar's Month: " + cal.get(Calendar.MONTH));
      System.out.println("Calendar's Day: " + cal.get(Calendar.DATE));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Calendar's Year: 2015
Calendar's Month: 8
Calendar's Day: 15
java_util_calendar.htm
Advertisements