H
Harold Yarmouth
Jean-Baptiste Nizet said:I have the feeling that Calendar.getInstance() is not returning a
singleton, and that it is also not zeroing out newly-created instances.
Of course it's not returning a singleton, since the javadoc of the
getInstance method explicitely says that "The Calendar returned is
based on the current time [...]".
That's ambiguous. Could have meant the current time from the viewpoint
of the guy writing the documentation, so, the time the documentation got
written.
Yes, that would be perverse.
And yes, I've seen worse.
The calendar object reuses the milliseconds as they were at the time
of the creation, i.e. of your getInstance call. But since you're
calling getInstance twice, you have two different instances of
Calendar, each with different milliseconds.
And therein derives the problematic behavior: using Calendar in the
"natural way" as a date builder results in dates that have an
essentially-random component.