Java异常管理作业——判断日期是否存在

 MyDate类只有一个构造方法,3个参数year,month,date 为年月日初始化 在构造方法中检查录入的数据是否准确

         year: [1900,2099]

        month: [1,12]

        date: 取决于year和month

如果录入不正确,抛出Exception异常


public class MyDate implements Cloneable,Comparable<MyDate>{
    public int year,month,date;

    public MyDate(int year,int month,int date) throws Exception {

        if (this.year<1900||this.year>2099){
            throw new Exception();
        }
        if (this.month<1||this.month>12){
            throw new Exception();
        }
        int[] mon={1,3,5,7,8,10,12};
        int[] mon1={4,6,9,11};
        for (int i=0;i<mon.length;i++)
        {
            if (this.month==mon[i]&&this.date<=31){

            }else {
                throw new Exception();
            }
        }
        for (int i=0;i<mon1.length;i++)
        {
            if (this.month==mon1[i]&&this.date<=3){

            }else {
                throw new Exception();
            }
        }
        if (this.month==2){
            if ((this.year%4==0&&year%100!=0)||(this.year%400==0)){
                if (this.date > 29) {
                    throw new Exception();
                } else if (this.date>28) {
                    throw new Exception();
                }
            }
        }


        this.year=year;
        this.month=month;
        this.date=date;
    }
    public boolean equals(Object obj){
        if (obj instanceof MyDate){
            MyDate temp=(MyDate) obj;
            return (this.year== temp.year&&
                    this.month==month&&this.date==date);
        }
        return false;
    }
    public String toString(){
        return "{"+this.year+"-"+this.month+"-"+this.date+"}";
    }
    public int hashcode(){
        return this.year;
    }

    public Object clone() {
        try {
            return new MyDate(year,month,date);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int compareTo(MyDate o) {
        if(this.year>o.year){
            return 1;
        } else if (this.year<o.year) {
            return -1;
        }else if (this.month>o.month){
            return 1;
        }else if(this.month<o.month){
            return -1;
        } else if (this.date>o.date) {
            return 1;
        }else if (this.date<o.date){
            return -1;
        }


        return 0;
    }
}

public class Test1 {
    public static void main(String[] args)  {
        MyDate d1= null;
        MyDate d2= null;
        MyDate[] da=new MyDate[3];
        try {
            d1 = new MyDate(1800,01,07);
            d2=new MyDate(1997,01,07);

            da[0]=new MyDate(1997,01,07);
            da[1]=new MyDate(1997,04,07);
            da[2]=new MyDate(1997,04,07);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



        System.out.println(d1==d2);

        MyDate d3=(MyDate) d1.clone();
        System.out.println(d3);


        System.out.println(da[0].compareTo(da[1]));
        System.out.println(da[2].compareTo(da[1]));
        System.out.println(da[1].compareTo(da[0]));



    }
}

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值