Detect Leap Year

Posted on 2011-10-10
Last Modified: 2012-05-12
How do I detect if its a leap year in Java and change the number of days in a month accordingly ?
Question by:abuyusuf35
    LVL 86

    Expert Comment

    Calendar c = Calendar.getInstance();
    boolean leap = c.isLeapYear();

    Open in new window

    LVL 86

    Expert Comment

    GrgorianCalendar c = new GregorianCalendar();
    boolean leap = c.isLeapYear();

    Open in new window

    LVL 47

    Accepted Solution


    This deermines niumber of days in a month:

    // Create a calendar object of the desired month
    Calendar cal = new GregorianCalendar(1999, Calendar.FEBRUARY, 1);
    // Get the number of days in that month
    int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28
    // Try month in a leap year
    cal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);
    days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);     // 29

    Open in new window
    LVL 37

    Expert Comment

    >> ... and change the number of days in a month accordingly?
    What exactly do you mean by that?
    LVL 86

    Expert Comment

    abuyusuf35, can you tell me why you ignored my first comment?

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    nested interface 5 30
    word0 challenge 3 35
    firstChar challenge 13 60
    solarwind tftp server 2 20
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now