📜  Java中的日历 setFirstDayOfWeek() 方法及示例

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

Java中的日历 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: Java/util/Calendar.html#setFirstDayOfWeek-int-