Java Utility Library

Java Calendar - get() Method



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

Syntax

public int get(int field)

Parameters

field Specify the given calendar field.

Return Value

Returns the value for the given calendar field.

Exception

Throws ArrayIndexOutOfBoundsException, if the specified field is out of range (field < 0 || field >= FIELD_COUNT).

Example:

In the example below, the java.util.Calendar.get() method returns the value of the given calendar field.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar instance
    Calendar Cal = Calendar.getInstance();

    //printing the Calendar
    System.out.println("The Calendar is: " + Cal.getTime());

    //printing the year from the calendar object
    System.out.println("Calendar Year is: " + Cal.get(Calendar.YEAR));

    //printing the day from the calendar object
    System.out.println("Calendar Day is: " + Cal.get(Calendar.DAY_OF_MONTH));
  }
}

The output of the above code will be:

The Calendar is: Wed Sep 09 16:51:27 UTC 2020
Calendar Year is: 2020
Calendar Day is: 9

❮ Java.util - Calendar