java日期有效性验证

 private static boolean isValidDate(String strValue ) {//20091001字符串 
     
   int d = Integer.parseInt(strValue.substring(6, 8));
   int m = Integer.parseInt(strValue.substring(4, 6));
   int y = Integer.parseInt(strValue.substring(0, 4));
  
   if (d < 1 || m < 1 || m > 12) return false;
  
   if (m == 2) {
          if (isLeapYear(y)) return d <= 29;
          else return d <= 28;
       }
       else if (m == 4 || m == 6 || m == 9 || m == 11)
          return d <= 30;
       else 
          return d <= 31;
    }
     
    private static boolean isLeapYear(int y) {//判断是否为闰年
       return y % 4 == 0 && (y % 400 == 0 || y % 100 != 0); 
    }

猜你喜欢

转载自blog.csdn.net/jifeijixufly/article/details/5089684