.get()
The .get()
method of the Calendar
class is used to return the value of the given calendar field.
Syntax
The get(int field)
is used to retrieve the value of a specific date or time field, indicated by the provided field argument.
public int get(int field)
Example 1
This example demonstrates a basic implementation of the .get
function. The code acquires the present year, month, and day of the month from a Calendar
instance, adjusting the month representation to account for the 0-based index.
import java.util.Calendar;public class CalendarExample {public static void main(String[] args) {// Create a Calendar instanceCalendar calendar = Calendar.getInstance();// Get the year, month, and day of the monthint year = calendar.get(Calendar.YEAR);// Month values are 0-based (0 = January, 11 = December)int month = calendar.get(Calendar.MONTH);int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);// Display the retrieved valuesSystem.out.println("Current Year: " + year);// Adding 1 to adjust for 0-based monthsSystem.out.println("Current Month: " + (month + 1));System.out.println("Day of the Month: " + dayOfMonth);}}
The output of the provided example would vary based on the current date when the code is executed. Here’s a general idea of what the output might look like:
Current Year: 2023Current Month: 8Day of the Month: 9
Example 2
This example employs the Calendar
class to set a fixed date (July 16, 2022) within a Calendar
instance. It retrieves the year, month, and day of the month from the configured instance and displays them in a formatted output on the console.
import java.util.Calendar;public class FixedDateCalendarExample {public static void main(String[] args) {// Create a Calendar instance and set it to a fixed dateCalendar calendar = Calendar.getInstance();calendar.set(2022, Calendar.JULY, 16);// Get the year, month, and day of the monthint year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);// Display the retrieved valuesSystem.out.println("Fixed Date: " + year + "-" + (month + 1) + "-" + dayOfMonth);}}
This will output the following:
Fixed Date: 2022-7-16
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly16 hours