有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java日验证返回错误

Day Validation总是返回非法参数异常,从不将日期设置为已输入,尤其是在闰年的2月尝试输入29时。我一直在努力寻找错误,但现在我放弃了。提前谢谢你的帮助

import java.util.Calendar;

public class DateOfBirth
{
    private int day;
    private int month;
    private int year;

    private Calendar cld = Calendar.getInstance();

    public DateOfBirth()
    {
        this(0,0,0);
    } 
    public DateOfBirth(int a,int b,int c)
    {
        setDay(c);
        setMonth(a);
        setYear(b);
    }
    public void setDay(int a)
    {
        if( getMonth()==2 && (getYear() % 4 == 0 && (getYear() % 100 != 0 || getYear() % 400 == 0))&& a == 29)
             day = a;
        else if(getMonth() == 2 && a <= 28)
            day = a;
        else if(getMonth() == 1 || getMonth() == 3 || getMonth() == 5 || getMonth() ==  7 || getMonth() ==  8 || getMonth() ==  10 || getMonth() == 12 && a < 31)
            day = a;
        else if(getMonth() == 4 || getMonth() == 6 || getMonth() == 9 || getMonth() == 11 && a < 30)
            day = a;
        else
            throw new IllegalArgumentException("Day Out Of Bounds.");
    }
    public void setMonth(int b)
    {
        if( b > 0 && b <= 12)
            month = b;
        else
            throw new IllegalArgumentException("Month Out of Bounds.");
    }
    public void setYear(int c)
    {
        if( c > (cld.get(Calendar.YEAR) - 150) && c < cld.get(Calendar.YEAR))
            year = c;
        else
            throw new IllegalArgumentException("Year Out of Bounds.");
    }
    public int getDay()
    {
        return day;
    }
    public int getMonth()
    {
        return month;
    }
    public int getYear()
    {
        return year;
    }
    @Override
    public String toString()
    {
        return String.format("Date Of Birth: %02d/%02d/%d",getDay(),getMonth(),getYear());
    }
}

共 (0) 个答案