DESKTOP-O1UAITJ_20200901-史鹏华

一,问题

通过看源码发现Date重写了从Object类继承equals方法以后比较的是两个Date对象调用getTime()方法后的值,以及它 的setTime()和getTime()都是对它的 private transient long fastTime;字段赋值取值。

       Date first = new Date( current - day * 365 * 20 );
       Date second = new Date( current );
       System.out.println( first.equals( third ) );    
  public long getTime() {
    return getTimeImpl();
}

private final long getTimeImpl() {
    if (cdate != null && !cdate.isNormalized()) {
        normalize();
    }
    return fastTime;
}

/**
 * Sets this {@code Date} object to represent a point in time that is
 * {@code time} milliseconds after January 1, 1970 00:00:00 GMT.
 *
 * @param   time   the number of milliseconds.
 */
public void setTime(long time) {
    fastTime = time;
    cdate = null;
}
 public boolean equals(Object obj) {
        return obj instanceof Date && getTime() == ((Date) obj).getTime();
    } 


二,吐槽

自然排序还不能熟练应用。。。。。。

标签

© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1