Java中的 DateFormatSymbols setShortMonths() 方法及示例
Java中DateFormatSymbols 类的setShortMonths(String[] newShMonth )方法用于将字符串格式的日历月份的短名称设置为不同的字符串。例如,“Jan”可以更改为“FEB”,“JUN”可以更改为“GEEK”等。
句法:
public void setShortMonths(String[] newShMonth)
参数:该方法采用一个参数newShMonth ,它是一个String类型的数组,引用了现有Months中要替换的新字符串。
返回值:该方法以字符串格式返回修改后的月份名称。
下面的程序说明了 setShortMonths() 方法的使用。
示例 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] + " ");
}
}
}
输出:
Original:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
Modified:
GEEK
FOR
GEEK
DEC
NOV
JAN
FEB
示例 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] + " ");
}
}
}
输出:
Original:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
Modified:
123
456
JAN
FEB
NOV
Dec
May
参考: https: Java/text/DateFormatSymbols.html#setShortMonths-java.lang.String:A-