Ad

Monday 30 September 2013

Lenient and Non-lenient modes of Calendar in Java

In the previous post Working with Calendar ,we saw about how the Calendar API can be used to work with dates in Java.Here we will see about the two modes of the Calendar -Lenient and Non Lenient.
Leninet
   By default the Calendar will be in lenient mode.If we give the wide range of values for the Calendar fields(that doesn't fall under its range),the calendar will accept the value.On the subsequent call of the get..() Method,the Calendar will recomputes and the adjust the value to fall under the range.For example if we set the Month Calendar Field(i.e Calendar.MONTH) to February and Day of the Month Field(i.e Calendar.DAY_OF_MONTH) to be out of range as 30,the lenient calendar will recomputes the date as March 2(as there are only 28 days in February).
Non-Lenient
   To make the calendar in non-lenient mode,lenient flag should be made as false using setLenient() method.If the above out of range Calendar fields were set in Non-lenient Calendar,it will throw the exception on call of any get..() method.Exception will be thrown only on call of get..()(and not during the set or add),since the calendar will recomputes the value only at this time.
we can more more understand about this by seeing the example program below

import java.util.Calendar;                                                                                                                        

public class CalendarLenientExample {

public static void main(String[] args) {
CalendarLenientExample calendarExample= new CalendarLenientExample();
calendarExample.testForLenient();
calendarExample.testForNonLenient();
}
 
/*Calendar in Lenient Mode*/
public void testForLenient() {
Calendar calendar= Calendar.getInstance();//By default the calendar will be in lenient mode
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.DAY_OF_MONTH, 30);
System.out.println("Constructed Date in Lenient Mode:"+calendar.getTime());

}

/*Calendar in Non-Lenient Mode*/
public void testForNonLenient() {
try
{
Calendar calendar= Calendar.getInstance();
calendar.setLenient(false);//Make the Calendar Mode as non-lenient
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.DAY_OF_MONTH, 30);
System.out.println("Constructed Date in NonLenient Mode:"+calendar.getTime());
}
catch(Exception e)
{
System.out.println("Exception encountered in Non-Leneient Mode");
e.printStackTrace();
}
}
}

OUTPUT:
Constructed Date in Lenient Mode:Sat Mar 02 11:23:38 IST 2013
Exception encountered in Non-Leneient Mode
java.lang.IllegalArgumentException: MONTH
at java.util.GregorianCalendar.computeTime(Unknown Source)
at java.util.Calendar.updateTime(Unknown Source)
at java.util.Calendar.getTimeInMillis(Unknown Source)
at java.util.Calendar.getTime(Unknown Source)
at CalendarLenientExample.testForNonLenient(CalendarLenientExample.java:28)
at CalendarLenientExample.main(CalendarLenientExample.java:8)

As we can see in the output,
  •  Although we have given the Calendar.DAY_OF_MONTH to be out of range i.e 30,the getTime() method of the lenient calendar recomputed the value to March 2.
  • In Non-Lenient Mode,the getTime() method of the Calendar thrown the IllegalArgumentException.The exception will also tell as which calendar field was given out of range.In our case it is MONTH as we can see in the exceptionstacktrace. 

No comments:

Post a Comment