有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在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()方法在比较使其能够返回-101的日期时的计算或算法是什么

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

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) 个答案

  1. # 1 楼答案

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

    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. # 3 楼答案

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

    参考:https://developer.android.com/reference/java/util/Calendar

    ^{}创建自己的类

    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";
        }
    }
    
  4. # 4 楼答案

    实现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;
        }
    
    }