Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean coding for date

Tags:

java

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.


2 Answers

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());
}
like image 198
Mureinik Avatar answered Feb 25 '26 16:02

Mureinik


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;
}
like image 44
Roshana Pitigala Avatar answered Feb 25 '26 15:02

Roshana Pitigala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!