Ahmad Ando
Ahmad Ando

Reputation: 41

Validation issue With Date Program

I have to validate the date,For example If I enter 31/2/2013,It should give an error as February doesn't contains 31 days,but I am stuck at how to achieve that,I tried to use the switch statement but still in vain.Help would be much appreciated.

public class Date
{
private int PDay;
private int PMonth;
private int PYear;

public Date(int day,int month,int year)
{
    setDay(day);
    setMonth(month);
    setYear(year);
}

public Date(int month,int year)
{
    this(1,month,year);
}

public Date(int year)
{
    this(1,1,year);
}

public void setDay(int day)
{
    PDay=day;
}

public int getDay()
{
    return PDay;
}

public void setMonth(int month)
{
    if(month>=1 && month<=12)
        PMonth=month;
    else
        System.out.println("Month Invalid-Must be Between 1 & 12");
}

public int getMonth()
{
    return PMonth;
}

public void setYear(int year)
{
    if(year>=1950 && year<=2049)
        PYear=year;
    else
        System.out.println("Year Invalid-Must be Between 1950 & 2049");
}

public int getYear()
{
    return PYear;
}

public String toString()
{
    return PDay+"/"+PMonth+"/"+PYear;
}
}

P.S. Its not Homework :P/>

Test Program is:

public class DateTest
{
    public static void main(String[] args)
{
    Date newDate = new Date(7,14,2012);
    Date newDate1 = new Date(2152);
    System.out.println(newDate);
    System.out.println(newDate1);
}
}

Upvotes: 1

Views: 171

Answers (2)

mschenk74
mschenk74

Reputation: 3591

EDIT: Don't implement date handling on your own as long as there are existing frameworks fitting your needs.

But beware of the pitfalls of some of the existing frameworks: (end of EDIT)

Don't use java.util.Date for such checks. Nearly all of the methods of Date are buggy and have been declared as deprecated. Those setters and getters have been reimplemented in the java.util.Calendar classes. But since they are still buggy in some circumstances the best choice would be to use the new Date Time API of Java 8 if this is an option or to use the "third-party" Jodatime library for correct date handling.

If you just want to have some checks on input, then you may use java.text.SimpleDateFormat that handles the parsing and the checks.

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

In the constructor, set the day after the year and month:

public Date(int day, int month, int year) {
    setYear(year);
    setMonth(month);
    setDay(day);
}

And in the method setDay() add the validation logic:

public void setDay(int day) {
    if (pMonth == 1 && (day < 1 || day > 31)) { 
        // throw exception or handle error
        throw new IllegalArgumentException("invalid number of days");
    } else if (pMonth == 2 && (day < 1 || day > 28)) {
        // same thing
    }

    // ... rest of validations

    PDay = day;

}

If you want to be even more strict, you can use the year to determine if February has 28 or 29 days.

Upvotes: 1

Related Questions