2
class Date {

    private int day;
    private int month;
    private int year;

    public Date() {
    }

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

    public int getDay() {
        return this.day;
    }

    public int getMonth() {
        return this.month;
    }

    public int getYear() {
        return this.year;
    }

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

    public void setMonth(int month) {
        month = enteredMonth;
    }

    public void setYear(int year) {
        year = enteredYear;
    }

    public String toString() {
        return getDay() + "/" + getMonth() + "/" + getYear();
    }

    public boolean isEarlier(Date) {
        if (enteredDay.getDay() < day) {
            return true;
        } else {
            return false;
        }
    }
}

I'm having trouble getting the last method to work. It must be boolean and return true if a date is earlier than it. My problem (at least as far as I know) is figuring out what to write either side of the '<' operator. Any feedback on the rest of the code would be greatly appreciated.

Mureinik
  • 297,002
  • 52
  • 306
  • 350

3 Answers3

5

I'd go over the year, month, and day and compare each in turn until you find a pair that's strictly earlier or later.

Using a Comparator, especially in Java 8's neat syntax, could save you a lot of boilerplate code here:

public boolean isEarlier(Date other) {
    return Comparator.comparingInt(Date::getYear)
                     .thenComparingInt(Date::getMoth)
                     .thenComparingInt(Date::getDay)
                     .compare(this, other) < 0;
}

EDIT:
To answer the question in the comments, you can of course manually compare each field:

public boolean isEarlier(Date other) {
    if (getYear() < other.getYear()) {
        return true;
    } else if (getYear() > other.getYear()) {
        return false;
    }

    // If we reached here, both dates' years are equal

    if (getMonth() < other.getMonth()) {
        return true;
    } else if (getMonth() > other.getMonth()) {
        return false;
    }

    // If we reached here, both dates' years and months are equal

    return getDay() < other.getDay();
}

This of course could be compacted to a single boolean statement, although whether it's more elegant or less elegant is somewhat in the eye of the beholder (the parenthesis aren't strictly needed, but IMHO they make the code clearer):

public boolean isEarlier(Date other) {
    return (getYear() < other.getYear()) ||
           (getYear() == other.getYear() && 
            getMonth() < other.getMonth()) ||
           (getYear() == other.getYear() && 
            getMonth() == other.getMonth() && 
            getDay() < other.getDay());
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
4

If you don't want to use any other libraries, try this:

public boolean isEarlier(Date date) {
    if (this.getYear() < date.getYear()) {
        return true;
    } else if (getYear() == date.getYear()
            && this.getMonth() < date.getMonth()) {
        return true;
    } else if (getYear() == date.getYear()
            && this.getMonth() == date.getMonth()
            && this.getDay() < date.getDay()) {
        return true;
    }
    return false;
}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
1

You asked for feedback on your code so here it is

Your constructor and set methods determine the value of the date, you'd want the date to be valid won't you?

right now i could do this:

Date date = new Date(102, 17, -300); 

and it would be considered okay.

If you want to allow invalid dates at least add a isValid method

public boolean isValid();
Ran Elgiser
  • 66
  • 1
  • 1
  • 14