Calendar.get() Method in Java

java.util.Calendar.get() method is a method of java.util.Calendar class. The Calendar class provides some methods for implementing a concrete calendar system outside the package. Some examples of Calendar fields are : YEAR, DATE, MONTH, DAY_OF_WEEK, DAY_OF_YEAR, WEEK_OF_YEAR, MINUTE, SECOND, HOUR, AM_PM, WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, HOUR_OF_DAY.

Syntax :

public int get(int field)

where, field represents the given calendar
field and the function returns the value of
given field.

Exception : If the specified field is out of range, then ArrayIndexOutOfBoundsException is thrown.



Applications :
Example 1: To fetch Date, Month, Year

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to implement calendar.get() function
import java.util.*;
  
class GFG {
      
// Driver code
public static void main(String[] args) {
  
    // creating a calendar
    Calendar c = Calendar.getInstance();
  
    // get the value of DATE field
    System.out.println("Day : " +
                        c.get(Calendar.DATE));
      
    // get the value of MONTH field
    System.out.println("Month : " +
                        c.get(Calendar.MONTH));
                      
    // get the value of YEAR field
    System.out.println("Year : "
                        c.get(Calendar.YEAR));
}
}

chevron_right


Output :

Day : 1
Month : 2
Year : 2018

Example 2 : To fetch Day of the week, Day of the year, Week of the month, Week of the year.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code of calendar.get() function
import java.util.*;
  
class GFG {
      
// Driver code
public static void main(String[] args) {
  
    // creating a calendar
    Calendar c = Calendar.getInstance();
  
    // get the value of DATE_OF_WEEK field
    System.out.println("Day of week : "
                        c.get(Calendar.DAY_OF_WEEK));
                          
    // get the value of DAY_OF_YEAR field
    System.out.println("Day of year : " +
                        c.get(Calendar.DAY_OF_YEAR));
                          
    // get the value of WEEK_OF_MONTH field
    System.out.println("Week in Month : "
                        c.get(Calendar.WEEK_OF_MONTH));
                          
    // get the value of WEEK_OF_YEAR field
    System.out.println("Week in Year : "
                        c.get(Calendar.WEEK_OF_YEAR));
  
                      
    // get the value of DAY_OF_WEEK_IN_MONTH field
    System.out.println("Day of Week in Month : " +
                        c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
}
}

chevron_right


Output :

Day of week : 5
Day of year : 60
Week in Month : 1
Week in Year : 9
Day of Week in Month : 1

Example 3 : To fetch Hour, Minute, Second and AM_PM.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of calendar.get()
// function in Java
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a calendar
        Calendar c = Calendar.getInstance();
  
        // get the value of HOUR field
        System.out.println("Hour : " + c.get(Calendar.HOUR));
  
        // get the value of MINUTE field
        System.out.println("Minute : " + c.get(Calendar.MINUTE));
  
        // get the value of SECOND field
        System.out.println("Second : " + c.get(Calendar.SECOND));
  
        // get the value of AM_PM field
        System.out.println("AM or PM : " + c.get(Calendar.AM_PM));
  
        // get the value of HOUR_OF_DAY field
        System.out.println("Hour (24-hour clock) : " + c.get(Calendar.HOUR_OF_DAY));
    }
}

chevron_right


Output :

Hour : 6
Minute : 51
Second : 53
AM or PM : 0
Hour (24-hour clock) : 6


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.