Monday, December 4, 2017

Leap years in Android CalendarView

Use CalendarView with caution.

MonthView uses Utils.getDaysInMonth() that defines a leap year as follows:

case Calendar.FEBRUARY:
    return (year % 4 == 0) ? 29 : 28;

And this is how a leap year should be defined:

Exactly Which Years Are Leap Years?

We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary, day and we add it to the shortest month of the year, February.

In the Gregorian calendar three criteria must be taken into account to identify leap years:


  • The year can be evenly divided by 4;
  • If the year can be evenly divided by 100, it is NOT a leap year, unless;
  • The year is also evenly divisible by 400. Then it is a leap year.


This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.

So, 2100 is not a leap year, but according to Android, it is:


There are more problems related to CalendarView.  For example, the control does not allow a user to select February 29, 2100, but it allows to select February 28.  Again, be cautious.

No comments:

Post a Comment