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 = JanMonth = FebMonth = MarMonth = AprMonth = MayMonth = JunMonth = JulMonth = AugMonth = SepMonth = OctMonth = NovMonth = 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 = JanMonth 1 = FebMonth 2 = MarMonth 3 = AprMonth 4 = MayMonth 5 = Jun
参考资料: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#getShortMonths-
