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:
import java.text.DateFormatSymbols;
import java.util.Locale;
public class DateFormat_Main {
public static void main(String args[])
{
DateFormatSymbols format
= new DateFormatSymbols(
new Locale( "en" , "US" ));
String[] Days = format.getMonths();
System.out.println( "Original: " );
for ( int i = 0 ; i < Days.length; i++) {
System.out.println(Days[i] + " " );
}
String[] modDays = { "GEEK" , "FOR" ,
"GEEK" , "DECEMBER" ,
"NOVEMBER" , "JAN" ,
"FEB" };
format.setMonths(modDays);
String[] modifiedDays
= format.getMonths();
System.out.println( "Modified: " );
for ( int i = 0 ; i < modifiedDays.length; i++) {
System.out.println(modifiedDays[i] + " " );
}
}
}
|
Output:
Original:
January
February
March
April
May
June
July
August
September
October
November
December
Modified:
GEEK
FOR
GEEK
DECEMBER
NOVEMBER
JAN
FEB
Example 2:
import java.text.DateFormatSymbols;
import java.util.Locale;
public class DateFormat_Main {
public static void main(String args[])
{
DateFormatSymbols format
= new DateFormatSymbols(
new Locale( "en" , "US" ));
String[] Days = format.getMonths();
System.out.println( "Original: " );
for ( int i = 0 ; i < Days.length; i++) {
System.out.println(Days[i] + " " );
}
String[] modDays = { "123" , "456" ,
"JAN" , "FEB" ,
"NOV" , "Dec" ,
"May" };
format.setMonths(modDays);
String[] modifiedDays
= format.getMonths();
System.out.println( "Modified: " );
for ( int i = 0 ; i < modifiedDays.length; i++) {
System.out.println(modifiedDays[i] + " " );
}
}
}
|
Output:
Original:
January
February
March
April
May
June
July
August
September
October
November
December
Modified:
123
456
JAN
FEB
NOV
Dec
May
Reference: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#setMonths-java.lang.String:A-
Last Updated :
03 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...