How do I 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 org.kodejava.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 (String month : months) {
            System.out.println("month = " + month);
        }

        String[] shortMonths = new DateFormatSymbols().getShortMonths();
        for (String shortMonth : shortMonths) {
            System.out.println("shortMonth = " + shortMonth);
        }

        DateFormatSymbols dfs = new DateFormatSymbols(Locale.GERMANY);
        String[] germanyMonths = dfs.getMonths();
        for (String germanyMonth : germanyMonths) {
            System.out.println("germanyMonth = " + germanyMonth);
        }

        String[] germanyShortMonths = dfs.getShortMonths();
        for (String germanyShortMonth : germanyShortMonths) {
            System.out.println("germanyShortMonth = "
                    + germanyShortMonth);
        }
    }
}

The results 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 = Mär
germanyShortMonth = Apr
germanyShortMonth = Mai
germanyShortMonth = Jun
germanyShortMonth = Jul
germanyShortMonth = Aug
germanyShortMonth = Sep
germanyShortMonth = Okt
germanyShortMonth = Nov
germanyShortMonth = Dez
germanyShortMonth = 

How do I get the current month name?

To get the current month name from the system we can use java.util.Calendar class. The Calendar.get(Calendar.MONTH) returns the month value as an integer starting from 0 as the first month and 11 as the last month. This mean January equals to 0, February equals to 1 and December equals to 11.

Let’s see the code below:

package org.kodejava.example.util;

import java.util.Calendar;

public class GetMonthNameExample {
    public static void main(String[] args) {
        String[] monthName = {"January", "February",
                "March", "April", "May", "June", "July",
                "August", "September", "October", "November",
                "December"};

        Calendar cal = Calendar.getInstance();
        String month = monthName[cal.get(Calendar.MONTH)];

        System.out.println("Month name: " + month);
    }
}

On the first line inside the main method we declare an array of string that keep our month names. Next we get the integer value of the current month and at the last step we look for the month name inside our previously defined array.

The example result of this program is:

Month name: January

The better way to get the month names or week names is to use the java.text.DateFormatSymbols class. The example on using this class can be found on the following links: How do I get a list of month names? and How do I get a list of weekday names?.