java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to Get a List of Month Names?

The example code below helps you to get all month names as an array of String. The first method, getMonths() return the full name string while the second method getShortMonths() return the short name of the month.

package com.javacoderanch.example.text;

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

public class MonthNames {
    public static void main(String[] args) {
        String[] months = new DateFormatSymbols().getMonths();
        for (int i = 0; i < months.length; i++) {
            String month = months[i];
            System.out.println("month = " + month);
        }

        String[] shortMonths = new DateFormatSymbols().getShortMonths();
        for (int i = 0; i < shortMonths.length; i++) {
            String shortMonth = shortMonths[i];
            System.out.println("shortMonth = " + shortMonth);
        }

        String[] germanyMonths = new DateFormatSymbols(Locale.GERMANY).getMonths();
        for (int i = 0; i < germanyMonths.length; i++) {
            String germanyMonth = germanyMonths[i];
            System.out.println("germanyMonth = " + germanyMonth);
        }

        String[] germanyShortMonths = new DateFormatSymbols(Locale.GERMANY).getShortMonths();
        for (int i = 0; i < germanyShortMonths.length; i++) {
            String germanyShortMonth = germanyShortMonths[i];
            System.out.println("germanyShortMonth = " + germanyShortMonth);
        }
    }
}

The result of the code above are:

month = January
month = February
month = March
month = April
month = May
month = June
month = July
month = August
month = September
month = October
month = November
month = December
month = 
shortMonth = Jan
shortMonth = Feb
shortMonth = Mar
shortMonth = Apr
shortMonth = May
shortMonth = Jun
shortMonth = Jul
shortMonth = Aug
shortMonth = Sep
shortMonth = Oct
shortMonth = Nov
shortMonth = Dec
shortMonth = 
germanyMonth = Januar
germanyMonth = Februar
germanyMonth = März
germanyMonth = April
germanyMonth = Mai
germanyMonth = Juni
germanyMonth = Juli
germanyMonth = August
germanyMonth = September
germanyMonth = Oktober
germanyMonth = November
germanyMonth = Dezember
germanyMonth = 
germanyShortMonth = Jan
germanyShortMonth = Feb
germanyShortMonth = Mrz
germanyShortMonth = Apr
germanyShortMonth = Mai
germanyShortMonth = Jun
germanyShortMonth = Jul
germanyShortMonth = Aug
germanyShortMonth = Sep
germanyShortMonth = Okt
germanyShortMonth = Nov
germanyShortMonth = Dez
germanyShortMonth =