Could someone please advise the current "best practice" around Date
and Calendar
types.
When writing new code, is it best to always favour Calendar
over Date
, or are there circumstances where Date
is the more appropriate datatype?
Could someone please advise the current "best practice" around Date
and Calendar
types.
When writing new code, is it best to always favour Calendar
over Date
, or are there circumstances where Date
is the more appropriate datatype?
The best way for new code (if your policy allows third-party code) is to use the Joda Time library.
Both, Date and Calendar, have so many design problems that neither are good solutions for new code.
Date
andCalendar
are really the same fundamental concept (both represent an instant in time and are wrappers around an underlyinglong
value).One could argue that
Calendar
is actually even more broken thanDate
is, as it seems to offer concrete facts about things like day of the week and time of day, whereas if you change itstimeZone
property, the concrete turns into blancmange! Neither objects are really useful as a store of year-month-day or time-of-day for this reason.Use
Calendar
only as a calculator which, when givenDate
andTimeZone
objects, will do calculations for you. Avoid its use for property typing in an application.Use
SimpleDateFormat
together withTimeZone
andDate
to generate display Strings.If you're feeling adventurous use Joda-Time, although it is unnecessarily complicated IMHO and is soon to be superceded by the JSR-310 date API in any event.
I have answered before that it is not difficult to roll your own
YearMonthDay
class, which usesCalendar
under the hood for date calculations. I was downvoted for the suggestion but I still believe it is a valid one because Joda-Time (and JSR-310) are really so over-complicated for most use-cases.Date should be re-developed. Instead of being a long interger, it should hold year, month, date, hour, minute, second, as separate fields. It might be even good to store the calendar and time zone this date is associated with.
In our natural conversation, if setup an appointment at Nov. 1, 2013 1pm NY Time, this is a DateTime. It is NOT a Calendar. So we should be able to converse like this in Java as well.
When Date is stored as a long integer (of mili seconds since Jan 1 1970 or something), calculating its current date depends on the calendar. Different calendars will give different date. This is from the prospective of giving an absolute time (eg 1 trillion seconds after Big Bang). But often we also need a convenient way of conversation, like an object encapsulating year, month etc.
I wonder if there are new advances in Java to reconcile these 2 objectives. Maybe my java knowledge is too old.
Date
s should be used as immutable points in time;Calendar
s are mutable, and can be passed around and modified if you need to collaborate with other classes to come up with a final date. Consider them analogous toString
andStringBuilder
and you'll understand how I consider they should be used.(And yes, I know Date isn't actually technically immutable, but the intention is that it should not be mutable, and if nothing calls the deprecated methods then it is so.)
A little bit late at party, but Java has a new Date Time API in JDK 8. You may want to upgrade your JDK version and embrace the standard. No more messy date/calendar, no more 3rd party jars.
Date is a simpler class and is mainly there for backward compatibility reasons. If you need to set particular dates or do date arithmetic, use a Calendar. Calendars also handle localization. The previous date manipulation functions of Date have since been deprecated.
Personally I tend to use either time in milliseconds as a long (or Long, as appropriate) or Calendar when there is a choice.
Both Date and Calendar are mutable, which tends to present issues when using either in an API.