Compare Date object with a TimeStamp in Java

后端 未结 10 872
悲&欢浪女
悲&欢浪女 2020-12-09 02:32

When I test this code:

java.util.Date date = new java.util.Date();
java.util.Date stamp = new java.sql.Timestamp(date.getTime());

assertTrue(date.equals(sta         


        
10条回答
  •  情歌与酒
    2020-12-09 03:11

    Take a look at the source code compare method for Timestamp:

    public boolean equals(java.lang.Object ts) {
      if (ts instanceof Timestamp) {
        return this.equals((Timestamp)ts);
      } else {
        return false;
      }
    }
    

    http://www.docjar.com/html/api/java/sql/Timestamp.java.html

    It will only ever return true if the comparing object is a timestamp. Also, here is the Date source code: http://www.docjar.com/html/api/java/util/Date.java.html , and since Timestamp inherits Date, it can compare it.

提交回复
热议问题