Get/Find All Months Using Java API

In this example we are showing How To Display all Months Using Java API

DateFormatSymbols is a public class for encapsulating localizable date-time formatting data, such as the names of the months, the names of the days of the week, and the time zone data. DateFormat and SimpleDateFormat both use DateFormatSymbols to encapsulate this information.

Reference -> http://docs.oracle.com/javase/7/docs/api/java/text/DateFormatSymbols.html

Following are the important methods avaialbe on DateFormatSymbols class

Note

The method DateFormatSymbols.getMonths() returns the array of all months. We can iterate this array and able to view the different months.

The method DateFormatSymbols.getShortMonths() returns the array of all months in short codes. We can iterate this array and able to view the different shortmonths

The method DateFormatSymbols(Locale.FRENCH).getMonths() returns the array of all months in FRENCH Locale. We can iterate this array and able to view the different months in appropriate Locale

The method DateFormatSymbols(Locale.FRENCH).getShortMonths() returns the array of all short months in FRENCH Locale. We can iterate this array and able to view the different shortmonths in FRENCH Locale

Get/Find all Months Using Java API

package com.test;

import java.text.DateFormatSymbols;
import java.util.Arrays;
import java.util.Locale;

public class FindAllMonthsInJava {

   
/**
     * Get All Months / Short Months / Locale Months In Java
     */
   
public static void main(String[] args) {
       
//Get List of Month Names
       
System.out.println("All Months: " +  Arrays.toString(new DateFormatSymbols().getMonths()));
       
//Get List of Short Month Name
       
System.out.println("Short Months: " +  Arrays.toString(new DateFormatSymbols().getShortMonths()));
       
//Get List of Month Names Using Locale
       
System.out.println("All Months In French: " +  Arrays.toString(new DateFormatSymbols(Locale.FRENCH).getMonths()));
       
//Get List of Short Month Name Using Locale
       
System.out.println("Short Months In French: " +  Arrays.toString(new DateFormatSymbols(Locale.FRENCH).getShortMonths()));
   
}
}
Output
All Months: [January, February, March, April, May, June, July, August, September, October, November, December, ]
Short Months: [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, ]
All Months In French: [janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre, ]
Short Months In French: [janv., févr., mars, avr., mai, juin, juil., août, sept., oct., nov., déc., ]
Note

From the above output, you can see that, Each array contains 13 strings, this is becauase some calendar systems [ chinese & lunar calendars etc ] have 13 months avaiable, that's why last array element is blank in the output

Get/Find Current Month Using Java

package com.test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class GetCurrentMonthInJava {

   
/**
     * Get / Find Current Month In Java
     */
   
public static void main(String[] args) {
       
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM")
        System.out.println
("Current Month: "+ dateFormat.format(new Date()))
   
}
}
Output
Current Month: May

 






Your email address will not be published. Required fields are marked *