I’m working with Date objects in my code, and the documentation on many of the methods I’m using say that the method is deprecated and that I should use the Calendar object and its equivalent methods. The easiest example to remember is Date.getHours() and Date.getMinutes() methods say to use Calendar.get(Calendar.HOUR), etc.
But, in the case where I’m working with two dates, performing some math on them and then comparing them, they seem to be referring to the same instance on Calendar. The documentation for Calendar says to use Calendar.getInstance(), but that seems to be returning a singleton Calendar instance.
The Date object was working just fine for me, but I’m afraid to use methods marked deprecated because that makes me feel like my code could break in a future version of the api.
Is it safe to use Date? Or is there a way to fetch unique Calendar instances?
Calendar.getInstance() does not return a singleton. The following test
produces
When doing date math, you should never work on the underlying milliseconds, because then you make the assumption that every day has 24 hours and every hour has 3600 seconds. Use the Calendar methods instead, or switch to JodaTime.