Java Code for calculating Leap Year

前端 未结 20 2268
执念已碎
执念已碎 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.

    0 讨论(0)
  • 2020-11-22 16:56

    Most Efficient Leap Year Test:

    if ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0))
    {
        /* leap year */
    }
    

    This is an excerpt from my detailed answer at https://stackoverflow.com/a/11595914/733805

    0 讨论(0)
  • 2020-11-22 16:56

    this answer is great but it won't work for years before Christ (using a proleptic Gregorian calendar). If you want it to work for B.C. years, then use the following adaptation:

    public static boolean isLeapYear(final int year) {
        final Calendar cal = Calendar.getInstance();
        if (year<0) {
            cal.set(Calendar.ERA, GregorianCalendar.BC);
            cal.set(Calendar.YEAR, -year);
        } else
            cal.set(Calendar.YEAR, year);
        return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
    }
    

    You can verify that for yourself by considering that year -5 (i.e. 4 BC) should be pronounced as a leap year assuming a proleptic Gregorian calendar. Same with year -1 (the year before 1 AD). The linked to answer does not handle that case whereas the above adapted code does.

    0 讨论(0)
  • 2020-11-22 16:57

    java.time.Year::isLeap

    I'd like to add the new java.time way of doing this with the Year class and isLeap method:

    java.time.Year.of(year).isLeap();
    
    0 讨论(0)
  • 2020-11-22 17:02

    If you are using java8 :

    java.time.Year.of(year).isLeap()
    

    Java implementation of above method:

    public static boolean isLeap(long year) {
            return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
        }
    
    0 讨论(0)
  • 2020-11-22 17:05

    This is what I came up with. There is an added function to check to see if the int is past the date on which the exceptions were imposed(year$100, year %400). Before 1582 those exceptions weren't around.

    import java.util.Scanner;
    
    public class lecture{
    
    
    public static void main(String[] args) {
        boolean loop=true;
        Scanner console = new Scanner( System.in );
        while (loop){
            System.out.print( "Enter the year: " );
    
            int year= console.nextInt();
            System.out.println( "The year is a leap year: "+ leapYear(year) );
            System.out.print( "again?: " );
            int again = console.nextInt();
            if (again == 1){
                loop=false;
            }//if
        }
    }
    public static boolean leapYear ( int year){
        boolean leaped = false;
        if (year%4==0){
            leaped = true;
            if(year>1582){
                if (year%100==0&&year%400!=0){
                    leaped=false;
                }
            }
        }//1st if
        return leaped;
    }
    } 
    
    0 讨论(0)
提交回复
热议问题