Java 日历 setFirstDayOfWeek()方法及示例

Java 日历 setFirstDayOfWeek()方法及示例

Calendar类中的 setFirstDayOfWeek(int day_val) 方法用于使用该日历中的day_val来设置一周的第一天。

语法

public void set(int day_val)

参数: 该方法需要一个整数类型的参数 day_val ,指的是一周的第一天。

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

下面的程序说明了日历类的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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 日历