Sparta Users Discussions > Be aware when using dates in your application while calculating Leap year

Be aware when using dates in your application while calculating Leap year readonly
Avatar

Joined: January 2009

Total Posts: 7
RavikumarJ
January 07, 2010 at 2:18 AM

Dear All,

    Be aware when using dates in your application while calculating Leap year. All years divisible by 4 are not leap year. Exceptions are there. 1900, 2100, 2200, 2300, 2500, 2600,2700,2900 e.t.c are not leap year.

Check:

1. GregorianCalendar greg = new GregorianCalendar();
    System.out.println(greg.isLeapYear(2100));

2. java.util.Date date = new java.util.Date(2100-1900,1,28);    //Feb 28, 2100
   System.out.println(date.getDay());
   java.util.Date date = new java.util.Date(2100-1900,1,29);     //Feb 29, 2100 (Not exist)
   System.out.println(date.getDay());
   java.util.Date date = new java.util.Date(2100-1900,2,1);       //Mar 1, 2100.      Feb 29, 2100 is same day as Mar 1, 2100.
   System.out.println(date.getDay());

0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday




Solution:
Leap year finding algorithm:

if year modulo 400 is 0
then is_leap_year
else if year modulo 100 is 0
then not_leap_year
else if year modulo 4 is 0
then is_leap_year
else
not_leap_year
 


Gregory agreed that 3 leap years in every 400 years would no longer be leap years. That makes the average calendar year 365.2425 days, compared with
the correct value 365.2422. The rule chosen was that if the last two digits of the year are both zero (1900, 2000, 2100 ...) then it is only a leap
year if the first two digits are divisible by 4. Thus 1600, 2000 are leap years, but 1700, 1800, 1900 and 2100 are not. That is considered good
enough, for the time being.


Ref: http://www.cryst.bbk.ac.uk/CCSG/resources/Year2000/Leap_year_30Feb.html
http://en.wikipedia.org/wiki/Leap_year



Kindly forward this to all developers.