Java 日期的布尔编码 上课日期{ 私人国际日; 私人整数月; 私人国际年; 公开日期(){ } 公开日期(整数天、整数月、整数年){ this.day=天; 本月=月; 今年=年; } public int getDay(){ 今天返回; } 公共整数getMonth(){ 本月返回; } 公共int getYear(){ 今年返回; } 公共无效设定日(整数日){ 日=输入日; } 公共无效设置月(整数月){ 月份=输入的月份; } 公共年(国际年){ 年份=输入年份; } 公共字符串toString(){ 返回getDay()+“/”+getMonth()+“/”+getYear(); } 公共布尔值isEarlier(日期){ 如果(enteredDay.getDay()

Java 日期的布尔编码 上课日期{ 私人国际日; 私人整数月; 私人国际年; 公开日期(){ } 公开日期(整数天、整数月、整数年){ this.day=天; 本月=月; 今年=年; } public int getDay(){ 今天返回; } 公共整数getMonth(){ 本月返回; } 公共int getYear(){ 今年返回; } 公共无效设定日(整数日){ 日=输入日; } 公共无效设置月(整数月){ 月份=输入的月份; } 公共年(国际年){ 年份=输入年份; } 公共字符串toString(){ 返回getDay()+“/”+getMonth()+“/”+getYear(); } 公共布尔值isEarlier(日期){ 如果(enteredDay.getDay(),java,Java,我很难找到最后一种方法。它必须是布尔值,如果日期早于它,则返回true。我的问题(至少据我所知)是弄清楚在“的任何一边写什么,我会在一年、一个月、一天中依次比较,直到你找到一对严格是早一点还是晚一点的 使用比较器,特别是在Java 8简洁的语法中,可以在此处为您节省大量样板代码: class Date { private int day; private int month; private int year; public Date() { }

我很难找到最后一种方法。它必须是布尔值,如果日期早于它,则返回true。我的问题(至少据我所知)是弄清楚在“的任何一边写什么,我会在一年、一个月、一天中依次比较,直到你找到一对严格是早一点还是晚一点的

使用
比较器
,特别是在Java 8简洁的语法中,可以在此处为您节省大量样板代码:

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;
        }
    }
}
public boolean isEarlier(日期其他){
返回比较器.comparingit(日期::getYear)
.然后比较(日期:getMoth)
.Then比较(日期::getDay)
.比较(这个,其他)<0;
}
编辑:
要回答评论中的问题,您当然可以手动比较每个字段:

public boolean isEarlier(Date other) {
    return Comparator.comparingInt(Date::getYear)
                     .thenComparingInt(Date::getMoth)
                     .thenComparingInt(Date::getDay)
                     .compare(this, other) < 0;
}
public boolean isEarlier(日期其他){
if(getYear()other.getYear()){
返回false;
}
//如果我们到达这里,两个日期的年份是相等的
if(getMonth()other.getMonth()){
返回false;
}
//如果我们到达这里,两个日期的年和月是相等的
返回getDay()
当然,这可以压缩为一个布尔语句,尽管它更优雅还是不那么优雅在某种程度上取决于旁观者(括号不是严格需要的,但它们如何使代码更清晰):

public boolean isEarlier(日期其他){
返回(getYear()
如果您不想使用任何其他库,请尝试以下方法:

public boolean isEarlier(Date other) {
    return (getYear() < other.getYear()) ||
           (getYear() == other.getYear() && 
            getMonth() < other.getMonth()) ||
           (getYear() == other.getYear() && 
            getMonth() == other.getMonth() && 
            getDay() < other.getDay());
}
public boolean isEarlier(日期){
if(this.getYear()
您要求对您的代码进行反馈,所以就在这里

你的构造函数和set方法决定了日期的值,你希望日期是有效的,不是吗

现在我可以这样做:

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;
}
这样就可以了

如果要允许无效日期,请至少添加一个isValid方法

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

你为什么要重新发明轮子?只需在imo中使用java日期时间API。可能应该是
public boolean isEarlier(日期输入日期){
starters@ShanuGupta这可能是一个任务——你永远不知道。知道这些API是如何在内部工作也是一件好事。<代码>早些时候方法也应该考虑一年和一个月来确定日期是否早。这个问题以前被问过和回答过,有人请,我们把原始的HO放在哪里了?你在这里做硬编码吗?@DerickDaniel可能不是这个词。。。xD@JohnDOe您的代码中有很多错误,我相信您是匆忙复制的。例如:在
setYear(int year)
中没有名为
enteredYear
的变量,它应该是
this.year=year
。除此之外,一切看起来都很好。
Date date = new Date(102, 17, -300); 
public boolean isValid();