.isLenient()
The .isLenient()
method of the Calendar
class returns True
if the interpretation of this Calendar
is lenient else it returns False
.
Syntax
calendar.isLenient()
Note: When a
Calendar
is lenient, it allows date and time values that may not make sense in a real-world context. For example,February 30th
or a time value of25:70:90
would be accepted. When aCalendar
is non-lenient, it enforces stricter rules, and attempting to set invalid date or time values will throw exceptions, such asIllegalArgumentException
.
Example 1
The example below demonstrates the use of the .isLenient()
method.
import java.util.Calendar;public class CalendarIsLenientExample {public static void main(String args[]){// Creating a calendar objectCalendar cal = Calendar.getInstance();// Displaying the calendaSystem.out.println("Current Calendar: " + cal.getTime());// Checking the leniencyboolean leniency = cal.isLenient();// Displaying the leniencySystem.out.println("Calendar is" + " lenient: " + leniency);}}
This code will return an output similar to the following:
Current Calendar: Wed Sep 06 17:51:57 GMT 2023Calendar is lenient: true
Example 2
The example below demonstrates the use of the .isLenient()
method.
import java.util.Calendar;public class CalendarIsLenientExample{public static void main(String args[]){// Creating a calendar objectCalendar cal = Calendar.getInstance();// Displaying the calendarSystem.out.println("Current Calendar: " + cal.getTime());// Checking the leniencyboolean leniency = cal.isLenient();cal.setLenient(false);leniency = cal.isLenient();// Displaying the leniencySystem.out.println("Calendar is" + " lenient: " + leniency);}}
This code will return an output similar to the following:
Current Calendar: Wed Sep 06 17:57:00 GMT 2023Calendar is lenient: false
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.