Compare Date object with a TimeStamp in Java

后端 未结 10 865
悲&欢浪女
悲&欢浪女 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:04
    1. date.equals(stamp) return true AND stamp equals(date) returns false. REASON :Date neglects the nanosecond part of timestamp and since the other parts happen to be equal so the result is equal. The fractional seconds - the nanos - are separate.The Timestamp.equals(Object) method never returns true when passed a value of type java.util.Date because the nanos component of a date is unknown. See here for more details

      1. date.compareTo(stamp) == 0 returns false AND stamp.compareTo(date) == 0 returns true. REASON: According to this bug compareTo function will behave as it does.
    0 讨论(0)
  • 2020-12-09 03:05

    Timestamp's nanos value is NOT the number of nanoseconds - it's a nanosecond-resolution number of millis (i.e. fractional seconds). As such, in the Timestamp constructor, it is setting the time on the super to be without milliseconds. Therefore, the Timestamp will always have a lower value for the member fastTime (used in Date's compareTo()) than the corresponding Date (unless, of course, it has no fractional seconds).

    Check the source for Timestamp at line 110.

    0 讨论(0)
  • 2020-12-09 03:06

    A Small note on Implementaion inheritance and Type inheritance..

    "An object's class defines how the object is implemented. In contrast, an object's type only refers to its interface. Class inheritance(Implementation inheritance) defines an object's implementation in terms of another object's implementation. Type inheritance describes when an object can be used in place of another."

    Timestamp and Date classes have implementation inheritance as JAVADOC said.

    0 讨论(0)
  • 2020-12-09 03:09

    I've resolved converting both Date and TimeStamp into a Calendar object and then i've compared the single Calendar's properties:

    Calendar date = Calendar.getInstance();
    date.setTimeInMillis(dateObject.getTime());
    Calendar timestamp = Calendar.getInstance();
    timestamp.setTimeInMillis(timestampObject.getTime());
    if (effettoAppendice.get(Calendar.DAY_OF_MONTH) == timestamp.get(Calendar.DAY_OF_MONTH) &&
        effettoAppendice.get(Calendar.MONTH) == timestamp.get(Calendar.MONTH) &&
        effettoAppendice.get(Calendar.YEAR) == timestamp.get(Calendar.YEAR)) {
        System.out.println("Date and Timestamp are the same");
    } else {
        System.out.println("Date and Timestamp are NOT the same");
    }
    

    Hope this helps.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 03:12

    Nican explained the equals part, about compareTo:

    • Timestamp has a compareTo(Date) method that converts it to Timestamp internally
    • Date does the comparison by downcasting (since Timestamp is a subclass of it); but as the javadoc states: "The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance"

    Which of course is an horrible idea, in my opinion.

    0 讨论(0)
提交回复
热议问题