Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDateFormat setLenient() Method in Java with Examples

DateFormat setLenient() Method in Java with Examples

The setLenient(boolean leniency) method in DateFormat class is used to specify whether the interpretation of the date and time of this DateFormat object is to be lenient or not.

Syntax:

public void setLenient(boolean leniency)

Parameters: The method takes one parameter leniency of the boolean type that refers to the mode of the calendar of the DateFormat object. The boolean value true turns on the leniency mode and false turns off the leniency mode.

Return Value: The method does not return any value.

Below programs illustrate the working of setLenient() Method of Calendar class:
Example 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());
    }
}


Output:

Object: java.text.SimpleDateFormat@7945516e
Mar 28, 2019 6:03:48 PM
Leniency: true
New Leniency: false

Example 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());
    }
}


Output:

Object: java.text.SimpleDateFormat@ce9bf0a5
Mar 28, 2019
Leniency: true
New Leniency: false

Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean)

RELATED ARTICLES

Most Popular

Recent Comments