Search code examples
javanullswitch-statementtry-catchtry-catch-finally

How to avoid return null on my case?


I need to write a Java program (class Date) and "class NextDay that calculates and prints the date of the next day by entering the day, month, and year."

At public Date getNextDay() method I must use return null, otherwise it gives error. How can I avoid return null?

Here are my codes;

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


    public Date(int day, int month, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }


    public int getMaxDaysInMonth()
    {
        int daysInMonth = 0;
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                daysInMonth = 31;
                break;
            case 2:
                if(isLeapYear())
                {
                    daysInMonth = 29;
                }
                else
                {
                    daysInMonth = 28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                daysInMonth = 30;   
        }

        return daysInMonth;
    }



    public Date getNextDay(){
        try {
            if(day < getMaxDaysInMonth()){
                return new Date(day + 1, month, year);
            }
            else if(day == getMaxDaysInMonth() & month < 12){
                return new Date(1, month+1, year);
            }
            else if(day == getMaxDaysInMonth() & month == 12){
                return new Date(1, 1, year+1);
            }
        } catch (Exception e) {
            System.out.println("You have entered an invalid Date.");
            e.printStackTrace();

        }
        return null;
    }




    public int getDay(){
        return day;
    }

    public int getMonth(){
        return month;
    }

    public int getYear(){
        return year;
    }

    public boolean isLeapYear(){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    public String toString(){
        return this.day + "." + this.month + "." + this.year;
    }
}




public class NextDay {

           public static void main(String args[]) throws Exception{
              Date dateObj = new Date(28, 2, 2015);
              System.out.println("Old Date: " + dateObj.getDay() + "." + dateObj.getMonth() + "." + dateObj.getYear() + ".");
              System.out.println("The next day is " + dateObj.getNextDay().toString() + ".");
           }
}

Solution

  • I suggest to throw an exception. As part of the exception you could also give some explanation about was was/is wrong.

    Returning a specific date is not a good idea, because this Date could also match to a Date that was created in a valid way. I.e. the Date 01-01-1970 can be created without any problem, right? So it should not be returned as kind or marker for a problem.

    Concerning your Date representation keep in mind that you can initialize a Date with Integer.MAX_VALUE for month, day, and year. What is the expected output in this case?