Java.util.Calendar.get(int field) Method

Advertisements


Description

The java.util.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

Example

The following example shows the usage of java.util.calendar.get() method.

package com.tutorialspoint;

import java.util.*;

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));
   }
}

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

Calendar's Year: 2012
Calendar's Month: 3
Calendar's Day: 29



Advertisements
Advertisements