Java – How to get a list of Month Names with Locale
In this example we obtain a list of month names as an array of String using DateFormatSymbols
.
List of Month Names with Locale
The first method getMonths()
returns the full name of the month. The second getShortMonths()
returns the short name of the month.
When you need to display the months in a different Locale
you can simply create a DateFormatSymbols
by passing the Locale
as the first constructor argument.
package com.memorynotfound.text;
import java.text.DateFormatSymbols;
import java.util.Arrays;
import java.util.Locale;
public class DisplayMonthNames {
// DateFormatSymbols in default Locale
private static final DateFormatSymbols dfs = new DateFormatSymbols();
// DateFormatSymbols in pre defined Locale
private static final DateFormatSymbols french_dfs = new DateFormatSymbols(Locale.FRENCH);
public static void main(String... args){
String[] months = dfs.getMonths();
System.out.printf("List of months: ");
System.out.println(Arrays.toString(months));
String[] shortMonths = dfs.getShortMonths();
System.out.printf("List of short months: ");
System.out.println(Arrays.toString(shortMonths));
String[] frenchMonths = french_dfs.getMonths();
System.out.printf("List of french months: ");
System.out.println(Arrays.toString(frenchMonths));
String[] frenchShortMonths = french_dfs.getShortMonths();
System.out.printf("List of french short months: ");
System.out.println(Arrays.toString(frenchShortMonths));
}
}
Output:
List of months: [January, February, March, April, May, June, July, August, September, October, November, December, ]
List of short months: [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, ]
List of french months: [janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre, ]
List of french short months: [janv., févr., mars, avr., mai, juin, juil., août, sept., oct., nov., déc., ]
References
- DateFormatSymbols JavaDoc
- DateFormatSymbols.getMonths() JavaDoc
- DateFormatSymbols.getShortMonths() JavaDoc