DateFormatSymbols setMonths() Method in Java with Examples
The setMonths(String[] newMonth) Method of DateFormatSymbols class in Java is used to set the names of the months of the calendar in string format into some different strings. For eg., “January” can be changed to “FEBRUARY”, “JUNE” can be changed to “GEEK” etc.
Syntax:
public void setMonths(String[] newMonth)
Parameters: The method takes one parameter newMonth 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 setMonths() method.
Example 1:
// Java code to demonstrate setMonths() 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.getMonths(); // 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" , "DECEMBER" , "NOVEMBER" , "JAN" , "FEB" }; // Setting the default into modified format.setMonths(modDays); // Displaying the modified string String[] modifiedDays = format.getMonths(); System.out.println( "Modified: " ); for ( int i = 0 ; i < modifiedDays.length; i++) { System.out.println(modifiedDays[i] + " " ); } } } |
Original: January February March April May June July August September October November December Modified: GEEK FOR GEEK DECEMBER NOVEMBER JAN FEB
Example 2:
// Java code to demonstrate setMonths() 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.getMonths(); // 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.setMonths(modDays); // Displaying the modified string String[] modifiedDays = format.getMonths(); System.out.println( "Modified: " ); for ( int i = 0 ; i < modifiedDays.length; i++) { System.out.println(modifiedDays[i] + " " ); } } } |
Original: January February March April May June July August September October November December Modified: 123 456 JAN FEB NOV Dec May
Recommended Posts:
- DateFormatSymbols getEras() Method in Java with Examples
- DateFormatSymbols getWeekdays() Method in Java with Examples
- DateFormatSymbols getMonths() Method in Java with Examples
- DateFormatSymbols getShortWeekdays() Method in Java with Examples
- DateFormatSymbols setShortWeekdays() Method in Java with Examples
- DateFormatSymbols getShortMonths() Method in Java with Examples
- DateFormatSymbols setWeekdays() Method in Java with Examples
- DateFormatSymbols getAmPmStrings() Method in Java with Examples
- DateFormatSymbols setShortMonths() Method in Java with Examples
- DateFormatSymbols getAvailableLocales() Method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.