Java中的 DateFormatSymbols getShortMonths() 方法及示例
Java中DateFormatSymbols 类的getShortMonths()方法用于获取字符串格式的日历月份的短名称。
句法:
public String[] getMonths()
参数:该方法不带任何参数。
返回值:该方法以字符串格式返回月份的短名称。
下面的程序说明了 getShortMonths() 方法的使用。
示例 1:
// Java code to demonstrate getShortMonths()
import java.text.DateFormatSymbols;
public class DateFormat_Main {
public static void main(String args[])
{
int i;
// Initializing DateFormatSymbols
String months[]
= new DateFormatSymbols()
.getShortMonths();
for (i = 0; i < months.length - 1; i++) {
// Displaying the short months
System.out.println(
"Month = " + months[i]);
}
}
}
输出:
Month = Jan
Month = Feb
Month = Mar
Month = Apr
Month = May
Month = Jun
Month = Jul
Month = Aug
Month = Sep
Month = Oct
Month = Nov
Month = Dec
示例 2:
// Java code to demonstrate getShortMonths()
import java.text.DateFormatSymbols;
public class DateFormat_Main {
public static void main(String args[])
{
int i;
// Initializing DateFormatSymbols
String months[]
= new DateFormatSymbols()
.getShortMonths();
for (i = 0; i < months.length / 2; i++) {
// Displaying the short months
System.out.println(
"Month " + i + " = " + months[i]);
}
}
}
输出:
Month 0 = Jan
Month 1 = Feb
Month 2 = Mar
Month 3 = Apr
Month 4 = May
Month 5 = Jun
参考: https: Java/text/DateFormatSymbols.html#getShortMonths–