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


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


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

用法:

public void setLenient(boolean leniency)

参数:该方法采用一种布尔类型的参数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://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#setLenient-boolean-



相关用法


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