Java Utility Library

Java Calendar - setLenient() Method



The java.util.Calendar.setLenient() method is used to specify whether or not date/time interpretation is to be lenient. With lenient interpretation, a date such as "February 942, 1996" will be treated as being equivalent to the 941st day after February 1, 1996. With strict (non-lenient) interpretation, such dates will cause an exception to be thrown. The default is lenient.

Syntax

public void setLenient(boolean lenient)

Parameters

lenient Specify true if the lenient mode is to be turned on; false if it is to be turned off.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.Calendar.setLenient() method is used to specify whether or not date/time interpretation is to be lenient.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar instance
    Calendar Cal = Calendar.getInstance();

    //creating a date object from calendar and printing it
    System.out.println("The Calendar is: " + Cal.getTime());

    //checks whether the date/time interpretation is lenient.
    System.out.println("Interpretation is lenient?: " + Cal.isLenient()); 

    //set the interpretation is to be not lenient. 
    Cal.setLenient(false);

    //checks whether the date/time interpretation is lenient.
    System.out.println("Interpretation is lenient?: " + Cal.isLenient());     
  }
}

The output of the above code will be:

The Calendar is: Sun Sep 13 13:16:02 UTC 2020
Interpretation is lenient?: true
Interpretation is lenient?: false

❮ Java.util - Calendar