这是判断日期是否合法的程序。相信有助于你。

来源:互联网 发布:淘宝闲鱼客户端 编辑:程序博客网 时间:2024/05/16 05:00

public class Mydate {
  int year;
  int month;
  int day;

 

         public Mydate(int year,int month,int day){
         this.year = year;
         this.month = month;
         this.day = day;
         }
               public void newdate(){
                  System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
                  if(year<0||month<=0||day<=0||month>12||day>31)
                  System.out.println("输入的是非法日期");
                     if(month==4||month==6||month==9||month==11&&day>30)
                     System.out.println("输入的是非法日期");
                         if(month==2 && ((year%4 == 0 && year%100 != 0) || (year%400 == 0))){
                         if(day>29) 
                            System.out.println("输入的是非法日期");
                      } 
            }
    public static void main (String[] args) {
    Mydate date1 = new Mydate(2008,2,29);
    date1.newdate();
    }
}

 

原创粉丝点击