Compare Date object with a TimeStamp in Java

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

提交回复
热议问题