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

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

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

Calendar 类中的setLenient(boolean leniency )方法用于指定对日期和时间的解释是否宽松。

句法:

public void setLenient(boolean leniency)

参数:该方法接受一个布尔类型的参数leniency,它是指日历的模式。布尔值 true 打开宽大模式, false 关闭宽大模式。

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

下面的程序说明了 Calendar 类的 setFirstDayOfWeek() 方法的工作:
示例 1:

// Java code to illustrate
// setLenient() method
  
import java.util.*;
public class Calendar_Demo {
    public static void main(String args[])
    {
  
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the current
        // mood of leniency
        boolean value = calndr.isLenient();
        System.out.println("Is the"
                           + " Calendar lenient? "
                           + value);
  
        // Changing the leniency
        calndr.setLenient(false);
  
        // Displaying the result
        System.out.println("The altered"
                           + " Leniency: "
                           + calndr.isLenient());
    }
}
输出:
Is the Calendar lenient? true
The altered Leniency: false

示例 2:

// Java code to illustrate
// isLenient() method
  
import java.util.*;
public class CalendarDemo {
    public static void main(String args[])
    {
  
        // Creating a calendar object
        Calendar calndr
            = Calendar.getInstance();
  
        // Displaying the calendar
        System.out.println("Current Calendar: "
                           + calndr.getTime());
  
        // Checking the leniency
        boolean leniency = calndr.isLenient();
        calndr.setLenient(false);
        leniency = calndr.isLenient();
  
        // Displaying the leniency
        System.out.println("Calendar is"
                           + " lenient? "
                           + leniency);
  
        // Checking the leniency
        calndr.setLenient(true);
        leniency = calndr.isLenient();
  
        // Displaying the leniency
        System.out.println("Calendar is"
                           + " lenient: "
                           + leniency);
    }
}
输出:
Current Calendar: Thu Feb 21 11:00:50 UTC 2019
Calendar is lenient? false
Calendar is lenient: true

参考: https: Java/util/Calendar.html#setLenient-boolean-