Why Java’s Date isn’t deprecated, even though it might look like it

This shouldn’t be news at this point (JDK 1.5 was released 5 years ago), but in Java 5 Sun decided to deprecate large swaths of java.util.Date’s methods and constructors and introduce the Calendar class, which can handle locale-specific conversions and manipulations of the units of time we humans are used to seeing. This has created some confusion here and there about whether to represent datetimes as Dates or as Calendars, as it looks at a casual glance like Date has been more or less completely deprecated. It remains, however, as the preferred way of representing datetimes in Java. An explanation is after the jump.

Remember, it never says that Date itself is deprecated, only the methods of conversion are. Also, the javadoc for Calendar explicitly describes it as a way of converting between, not representing, different points in time relative to different calendar systems. The idiomatic usage pattern has a datetime being represented as a Date, but then converted to the locale-specific Calendar object to manipulate the date by saying “2 days from now” or “1 month ago”, which are locale-specific manipulations.

It seems like this is more complecated than it needs to be. Why not just keep datetimes as Calendars and be done with it? There’s a major problem with this approach, however. Since Calendar is abstract, one concrete calendar object may work very differently from another. For instance, say an entitiy (A) in Locale A computes and stores a Calendar object, and then another entity (B) in Locale B recieves the Calendar. If B attempts to say “one month from this point in time” using the Calendar from A, it will act according to A’s Locale, not B’s. Date does not have this problem, and is a representation of UTC. Datetimes are usually best represented as Date objects, and manipulated as Calendars, and then converted back to Dates for storage or communication between systems.

It would be nice in the future to be able to have convenience functions on Date to do Calendar-like things within whatever the current Locale is, but for now, we have to keep that distinction.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s