Java Code for calculating Leap Year

前端 未结 20 2310
执念已碎
执念已碎 2020-11-22 16:30

I am following \"The Art and Science of Java\" book and it shows how to calculate a leap year. The book uses ACM Java Task Force\'s library.

Here is the code the boo

20条回答
  •  心在旅途
    2020-11-22 16:54

    From JAVA's GregorianCalendar sourcecode:

    /**
     * Returns true if {@code year} is a leap year.
     */
    public boolean isLeapYear(int year) {
        if (year > changeYear) {
            return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
        }
    
        return year % 4 == 0;
    }
    

    Where changeYear is the year the Julian Calendar becomes the Gregorian Calendar (1582).

    The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400.

    In the Gregorian Calendar documentation you can found more information about it.

提交回复
热议问题