有效日期方法逻辑?

时间:2016-02-22 03:53:58

标签: java logic

我无法理解为什么这个方法会使我输入的所有日期都返回为真(有效)。这种方法唯一可以完美运作的部分是闰年声明。我已经尝试定义另一个名为isValid的变量并将其返回为true并添加if语句以将该方法返回为true或false,但是,这仅适用于某些日期,我也不太明白。谁能告诉我为什么这种方法存在缺陷?

public boolean isValid()
{
  if ( month == Month.JAN || month == Month.MAR || 
        month == Month.MAY || month == Month.JUL
        || month == Month.OCT || month == Month.OCT 
        || month == Month.DEC && day <= Month.DAYS_ODD )
     return true;
  if ( month == Month.APR || month == Month.JUN 
        || month == Month.SEP
        || month == Month.NOV && day <= Month.DAYS_EVEN )
     return true;
  boolean leapYear = false;
  if ( year % Month.QUADRENNIAL == 0 || year % Month.CENTENNIAL == 0
        || year % Month.QUATERCENTENNIAL == 0 )
     leapYear = true;
  if ( leapYear )
     if ( month == Month.FEB && day <= Month.DAYS_FEB + 1)
        return true;
  if ( month == Month.FEB && day <= Month.DAYS_FEB )
     return true;
  return false;

}

public class Month 
{
   public static final int JAN = 1;
   public static final int FEB = 2;
   public static final int MAR = 3;
   public static final int APR = 4;
   public static final int MAY = 5;
   public static final int JUN = 6;
   public static final int JUL = 7;
   public static final int AUG = 8;
   public static final int SEP = 9;
   public static final int OCT = 10;
   public static final int NOV = 11;
   public static final int DEC = 12;
   public static final int DAYS_ODD = 31;
   public static final int DAYS_EVEN = 30;
   public static final int DAYS_FEB = 28;
   public static final int QUADRENNIAL = 4;
   public static final int CENTENNIAL = 100;
   public static final int QUATERCENTENNIAL = 400;
}

1 个答案:

答案 0 :(得分:0)

闰年的逻辑是错误的。它应该如下所示。

Getting FileVersion of C:\Users\User\Documents\Company\Source\product\testapp\bin\x86\Debug\app.exe: 2.0.0.0

可以被100整除的数字不是闰年,例如:1900年,它不是闰年。