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


Java Calendar setFirstDayOfWeek()用法及代码示例


Calendar类中的setFirstDayOfWeek(int day_val)方法用于在此Calendar中使用day_val设置一周的第一天。

用法:

public void set(int day_val)

参数:该方法采用整数类型的一个参数day_val,表示一周中的第一天。


返回值:该方法不返回任何值。

以下示例程序旨在说明Calendar类的setFirstDayOfWeek()方法的用法:
示例1:

// Java code to illustrate 
// setFirstDayOfWeek() method 
  
import java.util.*; 
  
public class Calendar_Demo { 
    public static void main(String args[]) 
    { 
  
        // Creating calendar object 
        Calendar calndr = Calendar.getInstance(); 
  
        // Displaying first day of the week 
        int first_day = calndr.getFirstDayOfWeek(); 
        System.out.println("The Current"
                           + " First day of the week: "
                           + first_day); 
  
        // Changing the first day of week 
        calndr.setFirstDayOfWeek(Calendar.THURSDAY); 
  
        // Displaying the alternate day 
        first_day = calndr.getFirstDayOfWeek(); 
        System.out.println("The new first"
                           + " day of the week: "
                           + first_day); 
    } 
}
输出:
The Current First day of the week: 1
The new first day of the week: 5

示例2:

// Java code to illustrate 
// setFirstDayOfWeek() method 
  
import java.util.*; 
public class Calendar_Demo { 
    public static void main(String args[]) 
    { 
  
        // Creating calendar object 
        Calendar calndr 
            = new GregorianCalendar(2018, 6, 10); 
  
        // Displaying first day of the week 
        int first_day = calndr.getFirstDayOfWeek(); 
        System.out.println("The"
                           + " First day of the week: "
                           + first_day); 
  
        // Changing the first day of week 
        calndr.setFirstDayOfWeek(Calendar.MONDAY); 
  
        // Displaying the alternate day 
        first_day = calndr.getFirstDayOfWeek(); 
        System.out.println("The new first"
                           + " day of the week: "
                           + first_day); 
    } 
}
输出:
The First day of the week: 1
The new first day of the week: 2

参考: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#setFirstDayOfWeek-int-



相关用法


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