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
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.