Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

DateFormatSymbols getMonths() Method in Java with Examples

  • Last Updated : 30 May, 2019

The getMonths() Method of DateFormatSymbols class in Java is used to get the name of the months of the calendar in a string format.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public String[] getMonths()

Parameters: The method does not take any parameters.

Return Values: The method returns the name of the months in a string format.



Below programs illustrate the use of getMonths() method.

Example 1:




// Java code to demonstrate getMonths()
  
import java.text.DateFormatSymbols;
  
public class DateFormat_Main {
    public static void main(String args[])
    {
        int i;
  
        // Initializing DateFormatSymbols
        String months[]
            = new DateFormatSymbols().getMonths();
  
        for (i = 0; i < months.length - 1; i++) {
  
            // Displaying the months
            System.out.println("Month = "
                               + months[i]);
        }
    }
}
Output:
Month = January
Month = February
Month = March
Month = April
Month = May
Month = June
Month = July
Month = August
Month = September
Month = October
Month = November
Month = December

Example 2:




// Java code to demonstrate getMonths()
  
import java.text.DateFormatSymbols;
  
public class DateFormat_Main {
    public static void main(String args[])
    {
  
        int i;
  
        // Initializing DateFormatSymbols
        String months[]
            = new DateFormatSymbols().getMonths();
  
        for (i = 0; i < months.length / 2; i++) {
  
            // Displaying the months
            System.out.println("Month " + i
                               + " = " + months[i]);
        }
    }
}
Output:
Month 0 = January
Month 1 = February
Month 2 = March
Month 3 = April
Month 4 = May
Month 5 = June

Reference: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#getMonths–




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!