Java .internalGet()
The .internalGet() method is a protected method used to retrieve the value of a field from a Calendar instance. In other words, it takes a field as input and returns its corresponding value. This method is generally used within the Calendar class or its sub classes.
Syntax
myCalendar.internalGet(int field)
The .internalGet() method accepts a field constant from the Calendar class (e.g. YEAR) as its argument and returns the corresponding value.
Example
In this example, the InternalGetExample class inherits the GregorianCalendar. The code first instantiates a new object of type InternalGetExample with the current date which uses the Gregorian calendar system.
Note: There are multiple calendar systems that
Calendar.getInstance()provides depending on the locale. These includeGregorianCalendar,BuddhistCalendar, andJapaneseImperialCalendar.
import java.util.GregorianCalendar;public class InternalGetExample extends GregorianCalendar{public static void main(String[] args) {// Create new instance with current date and timeInternalGetExample myCalendar = new InternalGetExample();// Get the current year, and monthint year = myCalendar.internalGet(YEAR);int month = myCalendar.internalGet(MONTH);System.out.println("Current Year: " + year);System.out.println("Current Month: " + month);}}
This output will vary depending on the current date, it will generally look like the following:
Current Year: 2023Current Month: 9
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours