Java 日历 setLenient()方法及示例

Java 日历 setLenient()方法及示例

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

语法

public void setLenient(boolean leniency)

参数: 该方法需要一个布尔类型的参数leniency,指的是日历的模式。布尔值 “true “开启宽大处理模式,”false “关闭宽大处理模式。

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

下面的程序说明了日历类的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://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#setLenient-boolean-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 日历