美文网首页
日期的`异常`捕获

日期的`异常`捕获

作者: 懵智的大仁哥 | 来源:发表于2016-03-03 14:56 被阅读41次
 /**
 *  日期的异常捕获
 */
//年异常
class YearException extends Exception{
    String info;
    int year;
    public YearException(){}
    public YearException(String info){
        this.info=info;
    }
    public YearException(String info,int year){
        this(info);
        this.year=year;
    }
    public String getInfo(){
        return this.info;
    }

}
//月异常
class MonthException extends Exception{
    String info;
    int month;
    public MonthException(){}
    public MonthException(String info){
        this.info=info;
    }
    public MonthException(String info,int month){
        this(info);
        this.month=month;
    }
    public String getInfo(){
        return this.info;
    }

}
//日异常
class DayException extends Exception{
    String info;
    int day;
    public DayException(){}
    public DayException(String info){
        this.info=info;
    }
    public DayException(String info,int day){
        this(info);
        this.day=day;
    }
    public String getInfo(){
        return this.info;
    }

}
//日期异常,如闰年2月29
class DateException extends Exception{
    String info;
    int year;
    int month;
    int day;
    public DateException(){}
    public DateException(String info){
        this.info=info;
    }
    public DateException(String info,int year){
        this(info);
        this.year=year;
    }
    public DateException(String info,int year,int month){
        this(info,year);
        this.month=month;
    }
    public DateException(String info,int year,int month,int day){
        this(info,year,month);
        this.day=day;
    }
    public String getInfo(){
        return this.info;
    }

}
//人类
class Person{
    BirthDay birthday; //生日
    class BirthDay{
        int year;
        int month;
        int day;
        public BirthDay(int year,int month,int day){

            this.year=year;
            this.month=month;
            this.day=day;
        }
    }
    //人类的构造函数,因为涉及生日的初始化,可能发生异常
    public Person(int year,int month,int day)throws YearException,MonthException,DayException,DateException{
        if(year<0){
            throw new YearException("Exception: Year = "+year+" < 0 !",year);
        }else if(month<1){
            throw new MonthException("Exception: Month = "+month+" < 1 !",month);
        }else if(month>12){
            throw new MonthException("Exception: Month = "+month+" > 12 !",month);
        }else if(day<0){
            throw new DayException("Exception: Day = "+day+" < 0 !",day);
        }else if(day>31) {
            throw new DayException("Exception: Day = "+day+" > 31 !",day);
        }else if((month==4||month==6||month==9||month==11)&& day > 30 ){
            throw new DateException("Exception: Month = "+month+",but Day = "+day+"> 30 !",year,month,day);
        }else if(month==2&&day>29) {
            throw new DateException("Exception: Month = "+month+",but Day = "+day+"> 29 !",year,month,day);
        }else if(!((year % 4 == 0 && year % 100 != 0) ||year%400==0)&&day>28){
            throw new DateException("Exception: Year = "+year+" is not Leap Year,"+" Month = "+month+",but Day = "+day+" > 29 !",year,month,day);
        }else{
            this.birthday = new BirthDay(year,month,day);
        }
        
    }
}

class DateDemo{
    //入口
    public static void main(String[] args){
        try{
            //年异常
            Person p1 = new Person(-1994,9,12);
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        
        try{
            //月异常
            Person p2 = new Person(1994,19,12);   
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        
        try{
            //日异常
            Person p3 = new Person(1994,9,32);    
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        try{
            //二月异常
            Person p4 = new Person(1994,2,30);    
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        try{
            //闰年二月异常
            Person p5 = new Person(2017,2,29);    
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }       
    }
}

结果:

MacbookPro:JAVA Hx$ java DateDemo
Exception: Year = -1994 < 0 !
Exception: Month = 19 > 12 !
Exception: Day = 32 > 31 !
Exception: Month = 2,but Day = 30> 29 !
Exception: Year = 2016 is Leap Year, Month = 2,but Day = 29 > 28 !

相关文章

网友评论

      本文标题:日期的`异常`捕获

      本文链接:https://www.haomeiwen.com/subject/qmtokttx.html