当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DateFormatSymbols setShortWeekdays()用法及代码示例


Java中的DateFormatSymbols类的setShortWeekdays(String [] newShWeekds)方法用于将日历工作日的短名称(以字符串格式)设置为一些不同的字符串。例如,可以将“Sun”更改为“FRI”,将“Mon”更改为“WED”等。

用法:

public void setShortWeekdays(String[] newShWeekds)

参数:该方法采用一个参数newShWeekds,它是String类型的数组,表示将在现有工作日中替换的新字符串。


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

以下示例程序旨在说明setShortWeekdays()方法的使用。
示例1:

// 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[] shDays 
            = format.getShortWeekdays(); 
  
        // Displaying the original 
        System.out.print("Original: "); 
  
        for (int i = 1; i < shDays.length; i++) { 
            System.out.print(shDays[i] + "  "); 
        } 
        System.out.println(); 
  
        // Taking an alternative names 
        String[] modDays = { "WED", "THU", "FRI", 
                             "MON", "TUE", "SUN", "SAT" }; 
  
        // Setting the default into modified 
        format.setShortWeekdays(modDays); 
  
        // Displaying the mdified string 
        String[] modifiedDays 
            = format.getShortWeekdays(); 
  
        System.out.print("Modified: "); 
        for (int i = 0; i < modifiedDays.length; i++) { 
            System.out.print(modifiedDays[i] + "  "); 
        } 
    } 
}
输出:
Original: Sun  Mon  Tue  Wed  Thu  Fri  Sat  
Modified: WED  THU  FRI  MON  TUE  SUN  SAT

示例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[] shDays = format.getShortWeekdays(); 
  
        // Displaying the original 
        System.out.print("Original: "); 
  
        for (int i = 1; i < shDays.length; i++) { 
            System.out.print(shDays[i] + "  "); 
        } 
        System.out.println(); 
  
        // Taking an alternative names with 
        // additional random strings 
        String[] modDays = { "WED", "THU", "FRI", 
                             "MON", "TUE", "SUN", 
                             "SAT", "ONE", "TWO" }; 
  
        // Setting the default into modified 
        format.setShortWeekdays(modDays); 
  
        // Displaying the mdified string 
        String[] modifiedDays 
            = format.getShortWeekdays(); 
  
        System.out.print("Modified: "); 
        for (int i = 0; i < modifiedDays.length; i++) { 
            System.out.print(modifiedDays[i] + "  "); 
        } 
    } 
}
输出:
Original: Sun  Mon  Tue  Wed  Thu  Fri  Sat  
Modified: WED  THU  FRI  MON  TUE  SUN  SAT  ONE  TWO

参考: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#setShortWeekdays-java.lang.String:A-



相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 DateFormatSymbols setShortWeekdays() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。