Java DateFormatSymbols setMonths()方法及实例
Java中 DateFormatSymbols类 的 setMonths(String[] newMonth ) 方法是用来将日历中的月份名称以字符串格式设置为一些不同的字符串。例如,”January “可以改成 “FEBRUARY”,”June “可以改成 “GEEK “等等。
语法
public void setMonths(String[] newMonth)
参数: 该方法需要一个参数 newMonth ,它是一个字符串类型的数组,指的是要在现有月份中替换的新字符串。
返回值: 该方法以字符串格式返回修改后的月份名称。
下面的程序说明了setMonths()方法的使用。
示例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
例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
参考资料: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#setMonths-java.lang.String:A-
