如何在Java中重新创建compareTo方法?

时间:2019-03-20 02:51:22

标签: java compareto

在Java中,日期的compareTo()方法在这里如何工作?我知道当您比较两个日期时,如果相等,结果将始终为0,如果日期为1compareTo()参数中比较的日期较早,如果在参数中的日期较新,则比较-1

//Just an example

String[] da = {"01/14/1975", "08/20/1975", "08/20/1975"};
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = new Date();
Date d2 = new Date();


//this outputs 1 because d2 is older than d1
d1 = f.parse(da[1]);            
d2 = f.parse(da[0]);     
System.out.println(d1.compareTo(d2));

//this outputs 0 because dates are the same
d1 = f.parse(da[1]);            
d2 = f.parse(da[2]);     
System.out.println(d1.compareTo(d2));

//this outputs -1 because d2 is more recent than d1
d1 = f.parse(da[0]);            
d2 = f.parse(da[1]);     
System.out.println(d1.compareTo(d2));

现在,我想比较日期而不使用compareTo()方法或Java中的任何内置方法。我想尽可能地只使用Java中的基本运算符。

在比较使日期能够返回compareTo()-10的日期时,1方法的计算或算法是什么?

编辑: 在我的书中存在示例问题的情况下,禁止使用java.util.Date,应该做的是创建自己的日期对象,如下所示:

public class DatesObj
{
    protected int day, month, year;

    public DatesObj (int mm, int dd, int yyyy) {
        month = mm;
        day = dd;        
        year = yyyy;
    }

    public int getMonth() { return month; }

    public int getDay() { return day; }

    public int getYear() { return year; }

}

现在,我该如何像他们int那样比较它,并确定哪个较旧,哪个较新?

4 个答案:

答案 0 :(得分:2)

实施Comparable并覆盖compareTo()。

class DatesObj implements Comparable<DatesObj>{
    protected int day, month, year;

    public DatesObj(int mm, int dd, int yyyy) {
        month = mm;
        day = dd;
        year = yyyy;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    public int getYear() { return year; }

    @Override
    public int compareTo(DatesObj o) {

        int diff = this.year - o.year;

        if(diff != 0) {
            return diff;
        }

        diff = this.month - o.month;

        if(diff != 0) {
            return diff;
        }

        return this.day - o.day;
    }

}

答案 1 :(得分:2)

比较年份。如果两个日期的年份相同,则比较月份。 如果月份相同,则比较日期。

public int compareDate(DatesObj d) { 
    if (this.year != d.year) { 
        if (this.year > d.year)
            return 1;
        else
            return -1;
    }
    if (this.month != d.month) {
        if (this.month > d.month)
            return 1;
        else
            return -1;
    }
    if (this.day != d.day) {
        if (this.day > d.day)
            return 1;
        else
            return -1;
    }
    return 0; 
}

答案 2 :(得分:2)

如果要比较两个日期就好像它们只是普通整数一样,则必须首先将每个日期转换为普通整数。将日期的年/月/日表示形式转换为普通整数的最简单方法(可以与其他日期的普通整数进行有效比较)是按照以下顺序进行排列:年份第一,下个月,最后一天:

//  in DateObj class....
public int getDateInt() {
    return (yyyy * 10000) + (mm * 100) + dd;
}

因此,对于2019年3月19日,您将获得20190319,对于1941年12月7日,您将获得19411207;通过比较日期的“整数”版本,您可以看到:

  • 19411207 <20190319,就像1941年12月7日早于2019年3月19日;
  • 20190319> 19411207,就像2019年3月19日晚于1941年12月7日;
  • 19411207!= 20190319,就像1941年12月7日和2019年3月19日是不同的日期一样

使用此特定实现,您只能将日期限制在普通时代之内,并且距未来不超过20万年。但是,只要稍作调整,您就可以轻松地处理这些范围之外的日期,就像教科书经常说的那样,我会把这个练习留给读者。

答案 3 :(得分:1)

Ref:https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

Ref:https://developer.android.com/reference/java/util/Calendar

使用Interface Comparable

创建自己的课程
class DateCompare implements Comparable<Date>
{
    protected int day, month, year;

    public DateCompare(int mm, int dd, int yyyy) {
        month = mm;
        day = dd;
        year = yyyy;
    }

    @Override
    public int compareTo(Date o) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(o);
        int diff = this.year - cal.get(Calendar.YEAR);

        if(diff != 0) {
            return diff;
        }

        diff = this.month - cal.get(Calendar.MONTH);

        if(diff != 0) {
            return diff;
        }

        return this.day - cal.get(Calendar.DAY_OF_MONTH);
    }
    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    public int getYear() { return year; }
}

其他更有用 https://gist.github.com/Ashusolanki/fed3b6a680092985ac0ab93ed70fcd7c

private String postTime(Date date) 
{
    long postTime = date.getTime();
    long atTime = System.currentTimeMillis();

    long diff = atTime - postTime;
    long sec = TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS);
    if (sec >= 60) {
        long minit = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
        if (minit >= 60) {
            long hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);

            if (hours >= 24) {
                long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
                return days + " Days Ago";
            } else {
                return hours + " Hours Ago";
            }
        } else {
            return minit + " Minutes Ago";
        }
    } else {
        return sec + " Secounds Ago";
    }
}