Java Code for calculating Leap Year

前端 未结 20 2256
执念已碎
执念已碎 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 17:15
    import javax.swing.*;
    public class LeapYear {
        public static void main(String[] args) {
        int year;
    String yearStr = JOptionPane.showInputDialog(null, "Enter radius: " );
    
    year = Integer.parseInt( yearStr );
    
    boolean isLeapYear;
    isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);  
    
     if(isLeapYear){ 
    JOptionPane.showMessageDialog(null, "Leap Year!"); 
     }  
     else{
    JOptionPane.showMessageDialog(null, "Not a Leap Year!"); 
        }
        }
        }
    
    0 讨论(0)
  • 2020-11-22 17:17
    new GregorianCalendar().isLeapYear(year);
    
    0 讨论(0)
  • 2020-11-22 17:17
    public static void main(String[] args)
    {
    
    String strDate="Feb 2013";
            String[] strArray=strDate.split("\\s+");        
    
            Calendar cal = Calendar.getInstance();
            cal.setTime(new SimpleDateFormat("MMM").parse(strArray[0].toString()));
            int monthInt = cal.get(Calendar.MONTH);
            monthInt++;
            cal.set(Calendar.YEAR, Integer.parseInt(strArray[1]));          
            strDate=strArray[1].toString()+"-"+monthInt+"-"+cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    
            System.out.println(strDate);    
    
    
    
    }
    
    0 讨论(0)
  • 2020-11-22 17:17
        import java.util.Scanner;
    
        public class LeapYear {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the year then press Enter : ");
            int year = input.nextInt();
    
            if ((year < 1580) && (year % 4 == 0)) {
                System.out.println("Leap year: " + year);
            } else {
                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                    System.out.println("Leap year: " + year);
                } else {
                    System.out.println(year + " not a leap year!");
                }
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 17:20

    The correct implementation is:

    public static boolean isLeapYear(int year) {
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.YEAR, year);
      return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
    }
    

    But if you are going to reinvent this wheel then:

    public static boolean isLeapYear(int year) {
      if (year % 4 != 0) {
        return false;
      } else if (year % 400 == 0) {
        return true;
      } else if (year % 100 == 0) {
        return false;
      } else {
        return true;
      }
    }
    
    0 讨论(0)
  • 2020-11-22 17:20

    I suggest you put this code into a method and create a unit test.

    public static boolean isLeapYear(int year) {
        assert year >= 1583; // not valid before this date.
        return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }
    

    In the unit test

    assertTrue(isLeapYear(2000));
    assertTrue(isLeapYear(1904));
    assertFalse(isLeapYear(1900));
    assertFalse(isLeapYear(1901));
    
    0 讨论(0)
提交回复
热议问题