Java DateFormat isLenient()方法及示例

来源:这里教程网 时间:2026-02-17 20:44:45 作者:

Java DateFormat isLenient()方法及示例

DateFormat类中的 isLenient() 方法用于了解和理解该DateFormat对象的日期和时间解析是否被认为是宽松的。

语法

public boolean isLenient()

参数: 该方法不接受任何参数。

返回值: 如果这个日历的解释是宽松的,该方法返回True,否则返回False。

下面的程序说明了日历类的isLenient()方法的工作原理:

例1 :

// Java code to illustrate// isLenient() 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());    }}

输出:

Object: java.text.SimpleDateFormat@7945516eMar 28, 2019 4:23:01 AMLeniency: true

例2 :

// Java code to illustrate// isLenient() method  import java.text.*;import java.util.*;  public class DateFormat_Demo {    public static void main(String[] argv)    {        // 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());    }}

输出:

Object: java.text.SimpleDateFormat@ce9bf0a5Mar 28, 2019Leniency: true

参考资料: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#isLenient()

相关推荐