VOOZH about

URL: https://www.geeksforgeeks.org/java/calendar-get-method-in-java/

⇱ Calendar.get() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Calendar.get() Method in Java

Last Updated : 7 May, 2025

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


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


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)

Comment
Article Tags: