📌  相关文章
📜  Java中的 DateFormatSymbols setWeekdays() 方法及示例

📅  最后修改于: 2022-05-13 01:55:02.634000             🧑  作者: Mango

Java中的 DateFormatSymbols setWeekdays() 方法及示例

Java中DateFormatSymbols 类setWeekdays(String[] newWeekds )方法用于将字符串格式的日历的工作日名称设置为不同的字符串。例如,“Sunday”可以更改为“FRIDAY”,“Monday”可以更改为“WEDNESDAY”或其他一些随机字符串。

句法:

public void setWeekdays(String[] newWeekds)

参数:该方法采用一个参数newWeekds ,它是一个字符串数组 type 并引用在现有工作日中要替换的新字符串。
返回值:该方法以字符串格式返回修改后的工作日名称
下面的程序说明了 setWeekdays() 方法的使用。
示例 1:

Java
// Java code to demonstrate setWeekdays()
 
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.getWeekdays();
 
        // Displaying the original
        System.out.print("Original: ");
        for (int i = 1; i < Days.length; i++) {
            System.out.print(Days[i] + "  ");
        }
        System.out.println();
 
        // Taking an alternative names with
        // additional random strings
        String[] modDays = { "WEDNESDAY", "THURSDAY",
                             "FRIDAY", "MONDAY",
                             "TUESDAY", "SUNDAY",
                             "SATURDAY" };
 
        // Setting the default into modified
        format.setWeekdays(modDays);
 
        // Displaying the modified string
        String[] modifiedDays = format.getWeekdays();
 
        System.out.print("Modified: ");
        for (int i = 0; i < modifiedDays.length; i++) {
            System.out.print(modifiedDays[i] + "  ");
        }
    }
}


Java
// Java code to demonstrate setShortWeekdays()
 
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.getWeekdays();
 
        // Displaying the original
        System.out.print("Original: ");
        for (int i = 1; i < Days.length; i++) {
            System.out.print(Days[i] + "  ");
        }
        System.out.println();
 
        // Taking an alternative names with
        // additional random strings
        String[] modDays = { "WEEK", "RANDOM",
                             "WEDNESDAY", "THURSDAY",
                             "FRIDAY", "MONDAY",
                             "TUESDAY", "SUNDAY",
                             "SATURDAY" };
 
        // Setting the default into modified
        format.setWeekdays(modDays);
 
        // Displaying the modified string
        String[] modifiedDays = format.getWeekdays();
 
        System.out.print("Modified: ");
        for (int i = 0; i < modifiedDays.length; i++) {
            System.out.print(modifiedDays[i] + "  ");
        }
    }
}


输出:
Original: Sunday  Monday  Tuesday  Wednesday  Thursday  Friday  Saturday  
Modified: WEDNESDAY  THURSDAY  FRIDAY  MONDAY  TUESDAY  SUNDAY  SATURDAY

示例 2:

Java

// Java code to demonstrate setShortWeekdays()
 
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.getWeekdays();
 
        // Displaying the original
        System.out.print("Original: ");
        for (int i = 1; i < Days.length; i++) {
            System.out.print(Days[i] + "  ");
        }
        System.out.println();
 
        // Taking an alternative names with
        // additional random strings
        String[] modDays = { "WEEK", "RANDOM",
                             "WEDNESDAY", "THURSDAY",
                             "FRIDAY", "MONDAY",
                             "TUESDAY", "SUNDAY",
                             "SATURDAY" };
 
        // Setting the default into modified
        format.setWeekdays(modDays);
 
        // Displaying the modified string
        String[] modifiedDays = format.getWeekdays();
 
        System.out.print("Modified: ");
        for (int i = 0; i < modifiedDays.length; i++) {
            System.out.print(modifiedDays[i] + "  ");
        }
    }
}
输出:
Original: Sunday  Monday  Tuesday  Wednesday  Thursday  Friday  Saturday  
Modified: WEEK  RANDOM  WEDNESDAY  THURSDAY  FRIDAY  MONDAY  TUESDAY  SUNDAY  SATURDAY

参考: https: Java/text/DateFormatSymbols.html#setWeekdays-java.lang.String:A-