This content has been marked as final. Show 8 replies
My code above is only for testing problem not real life program. In real code one object is java.util.Date and second is taken from database row implementation (in database DATE and in ViewObject is oracle.jbo.domain.Date). To compare both I use equals() metod.Of course as I hope is clear from first message not directly but with result of getValue() method.
As much as this looks like a bug, it also looks like a "documented feature".
"Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. 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. As a result, the Timestamp.equals(Object) method is not symmetric with respect to the java.util.Date.equals(Object) method. Also, the hashcode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation."
"Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance."
But, also the java.util.Date equals() method is documented with:
"Thus, two Date objects are equal if and only if the getTime method returns the same long value for both."
And in your example, this ...
results in this ...
System.out.println("oraDate.getValue().getTime() = " + oraDate.getValue().getTime()); System.out.println("utilDate.getTime() = " + utilDate.getTime());
Interesting question, maybe someone with more "date experience" can answer this.
oraDate.getValue().getTime() = 1172737018000 utilDate.getTime() = 1172737018000
thank Jan for your replies, and sorry for my delay - I was far from computers recently :-).
For me it is not problem with java.util.Date and java.sql.Timestamp but oracle.jbo.domain.Date. It results from how java.util.Date and java.sql.Timestamp relates and that in Timestamp class some methods are overridden. Instead of casting to java.util.Date in return of getValue() method of oracle.jbo.domain.Date class should be creating new java.utilDate object. This way always result from getValue() will always be java.util.Date.
And, more general, it is also very good example how careful one should be with casting. Thou casting in oracle.jbo.domain.Date.getValue() is valid, results in very error prone design.
I understand your issue results from the oracle.jbo.domain.Date class implementation.
But, because "the inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance" a discussion about this becomes more confusing because the term "is a" no longer has its typical semantics.
yes you're of course right about merit of this problem. But I think that if it is possible to make something clear why not to do this way? If oracle.jbo.domain.Date.getValue() would return always java.util.Date (as it is the case when not using casting but new java.utilDate() ) error prone situation would no longer exist. And I suppose this change is safe for existing applications code.