How do I change the date format symbols for a specified locale?

package org.kodejava.text;

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

public class ChangeDateFormatSymbols {
    public static void main(String[] args) {
        Locale id = new Locale("in", "ID");
        String pattern = "EEEE, dd MMM yyyy";
        Date today = new Date();

        // Gets formatted date specify by the given pattern for
        // Indonesian Locale no changes for default date format
        // is applied here.
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, id);
        String before = sdf.format(today);
        System.out.println("Before format change: " + before);

        // Create a DateFormatSymbols object for Indonesian locale.
        DateFormatSymbols dfs = new DateFormatSymbols(id);

        // Gets String array of default format of weekdays.
        String[] days = dfs.getWeekdays();
        String[] newDays = new String[days.length];
        for (int i = 0; i < days.length; i++) {
            // For each day, apply toUpperCase() method to
            // capitalized it.
            newDays[i] = days[i].toUpperCase();
        }

        // Set String array of weekdays.
        dfs.setWeekdays(newDays);

        // Gets String array of default format of short months.
        String[] shortMonths = dfs.getShortMonths();
        String[] months = new String[shortMonths.length];
        for (int j = 0; j < shortMonths.length; j++) {
            // For each short month, apply toUpperCase() method
            // to capitalized it.
            months[j] = shortMonths[j].toUpperCase();
        }

        // Set String array of short months.
        dfs.setShortMonths(months);

        // Create a SimpleDateFormat object by given pattern and 
        // symbol and then format the date object as String.
        sdf = new SimpleDateFormat(pattern, dfs);
        String after = sdf.format(today);
        System.out.println("After change format : " + after);
    }
}

Here are the output of our program:

Before format change: Selasa, 19 Okt 2021
After change format : SELASA, 19 OKT 2021

How do I change date formatting symbols?

This example shows how we can change the date formatting symbols. In this example we change the month names and short month names and also the weekday names and short weekday names.

Other than these two items we can also change other symbols such as the Era name and AM-PM string.

package org.kodejava.text;

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

public class DateFormatSymbolsExample {
    public static void main(String[] args) {
        // Defining a new Date Format Symbols, the following are month and day
        // names in Bahasa Indonesia.
        String[] newMonths = {"JANUARI", "FEBRUARI", "MARET", "APRIL", "MEI",
                "JUNI", "JULI", "AGUSTUS", "SEPTEMBER", "OKTOBER", "NOVEMBER",
                "DESEMBER"};
        String[] newShortMonths = {"JAN", "FEB", "MAR", "APR", "MEI", "JUN",
                "JUL", "AGU", "SEP", "OKT", "NOV", "DES"};
        String[] newWeekdays = {"", "MINGGU", "SENIN", "SELASA", "RABU", "KAMIS",
                "JUMAT", "SABTU"};
        String[] shortWeekdays = {"", "MIN", "SEN", "SEL", "RAB", "KAM", "JUM",
                "SAB"};

        DateFormatSymbols symbols = new DateFormatSymbols();
        symbols.setMonths(newMonths);
        symbols.setShortMonths(newShortMonths);
        symbols.setWeekdays(newWeekdays);
        symbols.setShortWeekdays(shortWeekdays);

        DateFormat format = new SimpleDateFormat("dd MMMM yyyy", symbols);
        System.out.println(format.format(new Date()));

        format = new SimpleDateFormat("dd MMM yyyy", symbols);
        System.out.println(format.format(new Date()));

        format = new SimpleDateFormat("EEEE, dd MMM yyyy", symbols);
        System.out.println(format.format(new Date()));

        format = new SimpleDateFormat("E, dd MMM yyyy", symbols);
        System.out.println(format.format(new Date()));
    }
}

The result of the code snippet above is:

02 OKTOBER 2021
02 OKT 2021
SABTU, 02 OKT 2021
SAB, 02 OKT 2021

How do I get a list of weekday names?

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

package org.kodejava.text;

import java.text.DateFormatSymbols;

public class WeekdayNames {
    public static void main(String[] args) {
        DateFormatSymbols dfs = new DateFormatSymbols();

        String[] weekdays = dfs.getWeekdays();
        for (String weekday : weekdays) {
            System.out.println("weekday = " + weekday);
        }

        String[] shortWeekdays = dfs.getShortWeekdays();
        for (String shortWeekday : shortWeekdays) {
            System.out.println("shortWeekday = " + shortWeekday);
        }
    }
}

The results of the code above are:

weekday = 
weekday = Sunday
weekday = Monday
weekday = Tuesday
weekday = Wednesday
weekday = Thursday
weekday = Friday
weekday = Saturday
shortWeekday = 
shortWeekday = Sun
shortWeekday = Mon
shortWeekday = Tue
shortWeekday = Wed
shortWeekday = Thu
shortWeekday = Fri
shortWeekday = Sat

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.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 = December
month = 
shortMonth = Jan
shortMonth = Feb
...
shortMonth = Dec
shortMonth = 
germanyMonth = Januar
germanyMonth = Februar
...
germanyMonth = Dezember
germanyMonth = 
germanyShortMonth = Jan
germanyShortMonth = Feb
...
germanyShortMonth = Dez
germanyShortMonth =