There are problems in two lines:
In that:
java.util.Date date2 = (java.util.Date(date.clone()));
it is right to make a cast
for java.util.Date
because clone()
returns the type Object
:
java.util.Date date2 = (java.util.Date)date.clone();
and in that:
System.out.println(date.equals(date2)));
have an extra parentheses, just remove the last.
Complete and trouble free code:
java.util.Date date = new java.util.Date();
java.util.Date date1 = date;
java.util.Date date2 = (java.util.Date)date.clone();
System.out.println(date==date1); // saída: true
System.out.println(date==date2); // saída: false
System.out.println(date.equals(date2)); // saída: true
Online Example
In itself site already has a question and answers which may explain the reasons for the results obtained in the operations.
Reading:
The way it is 2 errors! 2 lines have problems in this
java.util.Date date2 = (java.util.Date(date.clone()));
and in thatSystem.out.println(date.equals(date2)));
– novic
and to make no mistake I would have to stay as?
– CSAnimor
I decided to write an answer and also explain the problems.
– novic