Open In App

Calendar.get() Method in Java

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the Calendar class represents a specific moment in time and provides methods to extract individual date and time fields. One most commonly used method of the Calendar class is get(int field) method. This method returns the value of a given calendar field, such as year, month, or day.

In this article, we are going to learn about the Calendar.get() method in Java.

Syntax of Calendar.get() Method

public int get(int field)

  • Parameter: field: This field is one of the constants defined in java.util.Calendar. For example, Calendar.YEAR, Calendar.MONTH, Calendar.DATE.
  • Return Value: It returns the value of the requested field. For example, 2025 for Calendar.YEAR.

Important Points:

  • Calendar.MONTH returns values from 0 to 11 means January to December.
  • To display a human-readable month number, add 1 to the returned value.
  • Calendar.DATE and Calendar.DAY_OF_MONTH both are the same.

Examples of Java Calendar.get() Method

Example 1: Current Date Values

Java
// Java program to show current date using Calendar.get()
import java.util.Calendar;

public class Geeks {
    public static void main(String[] args) {
        
        // Get an instance set to the current date and time
        Calendar cal = Calendar.getInstance();

        int year  = cal.get(Calendar.YEAR);
        
        // add 1 for human-readable month
        int month = cal.get(Calendar.MONTH) + 1;  
        int day   = cal.get(Calendar.DATE);

        System.out.println("Current Date: " + year
            + "-" + month
            + "-" + day);
    }
}

Output
Current Date: 2025-5-7

Explanation:

  • get(Calendar.YEAR) returns the 4-digit year.
  • get(Calendar.MONTH) returns 4 for May, so we add 1.
  • get(Calendar.DATE) returns the day of the month.

Example 2: Specific Date

Java
// Java program to set and show a 
// specific date using Calendar.get()
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Geeks {
    public static void main(String[] args) {
        
        // Create a Calendar for July 21, 2023
        Calendar cal = new GregorianCalendar(2023, 6, 21);

        int year  = cal.get(Calendar.YEAR);
        
        // July is 6 internally
        int month = cal.get(Calendar.MONTH) + 1;  
        int day   = cal.get(Calendar.DATE);

        System.out.println("Set Date: " + year
            + "-" + month
            + "-" + day);
    }
}

Output
Set Date: 2023-7-21


Common Fields

Field Constant

Description

Usage

Calendar.YEAR

Year, for example, 2025

Retrievs the year

Calendar.MONTH

Month (0-11)

Retrievs the month

Calendar.DATE

Day of month (1-31)

Retrievs the day of month

Calendar.HOUR_OF_DAY

Hour in 24 hour format

Retrievs the hour

Calendar.MINUTE

Minute (0-59)

Retrievs the minute

Calendar.SECOND

Second (0-59)

Retrievs the second

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


Next Article
Practice Tags :

Similar Reads