DateFormat类中的setLenient(boolean leniency)方法用于指定此DateFormat对象的日期和时间的解释是否宽松。
用法:
public void setLenient(boolean leniency)
参数:该方法采用一种布尔类型的参数leniency,该leniency引用了DateFormat对象的日历模式。布尔值true将打开宽大处理模式,而false则关闭宽大处理模式。
返回值:该方法不返回任何值。
以下示例程序旨在说明Calendar类的setLenient()方法的用法:
示例1:
// Java code to illustrate
// setLenient() method
import java.text.*;
import java.util.*;
public class DateFormat_Demo {
public static void main(String[] args)
{
// Initializing the first formatter
DateFormat DFormat
= DateFormat.getDateTimeInstance();
System.out.println("Object: " + DFormat);
// String formatting
String str = DFormat.format(new Date());
// Displaying the string time
System.out.println(str);
System.out.println("Leniency: "
+ DFormat.isLenient());
// Changing the leniency
DFormat.setLenient(false);
// Displaying the modified leniency
System.out.println("New Leniency: "
+ DFormat.isLenient());
}
}
输出:
Object: java.text.SimpleDateFormat@7945516e
Mar 28, 2019 6:03:48 PM
Leniency: true
New Leniency: false
示例2:
// Java code to illustrate
// setLenient() method
import java.text.*;
import java.util.*;
public class DateFormat_Demo {
public static void main(String[] args)
{
// Initializing the first formatter
DateFormat DFormat
= DateFormat.getDateInstance();
System.out.println("Object: " + DFormat);
// String formatting
String str = DFormat.format(new Date());
// Displaying the string time
System.out.println(str);
System.out.println("Leniency: "
+ DFormat.isLenient());
// Changing the leniency
DFormat.setLenient(false);
// Displaying the modified leniency
System.out.println("New Leniency: "
+ DFormat.isLenient());
}
}
输出:
Object: java.text.SimpleDateFormat@ce9bf0a5
Mar 28, 2019
Leniency: true
New Leniency: false