get(int): This method is available in java.util.GregorianCalendar 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: One parameter is required for this method.
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.GregorianCalendar;public class GregorianCalendarget {public static void main(String[] args) {GregorianCalendar cal = new GregorianCalendar();int field = 0;System.out.println(cal.get(field));}}
Output:
1
Approach 2: ArrayIndexOutOfBoundsException
Java
import java.util.GregorianCalendar;public class GregorianCalendarget {public static void main(String[] args) {GregorianCalendar cal = new GregorianCalendar();int field = -1;System.out.println(cal.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