7

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");
        }
    }
}        
Community
  • 1
  • 1
Gmenfan83
  • 2,487
  • 5
  • 30
  • 34

3 Answers3

21

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.");
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
CharithJ
  • 46,289
  • 20
  • 116
  • 131
14

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;
}
Community
  • 1
  • 1
Clayton Louden
  • 1,056
  • 2
  • 11
  • 28
3

you could use a switch statment, with a few if statements inside the february case:

switch(month)
{
case 1:
    System.out.println("January " + year + " has 31 days");
    break;
case 2:
    int n=28;
    if(year%4==0 && (year%100!=0 || year%400==0)
        n++;
    System.out.println("February " + year + " has "+n+" days");
    break;
case 3:
    System.out.println("March " + year + " has 31 days");
    break;
case 4:
    System.out.println("April " + year + " has 30 days");
    break;
case 5:
    System.out.println("May " + year + " has 31 days");
    break;
case 6:
    System.out.println("June " + year + " has 30 days");
    break;
case 7:
    System.out.println("July " + year + " has 31 days");
    break;
case 8:
    System.out.println("August " + year + " has 31 days");
    break;
case 9:
    System.out.println("September " + year + " has 30 days");
    break;
case 10:
    System.out.println("October " + year + " has 30 days");
    break;
case 11:
    System.out.println("November " + year + " has 30 days");
    break;
case 12:
    System.out.println("December " + year + " has 31 days");
    break;
default case:
    System.out.println("Please enter the numerical value of the month you would like to view");
}

see http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html for more info

Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65