What time is it ?
// defined previously
// Date aDate = ...
Date date = new Date();
date = new Date(date.getTime());
if (aDate != null && aDate == date) {
date = new Date(date.getTime() + 10);
}
Hum, the biggest problem with this one is the equality condition test (aDate == date).
In java, == DOESN'T compare value, except for primitive data type, it compares pointer.
That's it, it test if two objects are same. To test if two object are equal (in value) use Object.equals method.
Then, it is trivial that
Date date = new Date();is stupid.
date = new Date(date.getTime());
Date date = new Date();is sufficient.
No comments:
Post a Comment