Compare Date object with a TimeStamp in Java

后端 未结 10 863
悲&欢浪女
悲&欢浪女 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.

提交回复
热议问题