Saturday, October 18, 2025
HomeLanguagesJavaDateFormatSymbols setShortMonths() Method in Java with Examples

DateFormatSymbols setShortMonths() Method in Java with Examples

The setShortMonths(String[] newShMonth) Method of DateFormatSymbols class in Java is used to set the short names of the months of the calendar in string format into some different strings. For eg., “Jan” can be changed to “FEB”, “JUN” can be changed to “GEEK” etc.

Syntax:

public void setShortMonths(String[] newShMonth)

Parameters: The method takes one parameter newShMonth which is an array of String type and refers to the new strings that are to be replaced in the existing Months.

Return Values: The method returns the modified names of the months in a string format.

Below programs illustrate the use of setShortMonths() method.
Example 1:




// Java code to demonstrate setShortMonths()
  
import java.text.DateFormatSymbols;
import java.util.Locale;
  
public class DateFormat_Main {
    public static void main(String args[])
    {
        // Initialising DateFormatSymbols object
        DateFormatSymbols format
            = new DateFormatSymbols(
                new Locale("en", "US"));
  
        // Taking the default short weekdays
        String[] Days = format.getShortMonths();
  
        // Displaying the original
        System.out.println("Original: ");
  
        for (int i = 0; i < Days.length; i++) {
            System.out.println(Days[i] + "  ");
        }
  
        // Taking an alternative names with
        // additional random strings
        String[] modDays = { "GEEK", "FOR",
                             "GEEK", "DEC",
                             "NOV", "JAN",
                             "FEB" };
  
        // Setting the default into modified
        format.setShortMonths(modDays);
  
        // Displaying the modified string
        String[] modifiedDays
            = format.getShortMonths();
  
        System.out.println("Modified: ");
        for (int i = 0; i < modifiedDays.length; i++) {
            System.out.println(modifiedDays[i] + "  ");
        }
    }
}


Output:

Original: 
Jan  
Feb  
Mar  
Apr  
May  
Jun  
Jul  
Aug  
Sep  
Oct  
Nov  
Dec  
  
Modified: 
GEEK  
FOR  
GEEK  
DEC  
NOV  
JAN  
FEB

Example 2:




// Java code to demonstrate setShortMonths()
  
import java.text.DateFormatSymbols;
import java.util.Locale;
  
public class DateFormat_Main {
    public static void main(String args[])
    {
        // Initialising DateFormatSymbols object
        DateFormatSymbols format
            = new DateFormatSymbols(
                new Locale("en", "US"));
  
        // Taking the default short weekdays
        String[] Days = format.getShortMonths();
  
        // Displaying the original
        System.out.println("Original: ");
        for (int i = 0; i < Days.length; i++) {
            System.out.println(Days[i] + "  ");
        }
  
        // Taking an alternative names with
        // additional random strings
        String[] modDays = { "123", "456",
                             "JAN", "FEB",
                             "NOV", "Dec",
                             "May" };
  
        // Setting the default into modified
        format.setShortMonths(modDays);
  
        // Displaying the modified string
        String[] modifiedDays
            = format.getShortMonths();
  
        System.out.println("Modified: ");
        for (int i = 0; i < modifiedDays.length; i++) {
            System.out.println(modifiedDays[i] + "  ");
        }
    }
}


Output:

Original: 
Jan  
Feb  
Mar  
Apr  
May  
Jun  
Jul  
Aug  
Sep  
Oct  
Nov  
Dec  
  
Modified: 
123  
456  
JAN  
FEB  
NOV  
Dec  
May

Reference: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#setShortMonths-java.lang.String:A-

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS