Java Calendar get()
May 16, 2022
On this page we will learn using calendar get
method.
Find the method declaration of calendar get
from Java doc.
int get(int field)
1. Parameters
field : the given calendar field such as WEEK_OF_MONTH, DAY_OF_MONTH etc.
2. Returns
Returns the value for the given calendar field.
3. Throws
If the field value is out of the range (field < 0 || field >= FIELD_COUNT), it throws
ArrayIndexOutOfBoundsException
exception.
Now find the examples with different calendar fields.
1. YEAR
TheYEAR
indicates the year.
Calendar c = Calendar.getInstance(); System.out.println(c.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = c.get(Calendar.YEAR); System.out.println(calVal); // 2022
2. MONTH
TheMONTH
indicates the month.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = calendar.get(Calendar.MONTH); System.out.println(calVal); // 4
3. WEEK_OF_MONTH
TheWEEK_OF_MONTH
indicates the week number within the current month.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = calendar.get(Calendar.WEEK_OF_MONTH); System.out.println(calVal); // 3
4. WEEK_OF_YEAR
TheWEEK_OF_YEAR
indicates the week number within the current year.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = calendar.get(Calendar.WEEK_OF_YEAR); System.out.println(calVal); // 21
5. DAY_OF_MONTH
TheDAY_OF_MONTH
indicates the day of the month. The DATE
field is the synonym for DAY_OF_MONTH
field.
Calendar c = Calendar.getInstance(); System.out.println(c.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = c.get(Calendar.DAY_OF_MONTH); System.out.println(calVal); // 16
6. DAY_OF_WEEK
TheDAY_OF_WEEK
indicates the day of week.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = calendar.get(Calendar.DAY_OF_WEEK); System.out.println(calVal); // 2
7. DAY_OF_WEEK_IN_MONTH
TheDAY_OF_WEEK_IN_MONTH
indicates the ordinal number of the day of the week within the current month.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH); System.out.println(calVal); // 3
8. DAY_OF_YEAR
TheDAY_OF_YEAR
indicates the day number within the current year.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 12:22:53 IST 2022 int calVal = calendar.get(Calendar.DAY_OF_YEAR); System.out.println(calVal); // 136
9. HOUR
TheHOUR
indicates the hour of the morning or afternoon.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 13:53:04 IST 2022 int calVal = calendar.get(Calendar.HOUR); System.out.println(calVal); // 1
10. HOUR_OF_DAY
The HOUR_OF_DAY
indicates the hour of the day.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 13:53:04 IST 2022 int calVal = calendar.get(Calendar.HOUR_OF_DAY); System.out.println(calVal); // 13
11. MINUTE
TheMINUTE
indicates minute within the hour.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 14:08:27 IST 2022 int calVal = calendar.get(Calendar.MINUTE); System.out.println(calVal); // 8
12. MILLISECOND
TheMILLISECOND
indicates the millisecond within the second.
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); // Mon May 16 14:16:47 IST 2022 int calVal = calendar.get(Calendar.MILLISECOND); System.out.println(calVal); // 821