Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate leap year in Java [duplicate]

Tags:

java

calendar

Possible Duplicate:
Java Code for calculating Leap Year, is this code correct?

This was homework, and I have already received my grade, but I did not implement leap years in my code. This is a simple program that displays the numbers in a month based on user input. The only thing is I can't figure out is a way to implement a leap year that would get 29 days for February rather than 28, without writing multiple if statements. Surely there is an easier way? Here is the code:

//Displays number of days in a month

package chapter_3;

import java.util.Scanner;


public class Chapter_3 {

    public static void main(String[] args) {
        System.out.println("This program will calculate \n"
                + "the number of days in a month based upon \n"
                + "your input, when prompted please enter \n"
                + "the numerical value of the month you would like\n"
                + "to view including the year. Example: if you would like\n"
                + "to view March enter 3 2011 and so on.");

        Scanner input = new Scanner(System.in);

        //Prompt user for month and year
        System.out.print("Please enter the month and year: ");
        int month = input.nextInt();
        int year = input.nextInt();

        if (month == 1) {
            System.out.println("January " + year + " has 31 days");
        }   
        else if (month == 2) {
            System.out.println("February " + year + " has 28 days");
        }   
        else if (month == 3) {
            System.out.println("March " + year + " has 31 days");
        }
        else if (month == 4) {
            System.out.println("April " + year + " has 30 days");
        }
        else if (month == 5) {
            System.out.println("May " + year + " has 31 days");
        }
        else if (month == 6) {
            System.out.println("June " + year + " has 30 days");
        }
        else if (month == 7) {
            System.out.println("July " + year + " has 31 days");
        }
        else if (month == 8) {
            System.out.println("August " + year + " has 31 days");
        }
        else if (month == 9) {
            System.out.println("September " + year + " has 30 days");
         }
        else if (month == 10) {
            System.out.println("October " + year + " has 30 days");
        }
        else if (month == 11) {
            System.out.println("November " + year + " has 30 days");
        }
        else if (month == 12) {
            System.out.println("December " + year + " has 31 days");
         }
        else {
            System.out.println("Please enter the numerical value "
                + "of the month you would like to view");
        }
    }
}        
like image 980
Gmenfan83 Avatar asked Sep 13 '11 00:09

Gmenfan83


People also ask

How do you calculate leap year in Java?

Method 1: Using if-else statements for Finding Leap Year in Java. The procedure is as follows: Let the year be y. We start our if-else block and check if the year is divisible by 400, or not, if true, the year is a leap year and we print the same, otherwise we move to the else if block.

Why is the leap year omitted 3 times every 400 years?

Because of the extra time that gets added to the calendar over a 400-year span, years that are divisible by 100 omit leap day unless they are also divisible by 400.

What is the algorithm for leap year?

The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years, if they are exactly divisible by 400.

How do you find the number of leap years between two dates?

Leap years happen every 4 years. Leap years have 366 days instead of the regular 365 days. The extra day is added in February, creating a new date: the 29th of February, which occurs once in four years.


2 Answers

If you use the GregorianCalendar, you could do as below

Determines if the given year is a leap year. Returns true if the given year is a leap year. To specify BC year numbers, 1 - year number must be given. For example, year BC 4 is specified as -3.

GregorianCalendar cal = new GregorianCalendar();

if(cal.isLeapYear(year))
{
    System.out.print("Given year is leap year.");
}
else
{ 
    System.out.print("Given year is not leap year.");
}
like image 84
CharithJ Avatar answered Sep 22 '22 07:09

CharithJ


Quoting http://en.wikipedia.org/wiki/Leap_year

Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.

So if you want to reinvent the wheel:

boolean isLeapYear = year%4 == 0 && (year%100 != 0 || year%400 == 0)

But the most elegant solution (taken from here) is probably

public static boolean isLeapYear(int year) {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, year);
  return cal.getActualMaximum(DAY_OF_YEAR) > 365;
}
like image 32
Clayton Louden Avatar answered Sep 19 '22 07:09

Clayton Louden