Java DateFormatSymbols setWeekdays()方法及实例

Java DateFormatSymbols setWeekdays()方法及实例

Java中 DateFormatSymbols类setWeekdays(String[] newWeekds ) 方法是用来将字符串格式的日历上的工作日名称设置为一些不同的字符串。例如,”星期日 “可以改为 “星期五”,”星期一 “可以改为 “星期三 “或其他一些随机字符串。

语法

public void setWeekdays(String[] _newWeekds_ )

参数: 该方法需要一个参数 _ newWeekds_ ,它是一个String类型的数组,指的是要在现有工作日中替换的新字符串。

返回值: 该方法以字符串格式返回 修改后的工作日名称

以下程序说明了setWeekdays()方法的使用。

示例1

// 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] + "  ");
        }
    }
}

输出

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

例2 :

// 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://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#setWeekdays-java.lang.String:A-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程