Thursday, September 4, 2025
HomeLanguagesJavaCalendar get() method in Java with Examples

Calendar get() method in Java with Examples

The get(int field_value) method of Calendar class is used to return the value of the given calendar field in the parameter.

Syntax:

public int get(int field)

Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose value is needed to be returned.

Return Value: The method returns the value of the passed field.

Below programs illustrate the working of get() Method of Calendar class:
Example 1:




// Java Code to illustrate
// get() Method
  
import java.util.*;
  
public class CalendarClassDemo
    extends GregorianCalendar {
    public static void main(String args[])
    {
        // Creating a calendar
        Calendar calndr = Calendar.getInstance();
  
        // Getting the value of all
        // the calendar date fields
        System.out.println("The given calendar's"
                           + " year is: " + calndr.get(Calendar.YEAR));
        System.out.println("The given calendar's"
                           + " month is: " + calndr.get(Calendar.MONTH));
        System.out.println("The given calendar's"
                           + " day is: " + calndr.get(Calendar.DATE));
    }
}


Output:

The given calendar's year is: 2019
The given calendar's month is: 1
The given calendar's day is: 13

Example 2:




// Java Code to illustrate
// get() Method
  
import java.util.*;
  
public class CalendarClassDemo
    extends GregorianCalendar {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr = new GregorianCalendar(2018, 9, 2);
  
        // Getting the value of all
        // the calendar date fields
        System.out.println("The given calendar's"
                           + " year is: " + calndr.get(Calendar.YEAR));
        System.out.println("The given calendar's"
                           + " month is: " + calndr.get(Calendar.MONTH));
        System.out.println("The given calendar's"
                           + " day is: " + calndr.get(Calendar.DATE));
    }
}


Output:

The given calendar's year is: 2019
The given calendar's month is: 9
The given calendar's day is: 2

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#get(int)

RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS