get(int): This method is available in java.util.Calendar class of Java.
Syntax:
int java.util.Calendar.get(int field)
This method takes one argument of type int as its parameter. This method returns the value of the given calendar field.
Parameters: field the given calendar field.
Returns: the value for the given calendar field.
Throws:
ArrayIndexOutOfBoundsException - if the specified field is out of range(field < 0 || field >= FIELD_COUNT).
Approach 1: When no exception
Java
import java.util.Calendar;public class Calendarget {public static void main(String[] args) {// create a calendarCalendar calendar = Calendar.getInstance();int field = 1;System.out.println(calendar.get(field));}}
Output:
2022
Approach 2: ArrayIndexOutOfBoundsException
Java
import java.util.Calendar;public class Calendarget {public static void main(String[] args) {// create a calendarCalendar calendar = Calendar.getInstance();int field = -1;System.out.println(calendar.get(field));}}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 17 at java.base/java.util.Calendar.internalGet(Calendar.java:1871) at java.base/java.util.Calendar.get(Calendar.java:1858)
No comments:
Post a Comment